using System.Text;
using System.Text.RegularExpressions;
using Markdig;
using Markdig.Renderers.Html;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
namespace Elternbeirat.Web.Shared;
///
/// Renders the editors' Markdown (the body, intro and answer
/// fields from PocketBase) to the HTML the pages show.
///
///
/// The pipeline is CommonMark plus exactly two extensions, each there for a reason
/// an editor can see:
///
/// -
///
/// Custom containers (::: name … :::) turn a block
/// into <div class="name">. That is how editors use the
/// design blocks (kennzahlen, aufruf, kacheln,
/// hinweis) without writing HTML. An unknown name just yields a
/// div without styling, so a typo never breaks a page.
///
///
/// -
///
/// Pipe tables: plain CommonMark has no tables at all, so without
/// this the table styles in app.css could never apply.
///
///
///
///
/// Raw HTML in the Markdown is escaped, not passed through
/// ('s DisableHtml). The result is
/// rendered as a MarkupString, so a passed-through <script>
/// would run in the visitor's browser, and the privacy text promises that no
/// program code runs there. Editors style pages with the blocks instead.
///
///
/// After parsing, a paragraph (or list item) that consists of nothing but one
/// link gets the class . Pure CSS cannot tell
/// "a link alone on its line" from "a link inside a sentence" (selectors ignore
/// the text around an element), but the stylesheet needs exactly that to show a
/// lone mailto: link as a button and a lone PDF link as a file card.
///
///
///
public static partial class Markdown
{
///
/// The class marking a paragraph or list item whose only content is one link.
/// app.css keys the mail button and the PDF card off it.
///
public const string LoneLinkClass = "lone-link";
private static readonly MarkdownPipeline Pipeline =
new MarkdownPipelineBuilder()
.UseCustomContainers()
.UsePipeTables()
.DisableHtml()
.Build();
///
/// Converts a Markdown string to HTML.
///
/// The editor's Markdown; may be .
///
/// The rendered HTML, or an empty string for or empty
/// input, so callers can bind the result directly.
///
///
///
/// Markdown.ToHtml("::: hinweis\nBitte vormerken.\n:::");
/// // <div class="hinweis"><p>Bitte vormerken.</p></div>
///
///
public static string ToHtml(string? markdown) =>
string.IsNullOrEmpty(markdown) ? "" : Render(Parse(markdown));
///
/// Extracts the first sentence of a Markdown text as plain text, e.g. as the
/// short description on a home page tile.
///
/// The editor's Markdown; may be .
///
/// The longest sentence kept as is; a longer one is cut at the last word
/// boundary before the limit and ends in ….
///
///
/// The first sentence of the first top-level paragraph, with all Markdown
/// removed and whitespace collapsed, or an empty string when there is no such
/// paragraph (empty body, or only headings, lists and blocks).
///
///
/// is zero or negative.
///
///
/// Only top-level paragraphs count: a heading repeats the title, and the text in
/// a list or a ::: block is rarely a sentence that describes the page.
///
/// A sentence ends at ., ! or ? followed by a space or
/// the end. A period after a single letter or a number does not count, so
/// z. B. and 13. November do not cut the sentence short. A
/// longer abbreviation such as bzw. still does; that costs the rest
/// of a teaser, never the page.
///
///
///
///
/// Markdown.FirstSentence("# Vorstand\n\nWir sind **sieben** Eltern. Mehr unten.");
/// // "Wir sind sieben Eltern."
///
///
public static string FirstSentence(string? markdown, int maxLength = 140) =>
maxLength <= 0
? throw new ArgumentOutOfRangeException(nameof(maxLength), maxLength, "Must be positive.")
: string.IsNullOrWhiteSpace(markdown)
? ""
: Parse(markdown).OfType().FirstOrDefault()?.Inline is { } inline
? Shorten(UpToSentenceEnd(PlainText(inline)), maxLength)
: "";
///
/// Parses Markdown with the site's pipeline, for callers that take the document
/// apart before rendering it (see ).
///
/// The editor's Markdown.
/// The parsed document.
internal static MarkdownDocument Parse(string markdown) =>
Markdig.Markdown.Parse(markdown, Pipeline);
///
/// Renders a document from to HTML, marking lone links on
/// the way, exactly as does.
///
/// The parsed document; changed in place.
/// The rendered HTML.
internal static string Render(MarkdownDocument document) =>
Markdig.Markdown.ToHtml(MarkLoneLinks(document), Pipeline);
///
/// Flattens inline Markdown to the text a reader sees: emphasis and link
/// markup dropped, link text kept, images left out, whitespace collapsed.
///
/// The inline content, e.g. of a paragraph or heading.
/// The visible text, trimmed.
internal static string PlainText(ContainerInline inline) =>
Whitespace().Replace(AppendText(new StringBuilder(), inline).ToString(), " ").Trim();
///
/// Whether an inline carries no visible content (whitespace or a line break).
///
/// The inline to inspect.
///
/// if it can be ignored when looking for links that stand
/// alone in a paragraph.
///
internal static bool IsBlank(Inline inline) =>
inline switch
{
LineBreakInline => true,
LiteralInline literal => literal.Content.IsEmptyOrWhitespace(),
_ => false,
};
///
/// Appends the visible text of an inline and its children.
///
/// The builder to append to.
/// The inline to flatten.
/// The same , for chaining.
private static StringBuilder AppendText(StringBuilder text, Inline inline) =>
inline switch
{
LiteralInline literal => text.Append(literal.Content.ToString()),
CodeInline code => text.Append(code.Content),
HtmlEntityInline entity => text.Append(entity.Transcoded.ToString()),
AutolinkInline autolink => text.Append(autolink.Url),
LineBreakInline => text.Append(' '),
LinkInline { IsImage: true } => text,
ContainerInline container => container.Aggregate(text, AppendText),
_ => text,
};
///
/// Cuts a text after its first sentence.
///
/// Plain text.
///
/// The first sentence including its end mark, or the whole text if it has none.
///
private static string UpToSentenceEnd(string text) =>
SentenceEnd().Match(text) is { Success: true } end ? text[..(end.Index + 1)] : text;
///
/// Shortens a text to at most characters plus an
/// ellipsis, cutting between words.
///
/// Plain text.
/// The longest text kept as is.
/// The text, shortened if needed.
///
/// The search for a space starts at itself, so a
/// word that ends exactly at the limit is kept. A single word longer than the
/// limit is cut hard.
///
private static string Shorten(string text, int maxLength) =>
text.Length <= maxLength
? text
: text.LastIndexOf(' ', maxLength) is > 0 and var space
? $"{text[..space].TrimEnd(',', ';', ':', '-', '–')}…"
: $"{text[..maxLength]}…";
///
/// Matches the end mark of a sentence: ., ! or ? before a
/// space or the end, unless it follows a lone letter (z.) or a number
/// (13.).
///
[GeneratedRegex(@"(?
/// Matches a run of whitespace, collapsed to one space in plain text.
///
[GeneratedRegex(@"\s+")]
private static partial Regex Whitespace();
///
/// Adds to every paragraph that holds nothing but
/// one link.
///
/// The parsed document; changed in place.
/// The same , for chaining.
///
/// Inside a list item the class goes on the item, not the paragraph: in a tight
/// list Markdig writes no <p> at all, so a class on the paragraph
/// would be silently dropped.
///
private static MarkdownDocument MarkLoneLinks(MarkdownDocument document)
{
foreach (var paragraph in document.Descendants().Where(IsLoneLink))
{
MarkdownObject target = paragraph.Parent is ListItemBlock { Count: 1 } item ? item : paragraph;
target.GetAttributes().AddClass(LoneLinkClass);
}
return document;
}
///
/// Whether a paragraph's content is exactly one link, ignoring surrounding
/// whitespace and line breaks.
///
/// The paragraph to inspect.
///
/// for one link (inline [text](url) or an
/// autolink <url>, but not an image) and nothing else.
///
private static bool IsLoneLink(ParagraphBlock paragraph) =>
paragraph.Inline?
.Where(inline => !IsBlank(inline))
.ToList() is [LinkInline { IsImage: false } or AutolinkInline];
}