Skip to content

Commit

Permalink
support gfm style html comment.
Browse files Browse the repository at this point in the history
  • Loading branch information
vwxyzh committed Apr 20, 2016
1 parent d1253da commit 9587a0b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public virtual IMarkdownToken TryMatch(IMarkdownParser engine, ref string source
}
source = source.Substring(match.Length);

return new MarkdownTextToken(this, engine.Context, match.Value, match.Value);
return new MarkdownRawToken(this, engine.Context, match.Value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.DocAsCode.MarkdownLite
{
using System.Collections.Immutable;
using System.Text.RegularExpressions;

public class GfmHtmlCommentBlockRule : IMarkdownRule
{
public virtual string Name => "GfmHtmlComment";

public virtual Regex HtmlComment => Regexes.Block.Gfm.HtmlComment;

public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source)
{
var match = HtmlComment.Match(source);
if (match.Length == 0)
{
return null;
}
source = source.Substring(match.Length);
return new MarkdownHtmlBlockToken(
this,
parser.Context,
new InlineContent(
ImmutableArray.Create<IMarkdownToken>(
new MarkdownRawToken(
this,
parser.Context,
match.Value))),
match.Value);
}
}
}
5 changes: 3 additions & 2 deletions src/Microsoft.DocAsCode.MarkdownLite/GfmEngineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected virtual void BuildRules()

