-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathProgram.cs
70 lines (61 loc) · 2.13 KB
/
Program.cs
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
using ConsoleAppFramework;
using Markdig.Syntax.Inlines;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommandTools
{
public class Program : ConsoleAppBase
{
static async Task Main(string[] args)
{
await Host.CreateDefaultBuilder()
.ConfigureLogging(x =>
{
x.ReplaceToSimpleConsole();
})
.RunConsoleAppFrameworkAsync<Program>(args);
}
// build-table-of-contents ../../../../../ReadMe.md
[Command("build-table-of-contents")]
public void BuildTableOfContents([Option(0)] string readMePath)
{
var md = Markdig.Markdown.Parse(File.ReadAllText(readMePath));
var sb = new StringBuilder();
foreach (var block in md.OfType<Markdig.Syntax.HeadingBlock>())
{
if (block.Level == 1) continue; // skip title
var headerText = ToStringInline(block.Inline);
sb.Append(' ', (block.Level - 2) * 4);
sb.Append("- [");
sb.Append(headerText);
sb.Append("](#");
sb.Append(headerText.ToLower().Replace(' ', '-').Replace(".", "").Replace("/", "").Replace("(", "").Replace(")", "").Replace("`", "").Replace("<", "").Replace(">", "").Replace(",", "").Replace("#", ""));
sb.Append(")");
sb.AppendLine();
}
Console.WriteLine(sb.ToString());
}
static string ToStringInline(ContainerInline inline)
{
var sb = new StringBuilder();
foreach (var item in inline)
{
if (item is LiteralInline li)
{
sb.Append(li.Content.ToString());
}
else if (item is CodeInline ci)
{
sb.Append('`');
sb.Append(ci.Content);
sb.Append('`');
}
}
return sb.ToString();
}
}
}