2021-04-27 15:09:58 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-04-27 15:09:58 +08:00
|
|
|
using Markdig;
|
2022-09-10 07:55:20 +08:00
|
|
|
using Markdig.Extensions.AutoLinks;
|
2022-10-09 02:24:57 +08:00
|
|
|
using Markdig.Extensions.CustomContainers;
|
2022-09-10 07:55:20 +08:00
|
|
|
using Markdig.Extensions.EmphasisExtras;
|
|
|
|
using Markdig.Extensions.Footnotes;
|
2021-04-28 11:53:12 +08:00
|
|
|
using Markdig.Extensions.Tables;
|
2021-04-27 15:09:58 +08:00
|
|
|
using Markdig.Extensions.Yaml;
|
|
|
|
using Markdig.Syntax;
|
2021-04-30 10:47:25 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-04-27 15:09:58 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Containers.Markdown;
|
2021-05-03 10:18:45 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-04-27 15:09:58 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers.Markdown
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class OsuMarkdownContainer : MarkdownContainer
|
2021-04-27 15:09:58 +08:00
|
|
|
{
|
2022-09-10 07:55:20 +08:00
|
|
|
/// <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;
|
|
|
|
|
2022-10-09 02:24:57 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Allows this markdown container to parse custom containers (used for flags and infoboxes).
|
|
|
|
/// </summary>
|
|
|
|
/// <seealso cref="CustomContainerExtension"/>
|
|
|
|
protected virtual bool CustomContainers => false;
|
|
|
|
|
2021-05-06 16:13:59 +08:00
|
|
|
public OsuMarkdownContainer()
|
|
|
|
{
|
|
|
|
LineSpacing = 21;
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:09:58 +08:00
|
|
|
protected override void AddMarkdownComponent(IMarkdownObject markdownObject, FillFlowContainer container, int level)
|
|
|
|
{
|
|
|
|
switch (markdownObject)
|
|
|
|
{
|
2022-06-24 20:25:23 +08:00
|
|
|
case YamlFrontMatterBlock:
|
2021-04-27 15:09:58 +08:00
|
|
|
// Don't parse YAML Frontmatter
|
|
|
|
break;
|
|
|
|
|
2021-04-30 10:43:21 +08:00
|
|
|
case ListItemBlock listItemBlock:
|
2021-06-07 15:08:44 +08:00
|
|
|
bool isOrdered = ((ListBlock)listItemBlock.Parent)?.IsOrdered == true;
|
|
|
|
|
|
|
|
OsuMarkdownListItem childContainer = CreateListItem(listItemBlock, level, isOrdered);
|
|
|
|
|
2021-04-30 10:43:21 +08:00
|
|
|
container.Add(childContainer);
|
2021-06-07 15:08:44 +08:00
|
|
|
|
2021-04-30 10:43:21 +08:00
|
|
|
foreach (var single in listItemBlock)
|
2021-04-30 11:12:43 +08:00
|
|
|
base.AddMarkdownComponent(single, childContainer.Content, level);
|
2021-04-30 10:43:21 +08:00
|
|
|
break;
|
|
|
|
|
2021-04-27 15:09:58 +08:00
|
|
|
default:
|
|
|
|
base.AddMarkdownComponent(markdownObject, container, level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 10:18:45 +08:00
|
|
|
public override SpriteText CreateSpriteText() => new OsuSpriteText
|
|
|
|
{
|
2021-08-03 23:14:44 +08:00
|
|
|
Font = OsuFont.GetFont(Typeface.Inter, size: 14, weight: FontWeight.Regular),
|
2021-05-03 10:18:45 +08:00
|
|
|
};
|
|
|
|
|
2021-04-27 17:01:32 +08:00
|
|
|
public override MarkdownTextFlowContainer CreateTextFlow() => new OsuMarkdownTextFlowContainer();
|
|
|
|
|
2021-05-03 10:35:55 +08:00
|
|
|
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) => new OsuMarkdownHeading(headingBlock);
|
|
|
|
|
2021-04-27 17:01:32 +08:00
|
|
|
protected override MarkdownFencedCodeBlock CreateFencedCodeBlock(FencedCodeBlock fencedCodeBlock) => new OsuMarkdownFencedCodeBlock(fencedCodeBlock);
|
|
|
|
|
2021-04-28 10:23:05 +08:00
|
|
|
protected override MarkdownSeparator CreateSeparator(ThematicBreakBlock thematicBlock) => new OsuMarkdownSeparator();
|
|
|
|
|
2021-04-28 11:11:29 +08:00
|
|
|
protected override MarkdownQuoteBlock CreateQuoteBlock(QuoteBlock quoteBlock) => new OsuMarkdownQuoteBlock(quoteBlock);
|
|
|
|
|
2021-04-28 11:53:12 +08:00
|
|
|
protected override MarkdownTable CreateTable(Table table) => new OsuMarkdownTable(table);
|
|
|
|
|
2021-04-30 10:47:25 +08:00
|
|
|
protected override MarkdownList CreateList(ListBlock listBlock) => new MarkdownList
|
|
|
|
{
|
|
|
|
Padding = new MarginPadding(0)
|
|
|
|
};
|
|
|
|
|
2021-05-07 14:20:48 +08:00
|
|
|
protected virtual OsuMarkdownListItem CreateListItem(ListItemBlock listItemBlock, int level, bool isOrdered)
|
|
|
|
{
|
|
|
|
if (isOrdered)
|
|
|
|
return new OsuMarkdownOrderedListItem(listItemBlock.Order);
|
|
|
|
|
|
|
|
return new OsuMarkdownUnorderedListItem(level);
|
|
|
|
}
|
2021-04-30 10:43:21 +08:00
|
|
|
|
2022-09-10 07:55:20 +08:00
|
|
|
// reference: https://github.com/ppy/osu-web/blob/05488a96b25b5a09f2d97c54c06dd2bae59d1dc8/app/Libraries/Markdown/OsuMarkdown.php#L301
|
2021-04-27 15:09:58 +08:00
|
|
|
protected override MarkdownPipeline CreateBuilder()
|
2022-09-10 07:55:20 +08:00
|
|
|
{
|
|
|
|
var pipeline = new MarkdownPipelineBuilder()
|
|
|
|
.UseAutoIdentifiers()
|
|
|
|
.UsePipeTables()
|
|
|
|
.UseEmphasisExtras(EmphasisExtraOptions.Strikethrough)
|
|
|
|
.UseYamlFrontMatter();
|
|
|
|
|
|
|
|
if (Footnotes)
|
|
|
|
pipeline = pipeline.UseFootnotes();
|
|
|
|
|
|
|
|
if (Autolinks)
|
|
|
|
pipeline = pipeline.UseAutoLinks();
|
|
|
|
|
2022-10-09 02:24:57 +08:00
|
|
|
if (CustomContainers)
|
|
|
|
pipeline.UseCustomContainers();
|
|
|
|
|
2022-09-10 07:55:20 +08:00
|
|
|
return pipeline.Build();
|
|
|
|
}
|
2021-04-27 15:09:58 +08:00
|
|
|
}
|
|
|
|
}
|