2021-04-27 07:09:58 +00: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.
|
|
|
|
|
|
|
|
|
|
using Markdig;
|
2022-09-09 23:55:20 +00:00
|
|
|
|
using Markdig.Extensions.Footnotes;
|
2021-04-28 03:53:12 +00:00
|
|
|
|
using Markdig.Extensions.Tables;
|
2021-04-27 07:09:58 +00:00
|
|
|
|
using Markdig.Extensions.Yaml;
|
|
|
|
|
using Markdig.Syntax;
|
2022-12-16 19:59:14 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2021-04-30 02:47:25 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-04-27 07:09:58 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Containers.Markdown;
|
2022-12-16 20:26:13 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers.Markdown.Footnotes;
|
2021-05-03 02:18:45 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2022-12-16 20:26:13 +00:00
|
|
|
|
using osu.Game.Graphics.Containers.Markdown.Footnotes;
|
2021-05-03 02:18:45 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2022-12-16 20:26:13 +00:00
|
|
|
|
using osuTK;
|
2021-04-27 07:09:58 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers.Markdown
|
|
|
|
|
{
|
2022-12-16 19:59:14 +00:00
|
|
|
|
[Cached]
|
2021-04-27 07:09:58 +00:00
|
|
|
|
public partial class OsuMarkdownContainer : MarkdownContainer
|
|
|
|
|
{
|
2021-05-06 08:13:59 +00:00
|
|
|
|
public OsuMarkdownContainer()
|
|
|
|
|
{
|
|
|
|
|
LineSpacing = 21;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 07:09:58 +00:00
|
|
|
|
protected override void AddMarkdownComponent(IMarkdownObject markdownObject, FillFlowContainer container, int level)
|
|
|
|
|
{
|
|
|
|
|
switch (markdownObject)
|
|
|
|
|
{
|
2022-06-24 12:25:23 +00:00
|
|
|
|
case YamlFrontMatterBlock:
|
2021-04-27 07:09:58 +00:00
|
|
|
|
// Don't parse YAML Frontmatter
|
|
|
|
|
break;
|
|
|
|
|
|
2021-04-30 02:43:21 +00:00
|
|
|
|
case ListItemBlock listItemBlock:
|
2023-06-24 14:07:01 +00:00
|
|
|
|
// `ListBlock.Parent` is annotated as null-returning in xmldoc.
|
|
|
|
|
// Unfortunately code analysis sees that the type doesn't have NRT enabled and complains.
|
|
|
|
|
// This is fixed upstream in 0.24.0 (https://github.com/xoofx/markdig/commit/6684c8257cbbcba2d34457020876be289d3cd8b9),
|
|
|
|
|
// but markdig is a transitive dependency from framework, wherein we are locked to 0.23.0
|
|
|
|
|
// (https://github.com/ppy/osu-framework/blob/9746d7d06f48910c05a24687a25f435f30d12f8b/osu.Framework/osu.Framework.csproj#L52C1-L54)
|
|
|
|
|
// Therefore...
|
2023-06-24 15:07:42 +00:00
|
|
|
|
// ReSharper disable once ConditionalAccessQualifierIsNonNullableAccordingToAPIContract
|
2021-06-07 07:08:44 +00:00
|
|
|
|
bool isOrdered = ((ListBlock)listItemBlock.Parent)?.IsOrdered == true;
|
|
|
|
|
|
|
|
|
|
OsuMarkdownListItem childContainer = CreateListItem(listItemBlock, level, isOrdered);
|
|
|
|
|
|
2021-04-30 02:43:21 +00:00
|
|
|
|
container.Add(childContainer);
|
2021-06-07 07:08:44 +00:00
|
|
|
|
|
2021-04-30 02:43:21 +00:00
|
|
|
|
foreach (var single in listItemBlock)
|
2021-04-30 03:12:43 +00:00
|
|
|
|
base.AddMarkdownComponent(single, childContainer.Content, level);
|
2021-04-30 02:43:21 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2021-04-27 07:09:58 +00:00
|
|
|
|
default:
|
|
|
|
|
base.AddMarkdownComponent(markdownObject, container, level);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 02:18:45 +00:00
|
|
|
|
public override SpriteText CreateSpriteText() => new OsuSpriteText
|
|
|
|
|
{
|
2021-08-03 15:14:44 +00:00
|
|
|
|
Font = OsuFont.GetFont(Typeface.Inter, size: 14, weight: FontWeight.Regular),
|
2021-05-03 02:18:45 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-11-16 03:43:25 +00:00
|
|
|
|
public override OsuMarkdownTextFlowContainer CreateTextFlow() => new OsuMarkdownTextFlowContainer();
|
2021-04-27 09:01:32 +00:00
|
|
|
|
|
2021-05-03 02:35:55 +00:00
|
|
|
|
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) => new OsuMarkdownHeading(headingBlock);
|
|
|
|
|
|
2024-02-17 23:00:55 +00:00
|
|
|
|
protected override MarkdownCodeBlock CreateCodeBlock(CodeBlock codeBlock) => new OsuMarkdownCodeBlock(codeBlock);
|
2021-04-27 09:01:32 +00:00
|
|
|
|
|
2021-04-28 02:23:05 +00:00
|
|
|
|
protected override MarkdownSeparator CreateSeparator(ThematicBreakBlock thematicBlock) => new OsuMarkdownSeparator();
|
|
|
|
|
|
2021-04-28 03:11:29 +00:00
|
|
|
|
protected override MarkdownQuoteBlock CreateQuoteBlock(QuoteBlock quoteBlock) => new OsuMarkdownQuoteBlock(quoteBlock);
|
|
|
|
|
|
2021-04-28 03:53:12 +00:00
|
|
|
|
protected override MarkdownTable CreateTable(Table table) => new OsuMarkdownTable(table);
|
|
|
|
|
|
2021-04-30 02:47:25 +00:00
|
|
|
|
protected override MarkdownList CreateList(ListBlock listBlock) => new MarkdownList
|
|
|
|
|
{
|
|
|
|
|
Padding = new MarginPadding(0)
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-07 06:20:48 +00: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 02:43:21 +00:00
|
|
|
|
|
2022-12-16 20:26:13 +00:00
|
|
|
|
protected override MarkdownFootnoteGroup CreateFootnoteGroup(FootnoteGroup footnoteGroup) => base.CreateFootnoteGroup(footnoteGroup).With(g => g.Spacing = new Vector2(5));
|
|
|
|
|
|
|
|
|
|
protected override MarkdownFootnote CreateFootnote(Footnote footnote) => new OsuMarkdownFootnote(footnote);
|
|
|
|
|
|
2022-12-19 19:16:36 +00:00
|
|
|
|
protected sealed override MarkdownPipeline CreateBuilder()
|
|
|
|
|
=> Options.BuildPipeline();
|
2022-09-09 23:55:20 +00:00
|
|
|
|
|
2022-12-19 19:16:36 +00:00
|
|
|
|
/// <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();
|
2021-04-27 07:09:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|