-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
372 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
examples/Demo/Shared/Components/MarkdownSectionPreCodeExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Markdig.Renderers; | ||
using Markdig; | ||
using Markdig.Renderers.Html; | ||
|
||
namespace FluentUI.Demo.Shared.Components; | ||
|
||
internal class MarkdownSectionPreCodeExtension : IMarkdownExtension | ||
{ | ||
public void Setup(MarkdownPipelineBuilder pipeline) | ||
{ | ||
} | ||
|
||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) | ||
{ | ||
var htmlRenderer = renderer as TextRendererBase<HtmlRenderer>; | ||
if (htmlRenderer == null) | ||
{ | ||
return; | ||
} | ||
|
||
var originalCodeBlockRenderer = htmlRenderer.ObjectRenderers.FindExact<CodeBlockRenderer>(); | ||
if (originalCodeBlockRenderer != null) | ||
{ | ||
htmlRenderer.ObjectRenderers.Remove(originalCodeBlockRenderer); | ||
} | ||
|
||
htmlRenderer.ObjectRenderers.AddIfNotAlready(new MarkdownSectionPreCodeRenderer( | ||
new MarkdownSectionPreCodeRendererOptions | ||
{ | ||
PreTagAttributes = "{.snippet .hljs-copy-wrapper}", | ||
}) | ||
); | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
examples/Demo/Shared/Components/MarkdownSectionPreCodeRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// ------------------------------------------------------------------------ | ||
// MIT License - Copyright (c) Microsoft Corporation. All rights reserved. | ||
// ------------------------------------------------------------------------ | ||
|
||
using Markdig.Extensions.GenericAttributes; | ||
using Markdig.Helpers; | ||
using Markdig.Parsers; | ||
using Markdig.Renderers; | ||
using Markdig.Renderers.Html; | ||
using Markdig.Syntax; | ||
|
||
namespace FluentUI.Demo.Shared.Components; | ||
|
||
/// <summary> | ||
/// Modified version of original markdig CodeBlockRenderer | ||
/// </summary> | ||
/// <see href="https://github.com/xoofx/markdig/blob/master/src/Markdig/Renderers/Html/CodeBlockRenderer.cs"/> | ||
internal class MarkdownSectionPreCodeRenderer : HtmlObjectRenderer<CodeBlock> | ||
{ | ||
private HashSet<string>? _blocksAsDiv; | ||
private readonly MarkdownSectionPreCodeRendererOptions? _options; | ||
|
||
public MarkdownSectionPreCodeRenderer(MarkdownSectionPreCodeRendererOptions? options) | ||
{ | ||
_options = options; | ||
} | ||
public bool OutputAttributesOnPre { get; set; } | ||
|
||
/// <summary> | ||
/// Gets a map of fenced code block infos that should be rendered as div blocks instead of pre/code blocks. | ||
/// </summary> | ||
public HashSet<string> BlocksAsDiv => _blocksAsDiv ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
|
||
protected override void Write(HtmlRenderer renderer, CodeBlock obj) | ||
{ | ||
renderer.EnsureLine(); | ||
|
||
if (_blocksAsDiv is not null && (obj as FencedCodeBlock)?.Info is string info && _blocksAsDiv.Contains(info)) | ||
{ | ||
var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ?? | ||
FencedCodeBlockParser.DefaultInfoPrefix; | ||
|
||
// We are replacing the HTML attribute `language-mylang` by `mylang` only for a div block | ||
// NOTE that we are allocating a closure here | ||
|
||
if (renderer.EnableHtmlForBlock) | ||
{ | ||
renderer.Write("<div") | ||
.WriteAttributes(obj.TryGetAttributes(), | ||
cls => cls.StartsWith(infoPrefix, StringComparison.Ordinal) ? cls.Substring(infoPrefix.Length) : cls) | ||
.Write('>'); | ||
} | ||
|
||
renderer.WriteLeafRawLines(obj, true, true, true); | ||
|
||
if (renderer.EnableHtmlForBlock) | ||
{ | ||
renderer.WriteLine("</div>"); | ||
} | ||
} | ||
else | ||
{ | ||
if (renderer.EnableHtmlForBlock) | ||
{ | ||
renderer.Write("<pre"); | ||
|
||
WritePreAttributes(renderer, obj, _options?.PreTagAttributes); | ||
|
||
renderer.Write("><code"); | ||
|
||
WriteCodeAttributes(renderer, obj, _options?.CodeTagAttributes); | ||
|
||
renderer.Write('>'); | ||
} | ||
|
||
renderer.WriteLeafRawLines(obj, true, true); | ||
|
||
if (renderer.EnableHtmlForBlock) | ||
{ | ||
renderer.WriteLine("</code></pre>"); | ||
} | ||
} | ||
|
||
renderer.EnsureLine(); | ||
} | ||
|
||
private void WritePreAttributes(HtmlRenderer renderer, CodeBlock obj, string? preGenericAttributes) | ||
{ | ||
HtmlAttributes? orig = null; | ||
|
||
if (OutputAttributesOnPre) | ||
{ | ||
orig = obj.TryGetAttributes(); | ||
} | ||
|
||
WriteElementAttributes(renderer, orig, preGenericAttributes); | ||
} | ||
|
||
private void WriteCodeAttributes(HtmlRenderer renderer, CodeBlock obj, string? codeGenericAttributes) | ||
{ | ||
HtmlAttributes? orig = null; | ||
|
||
if (!OutputAttributesOnPre) | ||
{ | ||
orig = obj.TryGetAttributes(); | ||
} | ||
|
||
WriteElementAttributes(renderer, orig, codeGenericAttributes); | ||
} | ||
static private void WriteElementAttributes(HtmlRenderer renderer, HtmlAttributes? fromCodeBlock, string? genericAttributes) | ||
{ | ||
// origin code block had no attributes | ||
fromCodeBlock ??= new HtmlAttributes(); | ||
|
||
// append if any additional attributes provided | ||
var ss = new StringSlice(genericAttributes); | ||
if (!ss.IsEmpty && GenericAttributesParser.TryParse(ref ss, out var attributes)) | ||
{ | ||
if (fromCodeBlock != null) | ||
{ | ||
if (attributes.Classes != null) | ||
{ | ||
foreach (var a in attributes.Classes) | ||
{ | ||
fromCodeBlock.AddClass(a); | ||
} | ||
} | ||
if (attributes.Properties != null) | ||
{ | ||
foreach (var pr in attributes.Properties) | ||
{ | ||
fromCodeBlock.AddProperty(pr.Key, pr.Value!); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// | ||
renderer.WriteAttributes(fromCodeBlock); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
examples/Demo/Shared/Components/MarkdownSectionPreCodeRendererOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace FluentUI.Demo.Shared.Components; | ||
|
||
/// <summary> | ||
/// Options for MarkdownSectionPreCodeRenderer | ||
/// </summary> | ||
internal class MarkdownSectionPreCodeRendererOptions | ||
{ | ||
/// <summary> | ||
/// html attributes for Tag element in markdig generic attributes format | ||
/// </summary> | ||
public string? PreTagAttributes; | ||
/// <summary> | ||
/// html attributes for Code element in markdig generic attributes format | ||
/// </summary> | ||
public string? CodeTagAttributes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.