From 8e0e33eb7bb4c67d67697196dc2f93ed7e5fb13d Mon Sep 17 00:00:00 2001 From: Yufei Huang Date: Tue, 16 Mar 2021 13:53:56 +0800 Subject: [PATCH] Remove tab group hash function --- docs/specs/markdown.yml | 12 +++--- .../Aggregator/TabGroupAggregator.cs | 17 +------- .../TabGroup/HtmlTabGroupBlockRenderer.cs | 2 +- .../TabGroup/TabGroupBlock.cs | 7 ++-- .../TabGroup/TabGroupIdRewriter.cs | 20 +-------- .../GeneralTest.cs | 42 +++++++++---------- .../TabGroupTest.cs | 39 ++++++++--------- .../MarkdownElementTelemetryExtensionTest.cs | 2 +- 8 files changed, 52 insertions(+), 89 deletions(-) diff --git a/docs/specs/markdown.yml b/docs/specs/markdown.yml index bddfd5cdb68..d0cdda093be 100644 --- a/docs/specs/markdown.yml +++ b/docs/specs/markdown.yml @@ -467,19 +467,19 @@ outputs: docs/a.json: | { "conceptual": " -
+
-
+

a

-
" @@ -849,4 +849,4 @@ inputs: outputs: docs/a.json: | - {"conceptual": "\n"} \ No newline at end of file + {"conceptual": "\n"} diff --git a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Aggregator/TabGroupAggregator.cs b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Aggregator/TabGroupAggregator.cs index 4735b1e714f..2251c55497a 100644 --- a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Aggregator/TabGroupAggregator.cs +++ b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/Aggregator/TabGroupAggregator.cs @@ -69,22 +69,7 @@ private static void AggregateCore( int startSpan, int offset) { - var groupId = GetHashString(items[0]?.Content?.ToString() ?? "").Replace("/", "-").Remove(10); - - context.AggregateTo( - new TabGroupBlock( - groupId, - items.ToImmutableArray(), - startLine, - startSpan, - 0), - offset); - } - - private static string GetHashString(string content) - { - using var sha256 = SHA256.Create(); - return Convert.ToBase64String(sha256.ComputeHash(Encoding.UTF8.GetBytes(content))); + context.AggregateTo(new TabGroupBlock(items.ToImmutableArray(), startLine, startSpan, 0), offset); } private static TabItemBlock CreateTabItem( diff --git a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/HtmlTabGroupBlockRenderer.cs b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/HtmlTabGroupBlockRenderer.cs index 6771a9cb8cf..582ca69915d 100644 --- a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/HtmlTabGroupBlockRenderer.cs +++ b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/HtmlTabGroupBlockRenderer.cs @@ -11,7 +11,7 @@ public class HtmlTabGroupBlockRenderer : HtmlObjectRenderer protected override void Write(HtmlRenderer renderer, TabGroupBlock block) { renderer.Write(@"
Items { get; set; } - public TabGroupBlock(string id, ImmutableArray blocks, int startLine, int startSpan, int activeTabIndex) + public TabGroupBlock(ImmutableArray blocks, int startLine, int startSpan, int activeTabIndex) : base(null) { - Id = id; ActiveTabIndex = activeTabIndex; Items = blocks; Line = startLine; @@ -33,4 +32,4 @@ public TabGroupBlock(string id, ImmutableArray blocks, int startLi Span = new SourceSpan(startSpan, blocks[length - 1].Content.Span.End); } } -} \ No newline at end of file +} diff --git a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/TabGroupIdRewriter.cs b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/TabGroupIdRewriter.cs index de1b4abc69c..3ca99ab0c36 100644 --- a/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/TabGroupIdRewriter.cs +++ b/src/Microsoft.DocAsCode.MarkdigEngine.Extensions/TabGroup/TabGroupIdRewriter.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Collections.Generic; using Markdig.Syntax; using Microsoft.DocAsCode.MarkdigEngine.Extensions; @@ -9,7 +8,7 @@ namespace Microsoft.DocAsCode.MarkdigEngine { public class TabGroupIdRewriter : IMarkdownObjectRewriter { - private readonly Dictionary _dict = new(); + private int _id; public void PostProcess(IMarkdownObject markdownObject) { @@ -23,22 +22,7 @@ public IMarkdownObject Rewrite(IMarkdownObject markdownObject) { if (markdownObject is TabGroupBlock block) { - var groupId = block.Id; - while (true) - { - if (_dict.TryGetValue(groupId, out var index)) - { - _dict[groupId] += 1; - groupId = $"{groupId}-{index}"; - block.Id = groupId; - return block; - } - else - { - _dict.Add(groupId, 1); - return markdownObject; - } - } + block.Id = ++_id; } return markdownObject; diff --git a/test/Microsoft.DocAsCode.MarkdigEngine.Extensions.Tests/GeneralTest.cs b/test/Microsoft.DocAsCode.MarkdigEngine.Extensions.Tests/GeneralTest.cs index f24cc183240..2daf945304b 100644 --- a/test/Microsoft.DocAsCode.MarkdigEngine.Extensions.Tests/GeneralTest.cs +++ b/test/Microsoft.DocAsCode.MarkdigEngine.Extensions.Tests/GeneralTest.cs @@ -91,7 +91,7 @@ public void TestDfmImageLink_WithSpecialCharactersInAltText() [InlineData("Hot keys: Ctrl+[ and Ctrl+]", "

Hot keys: Ctrl+[ and Ctrl+]

\n")] [InlineData("
Some text here
", "
Some text here
\n")] [InlineData( - @"# Hello @CrossLink1 @'CrossLink2'dummy + @"# Hello @CrossLink1 @'CrossLink2'dummy @World", "

Hello dummy

\n

\n")] [InlineData( @@ -195,20 +195,19 @@ public void TestTabGroup() # [title-b](#tab/b/c) content-b - - -"; - var groupId = "bHGHmlrG6S"; - var expected = $@"
+ var expected = $@"
-
+

content-a

-
@@ -230,37 +229,36 @@ public void TestTabGroup_2() # [title-b](#tab/b/a) content-b - - -"; - var groupId = "bHGHmlrG6S"; - var expected = $@"
+ var expected = $@"
-
+

content-a

-
-
+
-
+

content-a

-