From e85d0c837d3bf1d2529b6714d2119f4a5cda3ad0 Mon Sep 17 00:00:00 2001 From: Qinen Zhu Date: Fri, 6 May 2016 19:16:48 +0800 Subject: [PATCH] Fix #224: make sure unique html id --- .../Basic/BlockRules/MarkdownHeadingBlockRule.cs | 2 +- .../Basic/BlockTokens/MarkdownHeadingBlockToken.cs | 14 ++++++++++++++ .../MarkdownEngine.cs | 6 ++++++ .../DocfxFlavoredMarkdownTest.cs | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockRules/MarkdownHeadingBlockRule.cs b/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockRules/MarkdownHeadingBlockRule.cs index ded212786b5..117a3097b5b 100644 --- a/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockRules/MarkdownHeadingBlockRule.cs +++ b/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockRules/MarkdownHeadingBlockRule.cs @@ -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)); } diff --git a/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockTokens/MarkdownHeadingBlockToken.cs b/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockTokens/MarkdownHeadingBlockToken.cs index 26fd31219ec..810b6be9950 100644 --- a/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockTokens/MarkdownHeadingBlockToken.cs +++ b/src/Microsoft.DocAsCode.MarkdownLite/Basic/BlockTokens/MarkdownHeadingBlockToken.cs @@ -3,6 +3,8 @@ namespace Microsoft.DocAsCode.MarkdownLite { + using System.Collections.Generic; + public class MarkdownHeadingBlockToken : IMarkdownToken, IMarkdownRewritable { public MarkdownHeadingBlockToken(IMarkdownRule rule, IMarkdownContext context, InlineContent content, string id, int depth, string rawMarkdown) @@ -36,5 +38,17 @@ public MarkdownHeadingBlockToken Rewrite(IMarkdownRewriteEngine rewriterEngine) } return new MarkdownHeadingBlockToken(Rule, Context, c, Id, Depth, RawMarkdown); } + + public MarkdownHeadingBlockToken RewriteId(Dictionary 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; + } } } diff --git a/src/Microsoft.DocAsCode.MarkdownLite/MarkdownEngine.cs b/src/Microsoft.DocAsCode.MarkdownLite/MarkdownEngine.cs index 203363b0f38..6e337fc00eb 100644 --- a/src/Microsoft.DocAsCode.MarkdownLite/MarkdownEngine.cs +++ b/src/Microsoft.DocAsCode.MarkdownLite/MarkdownEngine.cs @@ -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(); + var idRewriteEngine = new MarkdownRewriteEngine( + this, + MarkdownTokenRewriterFactory.FromLambda( + (e, t) => t.RewriteId(idTable))); + tokens = idRewriteEngine.Rewrite(tokens); tokens = RewriteEngine.Rewrite(tokens); var renderer = Renderer; foreach (var token in tokens) diff --git a/test/Microsoft.DocAsCode.Dfm.Tests/DocfxFlavoredMarkdownTest.cs b/test/Microsoft.DocAsCode.Dfm.Tests/DocfxFlavoredMarkdownTest.cs index 90de4e077d7..ffe9f4b854a 100644 --- a/test/Microsoft.DocAsCode.Dfm.Tests/DocfxFlavoredMarkdownTest.cs +++ b/test/Microsoft.DocAsCode.Dfm.Tests/DocfxFlavoredMarkdownTest.cs @@ -54,6 +54,8 @@ public class DocfxFlavoredMarkdownTest [InlineData( @"[*a*](xref:uid)", "

a

\n")] + [InlineData("# Hello World\n# Hello World", "

Hello World

\n

Hello World

\n")] + [InlineData("# 微软。上海\n# 微软。上海", "

微软。上海

\n

微软。上海

\n")] public void TestDfmInGeneral(string source, string expected) { Assert.Equal(expected.Replace("\r\n", "\n"), DocfxFlavoredMarked.Markup(source));