diff --git a/Journaley/Forms/MainForm.cs b/Journaley/Forms/MainForm.cs index 9a62d8a..b5a1c90 100644 --- a/Journaley/Forms/MainForm.cs +++ b/Journaley/Forms/MainForm.cs @@ -1,6 +1,6 @@ namespace Journaley.Forms { - extern alias MDS; + extern alias MDD; using System; using System.Collections.Generic; @@ -21,7 +21,7 @@ using Journaley.Core.Utilities; using Journaley.Core.Watcher; using Journaley.Utilities; - using MDS.MarkdownSharp; + using MDD.MarkdownDeep; using Pabo.Calendar; using Squirrel; @@ -475,7 +475,8 @@ private Markdown Markdown if (this.markdown == null) { this.markdown = new Markdown(); - ((MarkdownOptions)this.markdown.Options).AutoNewLines = true; + this.markdown.ExtraMode = true; + this.markdown.SafeMode = false; } return this.markdown; @@ -638,53 +639,22 @@ private void OnGetMinMaxInfo(ref Message m) /// private void UpdateWebBrowser() { + string initialString = this.SelectedEntry.EntryText; + + // Fix for backtick fenced code blocks is to replace it into tilde fenced. + initialString = initialString.Replace("```", "~~~"); + + string docType = ""; + string formattedString = string.Format( - "
{3}
", + "{0}\n\n\n\n
\n{4}
\n\n", + docType, this.GetWebBrowserTypefaceCSS(), this.GetWebBrowserSizeCSS(), this.CustomCSS ?? string.Empty, - Markdown.Transform(this.SelectedEntry.EntryText)); - - this.webBrowser.DocumentText = this.RemoveLineBreaksWithinLists(formattedString); - } - - /// - /// Removes the wrong line breaks within nested ordered/unordered lists. - /// Hack to fix the issue #114. - /// - /// The entry content formatted by MarkdownSharp. - /// correctly formatted string with the wrong line breaks removed. - private string RemoveLineBreaksWithinLists(string formattedString) - { - System.Text.StringBuilder builder = new System.Text.StringBuilder(); - string pattern = @"^"; - - var lines = formattedString.Split( - new string[] { "\r\n", "\n" }, - StringSplitOptions.None); - - for (int i = 0; i < lines.Length - 1; ++i) - { - string line = lines[i]; - string nextLine = lines[i + 1]; - - // Remove the "
" tags only if the current line and the next line are starting - // with the opening/closing list tags. By doing this, it can prevent removing - // line breaks that are intentionally added by the user. - if (Regex.IsMatch(line, pattern) && Regex.IsMatch(nextLine, pattern)) - { - while (line.EndsWith("
")) - { - line = line.Substring(0, line.LastIndexOf("
")); - } - } - - builder.AppendLine(line); - } - - builder.AppendLine(lines.Last()); + PostMarkdownParser.PostMarkdown(Markdown.Transform(initialString))); - return builder.ToString(); + this.webBrowser.DocumentText = formattedString; } /// diff --git a/Journaley/Journaley.csproj b/Journaley/Journaley.csproj index aeea8da..a62ff28 100644 --- a/Journaley/Journaley.csproj +++ b/Journaley/Journaley.csproj @@ -81,9 +81,10 @@ False ..\packages\squirrel.windows.0.99.2\lib\Net45\ICSharpCode.SharpZipLib.dll - - ..\packages\MarkdownSharp.1.13.0.0\lib\35\MarkdownSharp.dll - MDS + + ..\packages\MarkdownDeep.NET.1.5\lib\.NetFramework 3.5\MarkdownDeep.dll + True + MDD ..\packages\winapicp.1.1\lib\Microsoft.WindowsAPICodePack.dll @@ -202,6 +203,7 @@ True Resources.resx + diff --git a/Journaley/Resources/JournaleyLarge.css b/Journaley/Resources/JournaleyLarge.css index 41307a7..faf09f7 100644 --- a/Journaley/Resources/JournaleyLarge.css +++ b/Journaley/Resources/JournaleyLarge.css @@ -45,3 +45,7 @@ div { img { width: 100%; } + +pre { + word-wrap: break-word; +} \ No newline at end of file diff --git a/Journaley/Resources/JournaleyMedium.css b/Journaley/Resources/JournaleyMedium.css index d682e75..df19d13 100644 --- a/Journaley/Resources/JournaleyMedium.css +++ b/Journaley/Resources/JournaleyMedium.css @@ -45,3 +45,7 @@ div { img { width: 100%; } + +pre { + word-wrap: break-word; +} \ No newline at end of file diff --git a/Journaley/Resources/JournaleySmall.css b/Journaley/Resources/JournaleySmall.css index 3cf4e9b..268ec13 100644 --- a/Journaley/Resources/JournaleySmall.css +++ b/Journaley/Resources/JournaleySmall.css @@ -45,3 +45,7 @@ div { img { width: 100%; } + +pre { + word-wrap: break-word; +} \ No newline at end of file diff --git a/Journaley/Utilities/PostMarkdownParser.cs b/Journaley/Utilities/PostMarkdownParser.cs new file mode 100644 index 0000000..2a21cc8 --- /dev/null +++ b/Journaley/Utilities/PostMarkdownParser.cs @@ -0,0 +1,84 @@ +namespace Journaley.Utilities +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + /// + /// Helper class for fixing MarkdownDeep parsed HTML. + /// + public class PostMarkdownParser + { + /// + /// Perform fixes after MarkdownDeep parsing for publishing + /// to Journaley. + /// - automatically adds break tags on single line breaks. + /// - makes the first sentence into a header. + /// - properly parses code blocks enclosed in p tags into pre. + /// + /// Formatted HTML string + /// Properly formatted HTML string for publishing. + public static string PostMarkdown(string formattedString) + { + StringBuilder builder = new StringBuilder(); + StringBuilder paragraphBuilder = new StringBuilder(); + + // -1 - skips check and just dumps the line to builder. + // 0 - stumbles upon the beginning of a

tag/usual parsing. + // 1 - stumbles upon the end of

tag. + var parseState = -1; + + string line; + using (StringReader reader = new StringReader(formattedString)) + { + while ((line = reader.ReadLine()) != null) + { + if (line.Contains("

")) + { + parseState = 1; + } + else if (line.Contains("

")) + { + parseState = 0; + } + else if (line.Contains("
"))
+                    {
+                        parseState = -1;
+                    }
+
+                    if (parseState == 1)
+                    {
+                        if (line.Contains("

")) + { + builder.AppendLine(line); + parseState = -1; + } + else + { + paragraphBuilder.AppendLine(line + "
"); + } + } + else if (parseState == 0) + { + paragraphBuilder.AppendLine(line); + + string paragraph = paragraphBuilder.ToString(); + + builder.Append(paragraph); + paragraphBuilder.Clear(); + parseState = -1; + } + else + { + builder.AppendLine(line); + } + } + } + + return builder.ToString(); + } + } +} diff --git a/Journaley/packages.config b/Journaley/packages.config index cb49543..32cc7a9 100644 --- a/Journaley/packages.config +++ b/Journaley/packages.config @@ -1,7 +1,7 @@  - +