Last active
January 20, 2025 14:59
-
-
Save baltermia/341e5a65a63ff303e420ae386e45895c to your computer and use it in GitHub Desktop.
Simple Markdown Sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| namespace SimpleMarkdown; | |
| class Testing | |
| { | |
| public static void Test() | |
| { | |
| // either use .With or set MarkdownBuilder.DefaultParseMode to globally set it once | |
| string nice = "Hey!".Bold().Link("https://google.com/").With(ParseMode.Markdown); | |
| Console.WriteLine(nice); // => [**Hey!**](https://google.com/) | |
| } | |
| } | |
| enum ParseMode | |
| { | |
| Html, | |
| Markdown, | |
| } | |
| abstract class MarkdownElement | |
| { | |
| public abstract string Apply(string content, ParseMode mode); | |
| } | |
| class BoldMarkdown : MarkdownElement | |
| { | |
| public override string Apply(string content, ParseMode mode) => mode switch | |
| { | |
| ParseMode.Html => $"<b>{content}</b>", | |
| ParseMode.Markdown => $"**{content}**", | |
| _ => content, | |
| }; | |
| } | |
| class LinkMarkdown(string url) : MarkdownElement | |
| { | |
| public string Url { get; } = url; | |
| public override string Apply(string content, ParseMode mode) => mode switch | |
| { | |
| ParseMode.Html => $"<a href={Url}>{content}</a>", | |
| ParseMode.Markdown => $"[{content}]({Url})", | |
| _ => content, | |
| }; | |
| } | |
| static class MarkdownExtensions | |
| { | |
| public static MarkdownBuilder Bold(this string content) => new MarkdownBuilder(content).Bold(); | |
| public static MarkdownBuilder Bold(this MarkdownBuilder builder) | |
| { | |
| if (!builder.Elements.OfType<BoldMarkdown>().Any()) | |
| builder.Elements.Add(new BoldMarkdown()); | |
| return builder; | |
| } | |
| public static MarkdownBuilder Link(this string content, string url) => new MarkdownBuilder(content).Link(url); | |
| public static MarkdownBuilder Link(this MarkdownBuilder builder, string url) | |
| { | |
| if (!builder.Elements.OfType<LinkMarkdown>().Any()) | |
| builder.Elements.Add(new LinkMarkdown(url)); | |
| return builder; | |
| } | |
| public static MarkdownBuilder With(this MarkdownBuilder builder, ParseMode mode) | |
| { | |
| builder.Mode = mode; | |
| return builder; | |
| } | |
| } | |
| class MarkdownBuilder(string content) | |
| { | |
| public static ParseMode DefaultParseMode = ParseMode.Html; | |
| public string Content { get; } = content; | |
| public ParseMode? Mode { get; set; } = null; | |
| public ICollection<MarkdownElement> Elements { get; } = []; | |
| public static implicit operator string(MarkdownBuilder builder) => builder.ToString(); | |
| public static implicit operator MarkdownBuilder(string text) => new(text); | |
| public override string ToString() => ToString(null); | |
| public string ToString(ParseMode? mode = null) | |
| { | |
| string result = Content; | |
| foreach (MarkdownElement element in Elements) | |
| result = element.Apply(result, mode ?? Mode ?? DefaultParseMode); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment