Skip to content

Commit

Permalink
Table of Contents: add script and regenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Nov 3, 2024
1 parent 3d38fb6 commit 077585a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A curated list of awesome F# frameworks, libraries, software and resources.
- [Actor frameworks](#actor-frameworks)
- [Build tools](#build-tools)
- [Cloud](#cloud)
- [Code Generation](#code-generation)
- [Compilers](#compilers)
- [Concurrent, asynchronous and parallel programming](#concurrent-asynchronous-and-parallel-programming)
- [Configuration](#configuration)
Expand All @@ -19,16 +20,16 @@ A curated list of awesome F# frameworks, libraries, software and resources.
- [IDE](#ide)
- [Editor plugins](#editor-plugins)
- [Performance analysis](#performance-analysis)
- [Game development](#game-development)
- [General purpose libraries](#general-purpose-libraries)
- [Game development](#game-development)
- [GUI](#gui)
- [HTTP Clients](#http-clients)
- [Logging](#logging)
- [Package Management](#package-management)
- [Parsing](#parsing)
- [PreCompilation](#precompilation)
- [Search](#search)
- [Serialization](#serialization)
- [Search](#search)
- [Simulation](#simulation)
- [Testing](#testing)
- [Type providers](#type-providers)
Expand Down
63 changes: 63 additions & 0 deletions update-toc.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#r "nuget: Markdig, 0.38.0"

open System
open Markdig
open System.IO
open Markdig.Renderers.Roundtrip
open Markdig.Syntax

let readmeFilePath = Path.Combine(__SOURCE_DIRECTORY__, "README.md")

let markdown = File.ReadAllText(readmeFilePath)
let document = Markdown.Parse(markdown, trackTrivia = true)

let headers =
document.Descendants<HeadingBlock>()
|> Seq.skip 2 // The document header and the Table of Contents

let getLink(header: string) =
let characters =
header
|> Seq.choose(function
| c when Char.IsLetterOrDigit c -> Some(Char.ToLowerInvariant c)
| ' ' -> Some '-'
| _ -> None
)
|> Seq.toArray

"#" + String characters

let tocText =
"- [Awesome F#](#)\n" + (
headers
|> Seq.map(fun h ->
let text = h.Inline.FirstChild.ToString()
let indent = String(' ', (h.Level - 1) * 2)
$"{indent}- [{text}]({getLink text})"
)
|> String.concat "\n"
) + "\n\n"

let updateHeader (header: HeadingBlock) (newContent: string) =
let parent = header.Parent
let index = parent.IndexOf header
while not (parent[index + 1] :? HeadingBlock) do
parent.RemoveAt(index + 1)

let newMd = Markdown.Parse(newContent, trackTrivia = true)
for block in newMd do
block.Parent.Remove block |> ignore
parent.Insert(index + 1, block)

let firstHeader =
document.Descendants<HeadingBlock>()
|> Seq.find (fun h -> h.Level = 2)

updateHeader firstHeader tocText

File.WriteAllText(readmeFilePath,
use sw = new StringWriter()
let renderer = RoundtripRenderer sw
renderer.Write document
sw.ToString()
)

0 comments on commit 077585a

Please sign in to comment.