From c0cc51e21f05ac3351a1894c7a18b4538e157fa0 Mon Sep 17 00:00:00 2001 From: Yufei Huang Date: Mon, 1 Apr 2024 15:35:44 +0800 Subject: [PATCH] chore: Remove options from MarkdigExtensionSetting (#9830) remove options, update emoji to use enum --- .../MarkdigExtensionSetting.cs | 47 ++----------------- .../MarkdownExtensions.cs | 12 +++-- .../AutoIdentifierTest.cs | 21 ++------- .../AutoLinkTest.cs | 6 +-- .../MarkdigBuiltinExtensionTests/EmojiTest.cs | 11 +---- .../EmphasisExtraTest.cs | 16 ++----- .../MediaLinksTest.cs | 6 +-- .../PipeTableTest.cs | 6 +-- .../SmartyPantsTest.cs | 7 +-- test/docfx.Tests/Api.verified.cs | 3 +- 10 files changed, 24 insertions(+), 111 deletions(-) diff --git a/src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSetting.cs b/src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSetting.cs index bfe3ca65797..2e7ffb317b2 100644 --- a/src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSetting.cs +++ b/src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSetting.cs @@ -29,7 +29,7 @@ public class MarkdigExtensionSetting /// /// Initializes a new instance of the class. /// - public MarkdigExtensionSetting(string name, JsonObject? options = null) + public MarkdigExtensionSetting(string name, JsonNode? options = null) { Name = name; if (options != null) @@ -58,49 +58,8 @@ public MarkdigExtensionSetting(string name, JsonObject? options = null) /// public T GetOptions(T fallbackValue) { - if (Options == null) - { - return fallbackValue; - } - - var jsonObject = JsonSerializer.SerializeToNode(Options)?.AsObject(); - - if (jsonObject != null - && jsonObject.TryGetPropertyValue("options", out var optionsNode) - && optionsNode != null) - { - return optionsNode.Deserialize(DefaultSerializerOptions)!; - } - else - { - return fallbackValue; - } - } - - /// - /// Gets markdig extension options as specified class object. - /// - public T GetOptionsValue(string key, T fallbackValue) - { - if (Options == null) - { - return fallbackValue; - } - - var jsonNode = JsonSerializer.SerializeToNode(Options)?.AsObject(); - - // Try to read options property that have specified key. - if (jsonNode != null - && jsonNode.TryGetPropertyValue("options", out var optionsNode) - && optionsNode != null - && optionsNode.AsObject().TryGetPropertyValue(key, out var valueNode)) - { - return valueNode!.GetValue()!; - } - else - { - return fallbackValue; - } + return Options is null ? fallbackValue + : JsonSerializer.Deserialize(JsonSerializer.Serialize(Options), DefaultSerializerOptions) ?? fallbackValue; } /// diff --git a/src/Docfx.MarkdigEngine.Extensions/MarkdownExtensions.cs b/src/Docfx.MarkdigEngine.Extensions/MarkdownExtensions.cs index 46c4d387f99..253405982cb 100644 --- a/src/Docfx.MarkdigEngine.Extensions/MarkdownExtensions.cs +++ b/src/Docfx.MarkdigEngine.Extensions/MarkdownExtensions.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using System.Text.Json; using Markdig; using Markdig.Extensions.AutoIdentifiers; using Markdig.Extensions.AutoLinks; @@ -18,6 +17,8 @@ namespace Docfx.MarkdigEngine.Extensions; public static class MarkdownExtensions { + enum EmojiMappingOption { Default, DefaultAndSmileys } + public static MarkdownPipelineBuilder UseDocfxExtensions( this MarkdownPipelineBuilder pipeline, MarkdownContext context, Dictionary notes = null, PlantUmlOptions plantUml = null) @@ -121,10 +122,11 @@ private static bool TryAddOrReplaceMarkdigExtension( // EmojiExtension (Docfx default: enableSmileys: false) case "emojis": { - var enableSmileys = extension.GetOptions(fallbackValue: true); - EmojiMapping emojiMapping = enableSmileys - ? EmojiMapping.DefaultEmojisAndSmileysMapping - : EmojiMapping.DefaultEmojisOnlyMapping; + var emojiMapping = extension.GetOptions(fallbackValue: EmojiMappingOption.DefaultAndSmileys) switch + { + EmojiMappingOption.DefaultAndSmileys => EmojiMapping.DefaultEmojisAndSmileysMapping, + _ => EmojiMapping.DefaultEmojisOnlyMapping, + }; pipeline.Extensions.ReplaceOrAdd(new EmojiExtension(emojiMapping)); return true; } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoIdentifierTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoIdentifierTest.cs index 31501ad3a44..0d925490e1d 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoIdentifierTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoIdentifierTest.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Text.Json.Nodes; using Markdig.Extensions.AutoIdentifiers; using Xunit; @@ -23,10 +22,7 @@ public void AutoIdentifierTest_DocfxDefault() TestUtility.VerifyMarkup(content, expected); TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("AutoIdentifiers", new JsonObject - { - ["options"] = "GitHub", - }) + new("AutoIdentifiers", "GitHub") ]); } @@ -40,17 +36,11 @@ public void AutoIdentifierTest_MarkdigDefault() TestUtility.VerifyMarkup(content, expected, optionalExtensions: ["AutoIdentifiers"]); TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("AutoIdentifiers", new JsonObject - { - ["options"] = "Default", - }) + new("AutoIdentifiers", "Default") ]); TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("AutoIdentifiers", new JsonObject - { - ["options"] = "AutoLink, AllowOnlyAscii", // Same as Default option. - }) + new("AutoIdentifiers", "AutoLink, AllowOnlyAscii") ]); } @@ -61,10 +51,7 @@ public void AutoIdentifierTest_None() var expected = @"

This - is a &@! heading _ with . and ! -

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("AutoIdentifiers", new JsonObject - { - ["options"] = "None", - }) + new("AutoIdentifiers", "None") ]); } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoLinkTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoLinkTest.cs index 0af18b2a842..d5097cd427e 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoLinkTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/AutoLinkTest.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Text.Json; -using System.Text.Json.Nodes; using Docfx.MarkdigEngine.Extensions; using Markdig.Extensions.AutoLinks; using Xunit; @@ -41,10 +40,7 @@ public void AutoLinkTest_Custom() var expected = @"

