Skip to content

Commit

Permalink
Fix dotnet#224: make sure unique html id
Browse files Browse the repository at this point in the history
  • Loading branch information
qinezh committed May 6, 2016
1 parent 4bf02f7 commit e85d0c8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public virtual IMarkdownToken TryMatch(IMarkdownParser parser, ref string source
t.Rule,
t.Context,
p.TokenizeInline(match.Groups[2].Value),
Regex.Replace(match.Groups[2].Value.ToLower(), @"[^\w]+", "-"),
Regex.Replace(match.Groups[2].Value.ToLower(), @"[^\p{L}\p{Nd}]+", "-"),
match.Groups[1].Value.Length,
t.RawMarkdown));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Microsoft.DocAsCode.MarkdownLite
{
using System.Collections.Generic;

public class MarkdownHeadingBlockToken : IMarkdownToken, IMarkdownRewritable<MarkdownHeadingBlockToken>
{
public MarkdownHeadingBlockToken(IMarkdownRule rule, IMarkdownContext context, InlineContent content, string id, int depth, string rawMarkdown)
Expand Down Expand Up @@ -36,5 +38,17 @@ public MarkdownHeadingBlockToken Rewrite(IMarkdownRewriteEngine rewriterEngine)
}
return new MarkdownHeadingBlockToken(Rule, Context, c, Id, Depth, RawMarkdown);
}

public MarkdownHeadingBlockToken RewriteId(Dictionary<string, int> idTable)
{
if (idTable.ContainsKey(Id))
{
var newId = string.Concat(Id, "-",idTable[Id]);
idTable[Id]++;
return new MarkdownHeadingBlockToken(Rule, Context, Content, newId, Depth, RawMarkdown);
}
idTable[Id] = 0;
return null;
}
}
}
6 changes: 6 additions & 0 deletions src/Microsoft.DocAsCode.MarkdownLite/MarkdownEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public StringBuffer Mark(string markdown, IMarkdownContext context)
(e, t) => t.Extract(parser)),
MaxExtractCount + 1));
tokens = internalRewriteEngine.Rewrite(tokens);
var idTable = new Dictionary<string, int>();
var idRewriteEngine = new MarkdownRewriteEngine(
this,
MarkdownTokenRewriterFactory.FromLambda<IMarkdownRewriteEngine, MarkdownHeadingBlockToken>(
(e, t) => t.RewriteId(idTable)));
tokens = idRewriteEngine.Rewrite(tokens);
tokens = RewriteEngine.Rewrite(tokens);
var renderer = Renderer;
foreach (var token in tokens)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class DocfxFlavoredMarkdownTest
[InlineData(
@"[*a*](xref:uid)",
"<p><a href=\"xref:uid\"><em>a</em></a></p>\n")]
[InlineData("# Hello World\n# Hello World", "<h1 id=\"hello-world\">Hello World</h1>\n<h1 id=\"hello-world-0\">Hello World</h1>\n")]
[InlineData("# 微软。上海\n# 微软。上海", "<h1 id=\"微软-上海\">微软。上海</h1>\n<h1 id=\"微软-上海-0\">微软。上海</h1>\n")]
public void TestDfmInGeneral(string source, string expected)
{
Assert.Equal(expected.Replace("\r\n", "\n"), DocfxFlavoredMarked.Markup(source));
Expand Down

0 comments on commit e85d0c8

Please sign in to comment.