Skip to content

Commit

Permalink
chore: Remove options from MarkdigExtensionSetting (#9830)
Browse files Browse the repository at this point in the history
remove options, update emoji to use enum
  • Loading branch information
yufeih authored Apr 1, 2024
1 parent 3be044e commit c0cc51e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 111 deletions.
47 changes: 3 additions & 44 deletions src/Docfx.MarkdigEngine.Extensions/MarkdigExtensionSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MarkdigExtensionSetting
/// <summary>
/// Initializes a new instance of the <see cref="MarkdigExtensionSetting"/> class.
/// </summary>
public MarkdigExtensionSetting(string name, JsonObject? options = null)
public MarkdigExtensionSetting(string name, JsonNode? options = null)
{
Name = name;
if (options != null)
Expand Down Expand Up @@ -58,49 +58,8 @@ public MarkdigExtensionSetting(string name, JsonObject? options = null)
/// </summary>
public T GetOptions<T>(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<T>(DefaultSerializerOptions)!;
}
else
{
return fallbackValue;
}
}

/// <summary>
/// Gets markdig extension options as specified class object.
/// </summary>
public T GetOptionsValue<T>(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<T>()!;
}
else
{
return fallbackValue;
}
return Options is null ? fallbackValue
: JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(Options), DefaultSerializerOptions) ?? fallbackValue;
}

/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/Docfx.MarkdigEngine.Extensions/MarkdownExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string, string> notes = null, PlantUmlOptions plantUml = null)
Expand Down Expand Up @@ -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<EmojiExtension>(new EmojiExtension(emojiMapping));
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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")
]);
}

Expand All @@ -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")
]);
}

Expand All @@ -61,10 +51,7 @@ public void AutoIdentifierTest_None()
var expected = @"<h1 id=""this-is-a-heading_with.and"">This - is a &amp;@! heading _ with . and ! -</h1>";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("AutoIdentifiers", new JsonObject
{
["options"] = "None",
})
new("AutoIdentifiers", "None")
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,10 +40,7 @@ public void AutoLinkTest_Custom()
var expected = @"<p>Sample URL (<a href=""http://www.google.com"" target=""_blank"">http://www.google.com</a>)</p>";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("AutoLinks", new JsonObject
{
["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions),
})
new("AutoLinks", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions))
]);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -41,10 +40,7 @@ public void EmojiTest_Smileys_Enabled()
var expected = @"<p>😃</p>";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("Emojis", new JsonObject
{
["options"] = true
}),
new("Emojis", "DefaultAndSmileys"),
]);
}

Expand All @@ -55,10 +51,7 @@ public void EmojiTest_Smileys_Disabled()
var expected = @"<p>:)</p>";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("Emojis", new JsonObject
{
["options"] = false,
}),
new("Emojis", "Default"),
]);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -63,10 +62,7 @@ public void EmphasisExtraTest_SuperscriptAndSubscript()
{
var expected = @"<p>H<sub>2</sub>O is a liquid. 2<sup>10</sup> is 1024</p>";
TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("EmphasisExtras", new JsonObject
{
["options"] = "Superscript, Subscript",
})]);
new("EmphasisExtras", "Superscript, Subscript")]);
}
}

Expand All @@ -84,10 +80,7 @@ public void EmphasisExtraTest_Inserted()
{
var expected = @"<p><ins>Inserted text</ins></p>";
TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("EmphasisExtras", new JsonObject
{
["options"] = "Inserted",
})]);
new("EmphasisExtras", "Inserted")]);
}
}

Expand All @@ -105,10 +98,7 @@ public void EmphasisExtraTest_Marked()
{
var expected = @"<p><mark>Marked text</mark></p>";
TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("EmphasisExtras", new JsonObject
{
["options"] = "Marked",
})]);
new("EmphasisExtras", "Marked")]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,10 +43,7 @@ public void MediaLinksTest_Custom()
var expected = $"""<p><video class="{options.Class}" width="{options.Width}" height="{options.Height}"><source type="video/mp4" src="https://example.com/video.mp4"></source></video></p>""";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("MediaLinks", new JsonObject
{
["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions),
})
new("MediaLinks", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions))
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,10 +50,7 @@ public void SmartyPantsTest_Custom()
string expected = "<p>This is a <<text with>> a another text'</p>";

TestUtility.VerifyMarkup(content, expected, optionalExtensions: [
new("SmartyPants", new JsonObject
{
["options"] = JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions),
})
new("SmartyPants", JsonSerializer.SerializeToNode(options, MarkdigExtensionSettingConverter.DefaultSerializerOptions))
]);
}
}
3 changes: 1 addition & 2 deletions test/docfx.Tests/Api.verified.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T fallbackValue) { }
public T GetOptionsValue<T>(string key, T fallbackValue) { }
public static Docfx.MarkdigEngine.Extensions.MarkdigExtensionSetting op_Implicit(string name) { }
}
public class MarkdownContext
Expand Down

0 comments on commit c0cc51e

Please sign in to comment.