Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
RickStrahl committed Sep 1, 2017
2 parents dd1510a + f000b65 commit cf1440e
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 31 deletions.
10 changes: 3 additions & 7 deletions AddIns/WebLogAddin/WeblogPostMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ public bool DontStripH1Header
/// </summary>
[YamlIgnore]
public string RawMarkdownBody { get; set; }



static readonly Regex YamlExtractionRegex = new Regex("^---[\n,\r\n].*?^---[\n,\r\n]", RegexOptions.Singleline | RegexOptions.Multiline);


/// <summary>
/// Strips the Markdown Meta data from the message and populates
/// the post structure with the meta data values.
Expand Down Expand Up @@ -212,7 +208,7 @@ public static WeblogPostMetadata GetPostYamlConfigFromMarkdown(string markdown,

string extractedYaml = null;
//var match = YamlExtractionRegex.Match(markdown);
var match = YamlExtractionRegex.Match(markdown);
var match = MarkdownUtilities.YamlExtractionRegex.Match(markdown);
if (match.Success)
extractedYaml = match.Value;

Expand Down Expand Up @@ -290,7 +286,7 @@ public string SetPostYaml()
CustomFields = customFields;

string extractedYaml = null;
var match = YamlExtractionRegex.Match(markdown);
var match = MarkdownUtilities.YamlExtractionRegex.Match(markdown);
if (match.Success)
extractedYaml = match.Value;

Expand Down
15 changes: 5 additions & 10 deletions MarkdownMonster/Editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,16 +498,11 @@ var te = window.textEditor = {
if (pos > 0)
text = text.substr(0, pos - 1);

// strip of front matter
if (text.substr(0, 4) === "---\r" || text.substr(0, 4) === "---\n") {
pos = text.indexOf("\n---\n");
if (pos < 0)
pos = text.indexOf("\n---\r\n");
if (pos > -1) {
pos += 6;
text = text.substr(pos);
}
}
// strip off front matter.
var frontMatterExp = /^---[ \t]*$[^]+?^(---|...)[ \t]*$/m;
var match = frontMatterExp.exec(text);
if (match && match.index == 0)
text = text.substr(match[0].length);

var regExWords = /\s+/gi;
var wordCount = text.replace(regExWords, ' ').split(' ').length;
Expand Down
9 changes: 5 additions & 4 deletions MarkdownMonster/Editor/editorSpellcheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ var sc = window.spellcheck = {
setTimeout(function (line, isLast) {
var lineText = lines[line];

if (isFrontMatter && lineText == "---")
isFrontMatter = false;
if (line == 0 && lineText == "---")
isFrontMatter = true;
var trimText = lineText.trim();
if (isFrontMatter && (trimText == "---" || trimText == "..."))
isFrontMatter = false;
if (line == 0 && trimText == "---")
isFrontMatter = true;


if (lineText && lineText.length > 2 && lineText.substr(0, 3) === "```") {
Expand Down
12 changes: 10 additions & 2 deletions MarkdownMonster/_Classes/MarkdownDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,17 @@ public string Title


// Front Matter Title
if (lines.Length > 2 && lines[0] == "---")
if (lines.Length > 2 && (lines[0] == "---" || lines[0] == "..."))
{
var block = mmFileUtils.ExtractString(CurrentText, "---", "---", returnDelimiters: true);
var start = lines[0];
string end = "---";

var endBlock1 = CurrentText.IndexOf("---", 3);
var endBlock2 = CurrentText.IndexOf("...", 3);
if (endBlock2 > -1 && (endBlock2 == -1 || endBlock2 < endBlock1))
end = "...";

var block = mmFileUtils.ExtractString(CurrentText, start, end, returnDelimiters: true);
if (!string.IsNullOrEmpty(block))
{
title = StringUtils.ExtractString(block, "title: ", "\n").Trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ protected string ParseStrikeout(string html)
return html;
}

static readonly Regex YamlExtractionRegex = new Regex("^---[\n,\r\n].*?^---[\n,\r\n]", RegexOptions.Singleline | RegexOptions.Multiline);

/// <summary>
/// Strips
/// </summary>
Expand All @@ -55,7 +53,7 @@ protected string ParseStrikeout(string html)
public string StripFrontMatter(string markdown)
{
string extractedYaml = null;
var match = YamlExtractionRegex.Match(markdown);
var match = MarkdownUtilities.YamlExtractionRegex.Match(markdown);
if (match.Success)
extractedYaml = match.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public override string Parse(string markdown)

var htmlWriter = new StringWriter();
var renderer = CreateRenderer(htmlWriter);

Markdown.Convert(markdown, renderer, Pipeline);
var html = htmlWriter.ToString();

Expand Down
2 changes: 1 addition & 1 deletion MarkdownMonster/_Classes/Utilities/MarkdownUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static string HtmlToMarkdown(string html, bool noErrorUI = false)

#region Front Matter Parsing

static readonly Regex YamlExtractionRegex = new Regex("^---[\n,\r\n].*?^---[\n,\r\n]", RegexOptions.Singleline | RegexOptions.Multiline);
public static readonly Regex YamlExtractionRegex = new Regex(@"\A---[ \t]*\r?\n[\s\S]+?\r?\n(---|\.\.\.)[ \t]*\r?\n", RegexOptions.Multiline | RegexOptions.Compiled);

/// <summary>
/// Strips
Expand Down
194 changes: 194 additions & 0 deletions Tests/MarkdownMonster.Test/FrontMatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MarkdownMonster.Test
{
/// <summary>
/// Tests the ability to parse out front matter in standard
/// fenced and pandoc fenced formats.
/// </summary>
/// <remarks>If MSTest Framework v2 is used, these can become DataRows
/// in a DataTestMethod, with each</remarks>
[TestClass]
public class FrontMatterTests
{
/// <summary>
/// Base markdown to use for permutation testing.
/// </summary>
const string PermutationMarkdown = @"{lead}
title: This is the title
author: this is the author
{trail}
# Heading 1
---
This is some text
";
/// <summary>
/// Tests the ability to parse front matter that
/// begins with <c>---</c> and ends with <c>---</c>
/// </summary>
[TestMethod]
public void YamlDashOpenDashCloseTest()
{
var lead = "---";
var trail = "---";
YamlFrontMatterParseHasHeaderTest(lead, trail);
YamlFrontMatterParseHasHorizontalRuleTest(lead, trail);
YamlFrontMatterParseHasContentTest(lead, trail);
YamlFrontMatterParseHasNoTitleTest(lead, trail);
YamlFrontMatterParseHasNoAuthorTest(lead, trail);
}

/// <summary>
/// Tests the ability to parse front matter that
/// begins with <c>---</c> and ends with <c>...</c>
/// </summary>
[TestMethod]
public void YamlDashOpenDotCloseTest()
{
var lead = "---";
var trail = "...";
YamlFrontMatterParseHasHeaderTest(lead, trail);
YamlFrontMatterParseHasHorizontalRuleTest(lead, trail);
YamlFrontMatterParseHasContentTest(lead, trail);
YamlFrontMatterParseHasNoTitleTest(lead, trail);
YamlFrontMatterParseHasNoAuthorTest(lead, trail);
}

/// <summary>
/// Tests the ability to parse front matter that
/// begins with <c>---</c> and ends with <c>---</c>
/// and has trailing whitespace after the YAML fence
/// </summary>
[TestMethod]
public void YamlDashOpenDashCloseWithSpacesTest()
{
var lead = "--- ";
var trail = "--- ";
YamlFrontMatterParseHasHeaderTest(lead, trail);
YamlFrontMatterParseHasHorizontalRuleTest(lead, trail);
YamlFrontMatterParseHasContentTest(lead, trail);
YamlFrontMatterParseHasNoTitleTest(lead, trail);
YamlFrontMatterParseHasNoAuthorTest(lead, trail);
}

/// <summary>
/// Tests the ability to parse front matter that
/// begins with <c>---</c> and ends with <c>...</c>
/// and has trailing whitespace after the YAML fence
/// </summary>
[TestMethod]
public void YamlDashOpenDotCloseWithSpacesTest()
{
// Should fail
var lead = "--- ";
var trail = "... ";
YamlFrontMatterParseHasHeaderTest(lead, trail);
YamlFrontMatterParseHasHorizontalRuleTest(lead, trail);
YamlFrontMatterParseHasContentTest(lead, trail);
YamlFrontMatterParseHasNoTitleTest(lead, trail);
YamlFrontMatterParseHasNoAuthorTest(lead, trail);
}

/// <summary>
/// Tests that the header (h1) following the YAML front matter
/// appears in the generated HTML
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// [DataTestMethod]
/// [DataRow("---", "---")]
/// [DataRow("---", "...")]
/// [DataRow("--- ", "--- ")]
/// [DataRow("--- ", "... ")]
public void YamlFrontMatterParseHasHeaderTest(string lead, string trail)
{
string html = CreateMarkdownPermutation(lead, trail);
Assert.IsTrue(html.Contains("Heading 1"));
}

/// <summary>
/// Tests that the horizontal rule following the YAML front matter
/// appears in the generated HTML. This ensures that the separator
/// is not read as part of the front matter.
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// [DataTestMethod]
/// [DataRow("---", "---")]
/// [DataRow("---", "...")]
/// [DataRow("--- ", "--- ")]
/// [DataRow("--- ", "... ")]
public void YamlFrontMatterParseHasHorizontalRuleTest(string lead, string trail)
{
string html = CreateMarkdownPermutation(lead, trail);
Assert.IsTrue(html.Contains("<hr"));
}

/// <summary>
/// Tests that the body content following the YAML front matter
/// appears in the generated HTML
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// [DataTestMethod]
/// [DataRow("---", "---")]
/// [DataRow("---", "...")]
/// [DataRow("--- ", "--- ")]
/// [DataRow("--- ", "... ")]
public void YamlFrontMatterParseHasContentTest(string lead, string trail)
{
string html = CreateMarkdownPermutation(lead, trail);
Assert.IsTrue(html.Contains("This is some text"));
}

/// <summary>
/// Tests that the YAML front matter 'title' does not
/// appear in the generated HTML
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// [DataTestMethod]
/// [DataRow("---", "---")]
/// [DataRow("---", "...")]
/// [DataRow("--- ", "--- ")]
/// [DataRow("--- ", "... ")]
public void YamlFrontMatterParseHasNoTitleTest(string lead, string trail)
{
string html = CreateMarkdownPermutation(lead, trail);
Assert.IsFalse(html.Contains("title"));
}

/// <summary>
/// Tests that the YAML front matter 'author' does not
/// appear in the generated HTML
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// [DataTestMethod]
/// [DataRow("---", "---")]
/// [DataRow("---", "...")]
/// [DataRow("--- ", "--- ")]
/// [DataRow("--- ", "... ")]
public void YamlFrontMatterParseHasNoAuthorTest(string lead, string trail)
{
string html = CreateMarkdownPermutation(lead, trail);
Assert.IsFalse(html.Contains("author"));
}

/// <summary>
/// Arranges the permuted markdown and acts to parse it into HTML.
/// </summary>
/// <param name="lead">The leading characters.</param>
/// <param name="trail">The closing characters.</param>
/// <returns>The generated HTML for the permutation</returns>
private static string CreateMarkdownPermutation(string lead, string trail)
{
var markdown = PermutationMarkdown.Replace("{lead}", lead)
.Replace("{trail}", trail);
var parser = MarkdownParserFactory.GetParser(usePragmaLines: false, forceLoad: true);
string html = parser.Parse(markdown);
return html;
}
}
}
1 change: 1 addition & 0 deletions Tests/MarkdownMonster.Test/MarkdownMonster.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<Compile Include="ClipBoardHelperTests.cs" />
<Compile Include="ComputerInfoTests.cs" />
<Compile Include="FolderStructureTests.cs" />
<Compile Include="FrontMatterTests.cs" />
<Compile Include="HtmlToPdfGenerationTests.cs" />
<Compile Include="MarkdownDocumentTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
6 changes: 2 additions & 4 deletions Tests/WeblogAddin.Test/WeblogMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public void SetPostYaml()
[TestMethod]
public void YamlExtractionRegExTest()
{
Regex YamlExtractionRegex = new Regex("^---\n.*?^---\n", RegexOptions.Multiline | RegexOptions.Singleline);
string markdown = STR_postWithMetaData;

var match = YamlExtractionRegex.Match(markdown);
var match = MarkdownUtilities.YamlExtractionRegex.Match(markdown);

Assert.IsTrue(match.Success);
}
Expand Down Expand Up @@ -130,8 +129,7 @@ WeblogPostMetadata GetMetaData()
};
}

public static string STR_postWithMetaData = @"
---
public static string STR_postWithMetaData = @"---
title: 'Tip: Create a Visual Studio Menu option to Open a Command Window'
abstract: 'This is the abstract for this blog post... '
keywords: Menu,External Tools, Command Line
Expand Down

0 comments on commit cf1440e

Please sign in to comment.