Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown improvements regarding tables, nested statements and special character support in links #1647

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OneMore/Commands/Edit/ConvertMarkdownCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public override async Task Execute(params object[] args)

var text = reader.ReadTextFrom(paragraphs, allContent);
text = Regex.Replace(text, @"<br>([\n\r]+)", "$1");
text = Regex.Replace(text, @"\<*input\s+type*=*\""checkbox\""\s+unchecked\s+[a-zA-Z *]*\/\>", "[ ]");
text = Regex.Replace(text, @"\<*input\s+type*=*\""checkbox\""\s+checked\s+[a-zA-Z *]*\/\>", "[x]");

var body = OneMoreDig.ConvertMarkdownToHtml(filepath, text);

Expand Down
4 changes: 4 additions & 0 deletions OneMore/Commands/File/ImportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace River.OneMoreAddIn.Commands
using System;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -578,6 +579,8 @@ private async Task ImportMarkdownFile(string filepath, CancellationToken token)
page.Title = Path.GetFileNameWithoutExtension(filepath);

var container = page.EnsureContentContainer();
body = Regex.Replace(body, @"\<*input\s+type*=*\""checkbox\""\s+unchecked\s+[a-zA-Z *]*\/\>", "[ ]");
body = Regex.Replace(body, @"\<*input\s+type*=*\""checkbox\""\s+checked\s+[a-zA-Z *]*\/\>", "[x]");

container.Add(new XElement(ns + "HTMLBlock",
new XElement(ns + "Data",
Expand All @@ -600,6 +603,7 @@ private async Task ImportMarkdownFile(string filepath, CancellationToken token)

converter = new MarkdownConverter(page);
converter.RewriteHeadings();
converter.RewriteTodo();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on this method. Should not affect entire page. I'm removing this line.


logger.WriteLine($"updating...");
logger.WriteLine(page.Root);
Expand Down
79 changes: 69 additions & 10 deletions OneMore/Commands/File/Markdown/MarkdownConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.Models;
using River.OneMoreAddIn.Styles;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
Expand Down Expand Up @@ -51,6 +52,28 @@ public void RewriteHeadings()
{
RewriteHeadings(outline.Descendants(ns + "OE"));
}

// added header spacing specific to markdown
var quickstyles = page.Root.Elements(ns + "QuickStyleDef");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad assumption. Should only affect Outline where we are converting markdown. Should abide by existing quick style definitions for page. If user wants spacing, they can create their own custom OneMore Style set and apply that after the conversion.

foreach (var quickstyle in quickstyles)
{
var name = quickstyle.Attribute("name").Value;
if (name.Equals("h1") || name.Equals("h2"))
{
replaceAtributes(quickstyle, spaceBefore: 0.8, spaceAfter: 0.5);
}
else
if (name.Equals("h3") || name.Equals("h4"))
{
replaceAtributes(quickstyle, spaceBefore: 0.3, spaceAfter: 0.3);
}
}
void replaceAtributes(XElement quickstyle, double spaceBefore, double spaceAfter)
{
quickstyle.SetAttributeValue("spaceBefore", spaceBefore.ToString("####0.00", CultureInfo.InvariantCulture));
quickstyle.SetAttributeValue("spaceAfter", spaceAfter.ToString("####0.00", CultureInfo.InvariantCulture));
}

}


Expand Down Expand Up @@ -141,8 +164,22 @@ public MarkdownConverter RewriteHeadings(IEnumerable<XElement> paragraphs)
}


/// <summary>
/// Applies standard OneNote styling to all recognizable headings in all Outlines
/// on the page
/// </summary>
public void RewriteTodo()
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here also, should only affect Outlines that were affected, not all Outlines on page. User can apply custom style after if they want, otherwise abide by existing styles on page.

foreach (var outline in page.BodyOutlines)
{
RewriteTodo(outline.Descendants(ns + "OE"));
}
}


/// <summary>
/// Tag current line with To Do tag if beginning with [ ] or [x]
/// Also :TAGS: will be handled here
/// All other :emojis: should be translated inline by Markdig
/// </summary>
/// <param name="paragraphs"></param>
Expand All @@ -158,21 +195,43 @@ public MarkdownConverter RewriteTodo(IEnumerable<XElement> paragraphs)
{
var cdata = run.GetCData();
var wrapper = cdata.GetWrapper();
if (wrapper.FirstNode is XText)
{
cdata.Value = wrapper.GetInnerXml();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does absolutely nothing? Convert cdata to wrapper and then set cdata to wrapper XML? That's the same thing.

}
while (wrapper.FirstNode is not XText && wrapper.FirstNode is not null)
{
wrapper = (XElement)wrapper.FirstNode;
}
if (wrapper.FirstNode is XText text)
{
var match = boxpattern.Match(text.Value);
// special treatment of todo tag
if (match.Success)
{
text.Value = text.Value.Substring(match.Length);

// ensure TagDef exists
var index = page.AddTagDef("3", "To Do", 4);

// inject tag prior to run
run.AddBeforeSelf(new Tag(index, match.Groups["x"].Value == "x"));
var org = text.Value;
var completed = match.Groups["x"].Value == "x";
text.Value = text.Value.Replace((completed ? "[x]" : "[ ]"), "");
cdata.Value = cdata.Value.Replace(org, text.Value);
page.SetTag(paragraph, tagSymbol: "3", tagStatus:completed,tagName:"todo");
}
else
{
// look for all other tags
foreach (var t in MarkdownEmojis.taglist)
{
// check for other tags
if (text.Value.Contains(t.name))
{
var org = text.Value;
text.Value = text.Value.Replace(t.name, "");
cdata.Value = cdata.Value.Replace(org, text.Value);
// ensure TagDef exists
page.SetTag(paragraph, tagSymbol: t.id, tagStatus: false, tagName: t.topic, tagType: t.type);
break;
}
}

// update run text
cdata.Value = wrapper.GetInnerXml();
}
}
}
Expand Down Expand Up @@ -204,7 +263,7 @@ public MarkdownConverter SpaceOutParagraphs(float spaceAfter)
public MarkdownConverter SpaceOutParagraphs(
IEnumerable<XElement> paragraphs, float spaceAfter)
{
var after = $"{spaceAfter:0.0}";
var after = spaceAfter.ToString("####0.00", CultureInfo.InvariantCulture);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an extension method, spaceAfter.ToInvariantString()


var last = paragraphs.Last();

Expand Down
36 changes: 36 additions & 0 deletions OneMore/Commands/File/Markdown/MarkdownEmojis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;

namespace River.OneMoreAddIn.Commands
{
public static class MarkdownEmojis
{
public static List<(string name, string id, string topic, int type)> taglist = new List<(string name, string id, string topic, int type)>
{
// (":todo:", "3", "todo" , 0),
(":question:", "6", "question" , 0),
(":star:", "13", "important", 0 ),
(":exclamation:", "17", "critical", 0),
(":phone:", "18", "phone", 0),
(":bulb:", "21", "idea", 0),
(":house:", "23", "address", 0),
(":three:", "33", "three", 0),
(":zero:", "39", "zero", 0),
(":two:", "51", "two", 0),
(":arrow_right:", "59", "main agenda item", 0),
(":one:", "70", "one", 0),
(":information_desk_person:","94", "discuss person a/b", 21),
(":bellsymbol:", "97", "bellsymbol", 0),
(":busts_in_silhouette:", "116", "busts_in_silhouette", 0),
(":bell:", "117", "bell", 0),
(":letter:", "118", "letter", 0),
(":musical_note:", "121", "musical_note", 0),
(":secret:", "131", "idea", 0),
(":book:", "132", "book", 0),
(":movie_camera:", "133", "movie_camera", 0),
(":zap:", "140", "lightning_bolt", 0),
(":o:", "1", "default", 0)
};


}
}
Loading