mirror of
https://github.com/ppy/osu
synced 2025-02-18 03:16:57 +00:00
Refactor markdown extension management
This commit is contained in:
parent
b42accb763
commit
db1380a346
@ -4,10 +4,6 @@
|
|||||||
#nullable disable
|
#nullable disable
|
||||||
|
|
||||||
using Markdig;
|
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.Tables;
|
||||||
using Markdig.Extensions.Yaml;
|
using Markdig.Extensions.Yaml;
|
||||||
using Markdig.Syntax;
|
using Markdig.Syntax;
|
||||||
@ -21,24 +17,6 @@ namespace osu.Game.Graphics.Containers.Markdown
|
|||||||
{
|
{
|
||||||
public partial class OsuMarkdownContainer : MarkdownContainer
|
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()
|
public OsuMarkdownContainer()
|
||||||
{
|
{
|
||||||
LineSpacing = 21;
|
LineSpacing = 21;
|
||||||
@ -99,25 +77,13 @@ namespace osu.Game.Graphics.Containers.Markdown
|
|||||||
return new OsuMarkdownUnorderedListItem(level);
|
return new OsuMarkdownUnorderedListItem(level);
|
||||||
}
|
}
|
||||||
|
|
||||||
// reference: https://github.com/ppy/osu-web/blob/05488a96b25b5a09f2d97c54c06dd2bae59d1dc8/app/Libraries/Markdown/OsuMarkdown.php#L301
|
protected sealed override MarkdownPipeline CreateBuilder()
|
||||||
protected override MarkdownPipeline CreateBuilder()
|
=> Options.BuildPipeline();
|
||||||
{
|
|
||||||
var pipeline = new MarkdownPipelineBuilder()
|
|
||||||
.UseAutoIdentifiers()
|
|
||||||
.UsePipeTables()
|
|
||||||
.UseEmphasisExtras(EmphasisExtraOptions.Strikethrough)
|
|
||||||
.UseYamlFrontMatter();
|
|
||||||
|
|
||||||
if (Footnotes)
|
/// <summary>
|
||||||
pipeline = pipeline.UseFootnotes();
|
/// Creates a <see cref="OsuMarkdownContainerOptions"/> instance which is used to determine
|
||||||
|
/// which CommonMark/Markdig extensions should be enabled for this <see cref="OsuMarkdownContainer"/>.
|
||||||
if (Autolinks)
|
/// </summary>
|
||||||
pipeline = pipeline.UseAutoLinks();
|
protected virtual OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions();
|
||||||
|
|
||||||
if (CustomContainers)
|
|
||||||
pipeline.UseCustomContainers();
|
|
||||||
|
|
||||||
return pipeline.Build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,10 @@ namespace osu.Game.Overlays.Comments
|
|||||||
{
|
{
|
||||||
public partial class CommentMarkdownContainer : OsuMarkdownContainer
|
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);
|
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) => new CommentMarkdownHeading(headingBlock);
|
||||||
|
|
||||||
|
@ -16,8 +16,11 @@ namespace osu.Game.Overlays.Wiki.Markdown
|
|||||||
{
|
{
|
||||||
public partial class WikiMarkdownContainer : OsuMarkdownContainer
|
public partial class WikiMarkdownContainer : OsuMarkdownContainer
|
||||||
{
|
{
|
||||||
protected override bool Footnotes => true;
|
protected override OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions
|
||||||
protected override bool CustomContainers => true;
|
{
|
||||||
|
Footnotes = true,
|
||||||
|
CustomContainers = true
|
||||||
|
};
|
||||||
|
|
||||||
public string CurrentPath
|
public string CurrentPath
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user