MapComplete/src/UI/Base/TableOfContents.ts

121 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import BaseUIElement from "../BaseUIElement"
import List from "./List"
import { marked } from "marked"
import { parse as parse_html } from "node-html-parser"
import { default as turndown } from "turndown"
2022-09-08 21:40:48 +02:00
import { Utils } from "../../Utils"
2021-11-30 22:50:48 +01:00
export default class TableOfContents {
2022-01-18 18:52:42 +01:00
2021-11-30 22:50:48 +01:00
private static asLinkableId(text: string): string {
return text
?.replace(/ /g, "-")
?.replace(/[?#.;:/]/, "")
?.toLowerCase() ?? ""
2021-11-30 22:50:48 +01:00
}
2022-01-18 18:52:42 +01:00
2022-09-08 21:40:48 +02:00
private static mergeLevel(
elements: { level: number; content: BaseUIElement }[]
): BaseUIElement[] {
const maxLevel = Math.max(...elements.map((e) => e.level))
const minLevel = Math.min(...elements.map((e) => e.level))
2021-11-30 22:50:48 +01:00
if (maxLevel === minLevel) {
2022-09-08 21:40:48 +02:00
return elements.map((e) => e.content)
2021-11-30 22:50:48 +01:00
}
2022-09-08 21:40:48 +02:00
const result: { level: number; content: BaseUIElement }[] = []
2021-11-30 22:50:48 +01:00
let running: BaseUIElement[] = []
for (const element of elements) {
if (element.level === maxLevel) {
running.push(element.content)
continue
}
if (running.length !== undefined) {
result.push({
content: new List(running),
level: maxLevel - 1
2021-11-30 22:50:48 +01:00
})
running = []
}
result.push(element)
}
if (running.length !== undefined) {
result.push({
content: new List(running),
level: maxLevel - 1
2021-11-30 22:50:48 +01:00
})
}
return TableOfContents.mergeLevel(result)
}
public static insertTocIntoMd(md: string): string {
const htmlSource = <string>marked.parse(md)
const el = parse_html(htmlSource)
const structure = TableOfContents.generateStructure(<any>el)
const firstTitle = structure[1]
let minDepth = undefined
do {
minDepth = Math.min(...structure.map(s => s.depth))
const minDepthCount = structure.filter(s => s.depth === minDepth)
if (minDepthCount.length > 1) {
break
}
// Erase a single top level heading
structure.splice(structure.findIndex(s => s.depth === minDepth), 1)
} while (structure.length > 0)
if (structure.length <= 1) {
return md
2021-11-30 22:50:48 +01:00
}
const separators = {
1: " -",
2: " +",
3: " *"
}
let toc = ""
let topLevelCount = 0
for (const el of structure) {
const depthDiff = el.depth - minDepth
const link = `[${el.title}](#${TableOfContents.asLinkableId(el.title)})`
if (depthDiff === 0) {
topLevelCount++
toc += `${topLevelCount}. ${link}\n`
} else if (depthDiff <= 3) {
toc += `${separators[depthDiff]} ${link}\n`
}
}
const heading = Utils.Times(() => "#", firstTitle.depth)
toc = heading + " Table of contents\n\n" + toc
2022-01-18 18:52:42 +01:00
const firstTitleIndex = md.indexOf(firstTitle.title)
const intro = md.substring(0, firstTitleIndex)
const splitPoint = intro.lastIndexOf("\n")
return md.substring(0, splitPoint) + toc + md.substring(splitPoint)
}
public static generateStructure(html: Element): { depth: number, title: string, el: Element }[] {
if (html === undefined) {
return []
}
return [].concat(...Array.from(html.childNodes ?? []).map(
child => {
const tag: string = child["tagName"]?.toLowerCase()
if (!tag) {
return []
}
if (tag.match(/h[0-9]/)) {
const depth = Number(tag.substring(1))
return [{ depth, title: child.textContent, el: child }]
}
return TableOfContents.generateStructure(<Element>child)
}
))
2021-11-30 22:50:48 +01:00
}
2022-09-08 21:40:48 +02:00
}