Skip to content

Commit

Permalink
Fix cherry-pick errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vnbaaij committed Jun 12, 2024
1 parent c88f64f commit 8eec315
Show file tree
Hide file tree
Showing 26 changed files with 372 additions and 387 deletions.
4 changes: 2 additions & 2 deletions examples/Demo/Client/FluentUI.Demo.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.30" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.30" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.31" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.31" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net7.0'">
Expand Down
2 changes: 1 addition & 1 deletion examples/Demo/Shared/Components/MarkdownSection.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Components;

using Microsoft.Fast.Components.FluentUI;
using Microsoft.Fast.Components.FluentUI.Infrastructure;
using Microsoft.JSInterop;


// Remember to replace the namespace below with your own project's namespace..
Expand Down
34 changes: 34 additions & 0 deletions examples/Demo/Shared/Components/MarkdownSectionPreCodeExtension.cs
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 examples/Demo/Shared/Components/MarkdownSectionPreCodeRenderer.cs
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);
}
}
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;
}
2 changes: 1 addition & 1 deletion examples/Demo/Shared/Components/SiteSettingsPanel.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task UpdateDirectionAsync()
Content.Dir = _dir;

await _jsModule!.InvokeVoidAsync("switchDirection", _dir.ToString());
await Direction.WithDefault(_dir.ToAttributeValue());
await Direction.WithDefault(_dir.ToAttributeValue()!);

StateHasChanged();
}
Expand Down
2 changes: 1 addition & 1 deletion examples/Demo/Shared/FluentUI.Demo.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.30" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.31" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net7.0'">
Expand Down
Loading

0 comments on commit 8eec315

Please sign in to comment.