Refactor markdown extension management

This commit is contained in:
Bartłomiej Dach 2022-12-19 20:16:36 +01:00
parent b42accb763
commit db1380a346
No known key found for this signature in database
4 changed files with 77 additions and 44 deletions

View File

@ -4,10 +4,6 @@
#nullable disable
using Markdig;
using Markdig.Extensions.AutoLinks;
using Markdig.Extensions.CustomContainers;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Extensions.Footnotes;
using Markdig.Extensions.Tables;
using Markdig.Extensions.Yaml;
using Markdig.Syntax;
@ -21,24 +17,6 @@ namespace osu.Game.Graphics.Containers.Markdown
{
public partial class OsuMarkdownContainer : MarkdownContainer
{
/// <summary>
/// Allows this markdown container to parse and link footnotes.
/// </summary>
/// <seealso cref="FootnoteExtension"/>
protected virtual bool Footnotes => false;
/// <summary>
/// Allows this markdown container to make URL text clickable.
/// </summary>
/// <seealso cref="AutoLinkExtension"/>
protected virtual bool Autolinks => false;
/// <summary>
/// Allows this markdown container to parse custom containers (used for flags and infoboxes).
/// </summary>
/// <seealso cref="CustomContainerExtension"/>
protected virtual bool CustomContainers => false;
public OsuMarkdownContainer()
{
LineSpacing = 21;
@ -99,25 +77,13 @@ protected virtual OsuMarkdownListItem CreateListItem(ListItemBlock listItemBlock
return new OsuMarkdownUnorderedListItem(level);
}
// reference: https://github.com/ppy/osu-web/blob/05488a96b25b5a09f2d97c54c06dd2bae59d1dc8/app/Libraries/Markdown/OsuMarkdown.php#L301
protected override MarkdownPipeline CreateBuilder()
{
var pipeline = new MarkdownPipelineBuilder()
.UseAutoIdentifiers()
.UsePipeTables()
.UseEmphasisExtras(EmphasisExtraOptions.Strikethrough)
.UseYamlFrontMatter();
protected sealed override MarkdownPipeline CreateBuilder()
=> Options.BuildPipeline();
if (Footnotes)
pipeline = pipeline.UseFootnotes();
if (Autolinks)
pipeline = pipeline.UseAutoLinks();
if (CustomContainers)
pipeline.UseCustomContainers();
return pipeline.Build();
}
/// <summary>
/// Creates a <see cref="OsuMarkdownContainerOptions"/> instance which is used to determine
/// which CommonMark/Markdig extensions should be enabled for this <see cref="OsuMarkdownContainer"/>.
/// </summary>
protected virtual OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions();
}
}

View File

@ -0,0 +1,61 @@
// 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.AutoLinks;
using Markdig.Extensions.CustomContainers;
using Markdig.Extensions.EmphasisExtras;
using Markdig.Extensions.Footnotes;
namespace osu.Game.Graphics.Containers.Markdown
{
/// <summary>
/// Groups options of customising the set of available extensions to <see cref="OsuMarkdownContainer"/> instances.
/// </summary>
public class OsuMarkdownContainerOptions
{
/// <summary>
/// Allows the <see cref="OsuMarkdownContainer"/> to parse and link footnotes.
/// </summary>
/// <seealso cref="FootnoteExtension"/>
public bool Footnotes { get; init; }
/// <summary>
/// Allows the <see cref="OsuMarkdownContainer"/> container to make URL text clickable.
/// </summary>
/// <seealso cref="AutoLinkExtension"/>
public bool Autolinks { get; init; }
/// <summary>
/// Allows the <see cref="OsuMarkdownContainer"/> to parse custom containers (used for flags and infoboxes).
/// </summary>
/// <seealso cref="CustomContainerExtension"/>
public bool CustomContainers { get; init; }
/// <summary>
/// Returns a prepared <see cref="MarkdownPipeline"/> according to the options specified by the current <see cref="OsuMarkdownContainerOptions"/> instance.
/// </summary>
/// <remarks>
/// Compare: https://github.com/ppy/osu-web/blob/05488a96b25b5a09f2d97c54c06dd2bae59d1dc8/app/Libraries/Markdown/OsuMarkdown.php#L301
/// </remarks>
public MarkdownPipeline BuildPipeline()
{
var pipeline = new MarkdownPipelineBuilder()
.UseAutoIdentifiers()
.UsePipeTables()
.UseEmphasisExtras(EmphasisExtraOptions.Strikethrough)
.UseYamlFrontMatter();
if (Footnotes)
pipeline = pipeline.UseFootnotes();
if (Autolinks)
pipeline = pipeline.UseAutoLinks();
if (CustomContainers)
pipeline.UseCustomContainers();
return pipeline.Build();
}
}
}

View File

@ -11,7 +11,10 @@ namespace osu.Game.Overlays.Comments
{
public partial class CommentMarkdownContainer : OsuMarkdownContainer
{
protected override bool Autolinks => true;
protected override OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions
{
Autolinks = true
};
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) => new CommentMarkdownHeading(headingBlock);

View File

@ -16,8 +16,11 @@ namespace osu.Game.Overlays.Wiki.Markdown
{
public partial class WikiMarkdownContainer : OsuMarkdownContainer
{
protected override bool Footnotes => true;
protected override bool CustomContainers => true;
protected override OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions
{
Footnotes = true,
CustomContainers = true
};
public string CurrentPath
{