Sample URL (http://www.google.com)

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("AutoLinks", new JsonObject - { - ["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions), - }) + new("AutoLinks", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions)) ]); } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmojiTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmojiTest.cs index 722028c7895..b553c050151 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmojiTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmojiTest.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Text.Json.Nodes; using Markdig.Extensions.Emoji; using Xunit; @@ -41,10 +40,7 @@ public void EmojiTest_Smileys_Enabled() var expected = @"

😃

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("Emojis", new JsonObject - { - ["options"] = true - }), + new("Emojis", "DefaultAndSmileys"), ]); } @@ -55,10 +51,7 @@ public void EmojiTest_Smileys_Disabled() var expected = @"

:)

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("Emojis", new JsonObject - { - ["options"] = false, - }), + new("Emojis", "Default"), ]); } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmphasisExtraTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmphasisExtraTest.cs index 60a28684fd5..357895b70c3 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmphasisExtraTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/EmphasisExtraTest.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Text.Json.Nodes; using Markdig.Extensions.EmphasisExtras; using Xunit; @@ -63,10 +62,7 @@ public void EmphasisExtraTest_SuperscriptAndSubscript() { var expected = @"

H2O is a liquid. 210 is 1024

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("EmphasisExtras", new JsonObject - { - ["options"] = "Superscript, Subscript", - })]); + new("EmphasisExtras", "Superscript, Subscript")]); } } @@ -84,10 +80,7 @@ public void EmphasisExtraTest_Inserted() { var expected = @"

Inserted text

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("EmphasisExtras", new JsonObject - { - ["options"] = "Inserted", - })]); + new("EmphasisExtras", "Inserted")]); } } @@ -105,10 +98,7 @@ public void EmphasisExtraTest_Marked() { var expected = @"

Marked text

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("EmphasisExtras", new JsonObject - { - ["options"] = "Marked", - })]); + new("EmphasisExtras", "Marked")]); } } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/MediaLinksTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/MediaLinksTest.cs index 5e2a66c84d1..e30d185a7d5 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/MediaLinksTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/MediaLinksTest.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Text.Json; -using System.Text.Json.Nodes; using Docfx.MarkdigEngine.Extensions; using Markdig.Extensions.MediaLinks; using Xunit; @@ -44,10 +43,7 @@ public void MediaLinksTest_Custom() var expected = $"""

"""; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("MediaLinks", new JsonObject - { - ["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions), - }) + new("MediaLinks", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions)) ]); } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/PipeTableTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/PipeTableTest.cs index 6752955efd0..e8a254da131 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/PipeTableTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/PipeTableTest.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Text.Json; -using System.Text.Json.Nodes; using Docfx.MarkdigEngine.Extensions; using Markdig.Extensions.Tables; using Xunit; @@ -86,10 +85,7 @@ public void PipeTableTest_Custom() TestUtility.VerifyMarkup(content, expected, optionalExtensions: ["gfm-pipetables"]); TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new ("PipeTables", new JsonObject - { - ["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions), - }) + new ("PipeTables", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions)) ]); } } diff --git a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/SmartyPantsTest.cs b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/SmartyPantsTest.cs index 0f637be3954..b69ff223289 100644 --- a/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/SmartyPantsTest.cs +++ b/test/Docfx.MarkdigEngine.Extensions.Tests/MarkdigBuiltinExtensionTests/SmartyPantsTest.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; using Docfx.MarkdigEngine.Extensions; using Markdig.Extensions.SmartyPants; using Xunit; @@ -52,10 +50,7 @@ public void SmartyPantsTest_Custom() string expected = "

This is a <> a another text'

"; TestUtility.VerifyMarkup(content, expected, optionalExtensions: [ - new("SmartyPants", new JsonObject - { - ["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions), - }) + new("SmartyPants", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions)) ]); } } diff --git a/test/docfx.Tests/Api.verified.cs b/test/docfx.Tests/Api.verified.cs index d38b2d6e963..a18ce4c6167 100644 --- a/test/docfx.Tests/Api.verified.cs +++ b/test/docfx.Tests/Api.verified.cs @@ -3572,11 +3572,10 @@ public void Setup(Markdig.MarkdownPipeline pipeline, Markdig.Renderers.IMarkdown [System.Diagnostics.DebuggerDisplay("Name = {Name}")] public class MarkdigExtensionSetting { - public MarkdigExtensionSetting(string name, System.Text.Json.Nodes.JsonObject? options = null) { } + public MarkdigExtensionSetting(string name, System.Text.Json.Nodes.JsonNode? options = null) { } public string Name { get; init; } public System.Text.Json.JsonElement? Options { get; init; } public T GetOptions(T fallbackValue) { } - public T GetOptionsValue(string key, T fallbackValue) { } public static Docfx.MarkdigEngine.Extensions.MarkdigExtensionSetting op_Implicit(string name) { } } public class MarkdownContext