Enable generic attribute support for wiki markdown containers

This commit is contained in:
Bartłomiej Dach 2022-12-19 20:23:01 +01:00
parent 12aa2e96db
commit 7c282d9def
No known key found for this signature in database
4 changed files with 68 additions and 2 deletions

View File

@ -0,0 +1,34 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using Markdig;
using Markdig.Extensions.GenericAttributes;
using Markdig.Renderers;
using Markdig.Syntax;
namespace osu.Game.Graphics.Containers.Markdown.Extensions
{
/// <summary>
/// A variant of <see cref="Markdig.Extensions.GenericAttributes.GenericAttributesExtension"/>
/// which only handles generic attributes in the current markdown <see cref="Block"/> and ignores inline generic attributes.
/// </summary>
/// <remarks>
/// For rationale, see implementation of <see cref="Setup(Markdig.MarkdownPipelineBuilder)"/>.
/// </remarks>
public class BlockAttributeExtension : IMarkdownExtension
{
private readonly GenericAttributesExtension genericAttributesExtension = new GenericAttributesExtension();
public void Setup(MarkdownPipelineBuilder pipeline)
{
genericAttributesExtension.Setup(pipeline);
// GenericAttributesExtension registers a GenericAttributesParser in pipeline.InlineParsers.
// this conflicts with the CustomContainerExtension, leading to some custom containers (e.g. flags) not displaying.
// as a workaround, remove the inline parser here before it can do damage.
pipeline.InlineParsers.RemoveAll(parser => parser is GenericAttributesParser);
}
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) => genericAttributesExtension.Setup(pipeline, renderer);
}
}

View File

@ -0,0 +1,21 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using Markdig;
namespace osu.Game.Graphics.Containers.Markdown.Extensions
{
public static class OsuMarkdownExtensions
{
/// <summary>
/// Uses the block attributes extension.
/// </summary>
/// <param name="pipeline">The pipeline.</param>
/// <returns>The modified pipeline.</returns>
public static MarkdownPipelineBuilder UseBlockAttributes(this MarkdownPipelineBuilder pipeline)
{
pipeline.Extensions.AddIfNotAlready<BlockAttributeExtension>();
return pipeline;
}
}
}

View File

@ -6,6 +6,7 @@
using Markdig.Extensions.CustomContainers;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Extensions.Footnotes;
using osu.Game.Graphics.Containers.Markdown.Extensions;
namespace osu.Game.Graphics.Containers.Markdown
{
@ -32,6 +33,12 @@ public class OsuMarkdownContainerOptions
/// <seealso cref="CustomContainerExtension"/>
public bool CustomContainers { get; init; }
/// <summary>
/// Allows the <see cref="OsuMarkdownContainer"/> to parse custom attributes in block elements (used e.g. for custom anchor names in the wiki).
/// </summary>
/// <seealso cref="BlockAttributeExtension"/>
public bool BlockAttributes { get; init; }
/// <summary>
/// Returns a prepared <see cref="MarkdownPipeline"/> according to the options specified by the current <see cref="OsuMarkdownContainerOptions"/> instance.
/// </summary>
@ -53,7 +60,10 @@ public MarkdownPipeline BuildPipeline()
pipeline = pipeline.UseAutoLinks();
if (CustomContainers)
pipeline.UseCustomContainers();
pipeline = pipeline.UseCustomContainers();
if (BlockAttributes)
pipeline = pipeline.UseBlockAttributes();
return pipeline.Build();
}

View File

@ -19,7 +19,8 @@ public partial class WikiMarkdownContainer : OsuMarkdownContainer
protected override OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions
{
Footnotes = true,
CustomContainers = true
CustomContainers = true,
BlockAttributes = true
};
public string CurrentPath