Skip to content

Commit 077585a

Browse files
committed
Table of Contents: add script and regenerate
1 parent 3d38fb6 commit 077585a

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A curated list of awesome F# frameworks, libraries, software and resources.
1111
- [Actor frameworks](#actor-frameworks)
1212
- [Build tools](#build-tools)
1313
- [Cloud](#cloud)
14+
- [Code Generation](#code-generation)
1415
- [Compilers](#compilers)
1516
- [Concurrent, asynchronous and parallel programming](#concurrent-asynchronous-and-parallel-programming)
1617
- [Configuration](#configuration)
@@ -19,16 +20,16 @@ A curated list of awesome F# frameworks, libraries, software and resources.
1920
- [IDE](#ide)
2021
- [Editor plugins](#editor-plugins)
2122
- [Performance analysis](#performance-analysis)
22-
- [Game development](#game-development)
2323
- [General purpose libraries](#general-purpose-libraries)
24+
- [Game development](#game-development)
2425
- [GUI](#gui)
2526
- [HTTP Clients](#http-clients)
2627
- [Logging](#logging)
2728
- [Package Management](#package-management)
2829
- [Parsing](#parsing)
2930
- [PreCompilation](#precompilation)
30-
- [Search](#search)
3131
- [Serialization](#serialization)
32+
- [Search](#search)
3233
- [Simulation](#simulation)
3334
- [Testing](#testing)
3435
- [Type providers](#type-providers)

update-toc.fsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#r "nuget: Markdig, 0.38.0"
2+
3+
open System
4+
open Markdig
5+
open System.IO
6+
open Markdig.Renderers.Roundtrip
7+
open Markdig.Syntax
8+
9+
let readmeFilePath = Path.Combine(__SOURCE_DIRECTORY__, "README.md")
10+
11+
let markdown = File.ReadAllText(readmeFilePath)
12+
let document = Markdown.Parse(markdown, trackTrivia = true)
13+
14+
let headers =
15+
document.Descendants<HeadingBlock>()
16+
|> Seq.skip 2 // The document header and the Table of Contents
17+
18+
let getLink(header: string) =
19+
let characters =
20+
header
21+
|> Seq.choose(function
22+
| c when Char.IsLetterOrDigit c -> Some(Char.ToLowerInvariant c)
23+
| ' ' -> Some '-'
24+
| _ -> None
25+
)
26+
|> Seq.toArray
27+
28+
"#" + String characters
29+
30+
let tocText =
31+
"- [Awesome F#](#)\n" + (
32+
headers
33+
|> Seq.map(fun h ->
34+
let text = h.Inline.FirstChild.ToString()
35+
let indent = String(' ', (h.Level - 1) * 2)
36+
$"{indent}- [{text}]({getLink text})"
37+
)
38+
|> String.concat "\n"
39+
) + "\n\n"
40+
41+
let updateHeader (header: HeadingBlock) (newContent: string) =
42+
let parent = header.Parent
43+
let index = parent.IndexOf header
44+
while not (parent[index + 1] :? HeadingBlock) do
45+
parent.RemoveAt(index + 1)
46+
47+
let newMd = Markdown.Parse(newContent, trackTrivia = true)
48+
for block in newMd do
49+
block.Parent.Remove block |> ignore
50+
parent.Insert(index + 1, block)
51+
52+
let firstHeader =
53+
document.Descendants<HeadingBlock>()
54+
|> Seq.find (fun h -> h.Level = 2)
55+
56+
updateHeader firstHeader tocText
57+
58+
File.WriteAllText(readmeFilePath,
59+
use sw = new StringWriter()
60+
let renderer = RoundtripRenderer sw
61+
renderer.Write document
62+
sw.ToString()
63+
)

0 commit comments

Comments
 (0)