-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlainTextMarkdownVisitor.cs
More file actions
147 lines (128 loc) · 4.32 KB
/
Copy pathPlainTextMarkdownVisitor.cs
File metadata and controls
147 lines (128 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Markdig.Extensions.Tables;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
namespace HelpLine.Docs;
/// <summary>
/// Renders a parsed Markdown AST as plain text (with optional ANSI formatting) to a <see cref="TextWriter"/>.
/// </summary>
public sealed class PlainTextMarkdownVisitor : IMarkdownVisitor
{
private readonly TextWriter _writer;
public PlainTextMarkdownVisitor(TextWriter writer)
{
ArgumentNullException.ThrowIfNull(writer);
_writer = writer;
}
/// <inheritdoc/>
public void VisitHeading(HeadingBlock heading)
{
var title = RenderInlines(heading.Inline);
var prefix = new string('#', heading.Level);
_writer.WriteLine(Colorize($"{prefix} {title}", bold: true, colorCode: "96"));
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitParagraph(ParagraphBlock paragraph)
{
_writer.WriteLine(RenderInlines(paragraph.Inline));
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitCode(CodeBlock code)
{
foreach (var line in code.Lines.Lines)
{
_writer.WriteLine(" " + line.Slice.ToString());
}
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitFencedCode(FencedCodeBlock code)
{
foreach (var line in code.Lines.Lines)
{
_writer.WriteLine(" " + line.Slice.ToString());
}
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitListItem(ListItemBlock item, bool isOrdered)
{
var bullet = isOrdered ? " • " : " • ";
foreach (var block in item)
{
if (block is ParagraphBlock paragraph)
{
_writer.Write(bullet);
_writer.WriteLine(RenderInlines(paragraph.Inline));
}
}
}
/// <inheritdoc/>
public void VisitQuote(QuoteBlock quote)
{
foreach (var block in quote)
{
if (block is ParagraphBlock paragraph)
{
_writer.Write(" ");
_writer.WriteLine(RenderInlines(paragraph.Inline));
}
}
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitThematicBreak(ThematicBreakBlock rule)
{
_writer.WriteLine(new string('-', 40));
_writer.WriteLine();
}
/// <inheritdoc/>
public void VisitTable(Table table)
{
foreach (var row in table.OfType<TableRow>())
{
var cells = row.OfType<TableCell>().Select(cell => RenderInlines(cell.Descendants<ParagraphBlock>().FirstOrDefault()?.Inline));
_writer.WriteLine(string.Join(" | ", cells));
}
_writer.WriteLine();
}
/// <summary>
/// Renders an inline container (bold, emphasis, code spans, links, literal text) to a string.
/// </summary>
private string RenderInlines(ContainerInline? inlines)
{
if (inlines is null)
{
return string.Empty;
}
var sb = new System.Text.StringBuilder();
foreach (var inline in inlines)
{
sb.Append(RenderInline(inline));
}
return sb.ToString();
}
private string RenderInline(Inline inline) => inline switch
{
LiteralInline literal => literal.Content.ToString(),
CodeInline code => Colorize(code.Content, bold: false, colorCode: "93"),
EmphasisInline emphasis when emphasis.DelimiterCount >= 2 => Colorize(RenderInlines(emphasis), bold: true, colorCode: "93"),
EmphasisInline emphasis => Colorize(RenderInlines(emphasis), bold: false, colorCode: "93"),
LinkInline link => RenderInlines(link),
LineBreakInline => Environment.NewLine,
ContainerInline container => RenderInlines(container),
_ => string.Empty
};
private bool SupportsAnsi() =>
ReferenceEquals(_writer, Console.Out) && !Console.IsOutputRedirected;
private string Colorize(string text, bool bold, string colorCode)
{
if (!SupportsAnsi())
{
return text;
}
var boldCode = bold ? "1;" : string.Empty;
return $"\u001b[{boldCode}{colorCode}m{text}\u001b[0m";
}
}