protected virtual void BuildBlockRules()
{
var builder = ImmutableList<IMarkdownRule>.Empty.ToBuilder();
var builder = ImmutableList.CreateBuilder<IMarkdownRule>();
builder.Add(new MarkdownNewLineBlockRule());
builder.Add(new MarkdownCodeBlockRule());
builder.Add(new GfmFencesBlockRule());
Expand All @@ -31,6 +31,7 @@ protected virtual void BuildBlockRules()
builder.Add(new MarkdownHrBlockRule());
builder.Add(new MarkdownBlockquoteBlockRule());
builder.Add(new MarkdownListBlockRule());
builder.Add(new GfmHtmlCommentBlockRule());
builder.Add(new MarkdownHtmlBlockRule());
builder.Add(new MarkdownDefBlockRule());
builder.Add(new MarkdownTableBlockRule());
Expand All @@ -41,7 +42,7 @@ protected virtual void BuildBlockRules()

protected virtual void BuildInlineRules()
{
var builder = ImmutableList<IMarkdownRule>.Empty.ToBuilder();
var builder = ImmutableList.CreateBuilder<IMarkdownRule>();
builder.Add(new GfmEscapeInlineRule());
builder.Add(new MarkdownCommentInlineRule());
builder.Add(new MarkdownAutoLinkInlineRule());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private void BuildRulesByOptions()

private void BuildBlockRulesByOptions()
{
var builder = ImmutableList<IMarkdownRule>.Empty.ToBuilder();
var builder = ImmutableList.CreateBuilder<IMarkdownRule>();
builder.Add(new MarkdownNewLineBlockRule());
builder.Add(new MarkdownCodeBlockRule());
if (Options.Gfm)
Expand All @@ -42,6 +42,10 @@ private void BuildBlockRulesByOptions()
builder.Add(new MarkdownHrBlockRule());
builder.Add(new MarkdownBlockquoteBlockRule());
builder.Add(new MarkdownListBlockRule());
if (Options.Gfm)
{
builder.Add(new GfmHtmlCommentBlockRule());
}
builder.Add(new MarkdownHtmlBlockRule());
builder.Add(new MarkdownDefBlockRule());
if (Options.Tables)
Expand All @@ -62,51 +66,51 @@ private void BuildBlockRulesByOptions()

private void BuildInlineRulesByOptions()
{
var irb = ImmutableList<IMarkdownRule>.Empty.ToBuilder();
var builder = ImmutableList.CreateBuilder<IMarkdownRule>();
if (Options.Gfm)
{
irb.Add(new GfmEscapeInlineRule());
builder.Add(new GfmEscapeInlineRule());
}
else
{
irb.Add(new MarkdownEscapeInlineRule());
builder.Add(new MarkdownEscapeInlineRule());
}
irb.Add(new MarkdownAutoLinkInlineRule());
builder.Add(new MarkdownAutoLinkInlineRule());
if (Options.Gfm)
{
irb.Add(new GfmUrlInlineRule());
builder.Add(new GfmUrlInlineRule());
}
irb.Add(new MarkdownCodeElementInlineRule());
irb.Add(new MarkdownTagInlineRule());
irb.Add(new MarkdownLinkInlineRule());
irb.Add(new MarkdownRefLinkInlineRule());
irb.Add(new MarkdownNoLinkInlineRule());
builder.Add(new MarkdownCodeElementInlineRule());
builder.Add(new MarkdownTagInlineRule());
builder.Add(new MarkdownLinkInlineRule());
builder.Add(new MarkdownRefLinkInlineRule());
builder.Add(new MarkdownNoLinkInlineRule());
if (Options.Gfm)
{
irb.Add(new GfmStrongEmInlineRule());
irb.Add(new GfmStrongInlineRule());
irb.Add(new GfmEmInlineRule());
builder.Add(new GfmStrongEmInlineRule());
builder.Add(new GfmStrongInlineRule());
builder.Add(new GfmEmInlineRule());
}
else
{
irb.Add(new MarkdownStrongInlineRule());
irb.Add(new MarkdownEmInlineRule());
builder.Add(new MarkdownStrongInlineRule());
builder.Add(new MarkdownEmInlineRule());
}
irb.Add(new MarkdownCodeInlineRule());
irb.Add(new MarkdownBrInlineRule());
builder.Add(new MarkdownCodeInlineRule());
builder.Add(new MarkdownBrInlineRule());
if (Options.Gfm)
{
irb.Add(new GfmDelInlineRule());
irb.Add(new MarkdownEscapedTextInlineRule());
irb.Add(new GfmTextInlineRule());
builder.Add(new GfmDelInlineRule());
builder.Add(new MarkdownEscapedTextInlineRule());
builder.Add(new GfmTextInlineRule());
}
else
{
irb.Add(new MarkdownEscapedTextInlineRule());
irb.Add(new MarkdownTextInlineRule());
builder.Add(new MarkdownEscapedTextInlineRule());
builder.Add(new MarkdownTextInlineRule());
}

InlineRules = irb.ToImmutable();
InlineRules = builder.ToImmutable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Align.cs" />
<Compile Include="Gfm\GfmHtmlCommentBlockRule.cs" />
<Compile Include="Basic\BlockTokens\TwoPhaseBlockToken.cs" />
<Compile Include="Basic\InlineRules\MarkdownCommentInlineRule.cs" />
<Compile Include="Basic\InlineRules\MarkdownEscapedTextInlineRule.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.DocAsCode.MarkdownLite/Regexes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static class Gfm
public static readonly Regex Fences = new Regex(@"^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)", RegexOptionCompiled);
public static readonly Regex Paragraph = new Regex(@"^((?:[^\n]+\n?(?! *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\2 *(?:\n+|$)|( *)((?:[*+-]|\d+\.)) [\s\S]+?(?:\n+(?=\5?(?:[-*_] *){3,}(?:\n+|$))|\n+(?= *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +[""(]([^\n]+)["")])? *(?:\n+|$))|\n{2,}(?! )(?!\5(?:[*+-]|\d+\.) )\n*|\s*$)|( *[-*_]){3,} *(?:\n+|$)| *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)|([^\n]+)\n *(=|-){2,} *(?:\n+|$)|( *>[^\n]+(\n(?! *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +[""(]([^\n]+)["")])? *(?:\n+|$))[^\n]+)*\n*)+|<(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\b)\w+(?!:\/|[^\w\s@]*@)\b| *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +[""(]([^\n]+)["")])? *(?:\n+|$)))+)\n*", RegexOptionCompiled);
public static readonly Regex Heading = new Regex(@"^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)", RegexOptionCompiled);
public static readonly Regex HtmlComment = new Regex(@"^(?:<!--[\s\S]*?-->) *(?:\n|$)", RegexOptionCompiled);
}

public static class Tables
Expand Down
6 changes: 6 additions & 0 deletions test/Microsoft.DocAsCode.MarkdownLite.Tests/GfmTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ 2. D
<li>C</li>
<li>D</li>
</ol>
")]
[InlineData(
@"<!--aaa-->
aaa",
@"<!--aaa-->
<p>aaa</p>
")]
#endregion
public void TestGfmInGeneral(string source, string expected)
Expand Down

0 comments on commit 9587a0b

Please sign in to comment.