-
Notifications
You must be signed in to change notification settings - Fork 0
/
toMarkdown.ts
85 lines (72 loc) · 2.58 KB
/
toMarkdown.ts
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
import { FrontmatterParsers, parseFrontmatter } from "./frontmatter.ts"
type Directive = "hide-start" | "hide-end" | "hide-next-line"
const rDirective = /^\s*\/{2}\s*(?<directive>hide-start|hide-end|hide-next-line)(?:\s+.*)?$/
const rMarkdownLine = /^\s*\/{3} ?(?<content>.+)/
type Line =
| { kind: "markdown"; content: string }
| { kind: "code"; content: string }
| { kind: "directive"; directive: Directive }
| { kind: "blank"; content: "" }
export function toMarkdown<F extends Record<string, unknown>>(
pathname: string,
src: string,
parsers: FrontmatterParsers<F>,
) {
const { frontmatter, body } = parseFrontmatter(pathname, src, parsers)
const lines = body.split("\n").map((line): Line => {
if (line.trim() === "") return { kind: "blank", content: "" }
const directiveMatch = rDirective.exec(line)
if (directiveMatch) {
const directive = directiveMatch.groups!.directive! as Directive
return { kind: "directive", directive }
}
const markdownMatch = rMarkdownLine.exec(line)
if (markdownMatch) {
const content = markdownMatch.groups!.content!
return { kind: "markdown", content }
}
return { kind: "code", content: line }
})
const filteredLines = []
for (let i = 0; i < lines.length;) {
const line = lines[i]!
if (line.kind === "directive") {
if (line.directive === "hide-next-line") {
i += 2
} else if (line.directive === "hide-start") {
while (true) {
i++
const line = lines[i]
if (!line) throw new Error("unmatched hide-start comment")
if (line.kind === "directive" && line.directive === "hide-end") {
i++
break
}
}
} else if (line.directive === "hide-end") {
throw new Error("unmatched hide-end comment")
} else {
assertNever(line.directive)
}
} else {
filteredLines.push(line)
i++
}
}
type Section = { kind: "code" | "markdown"; lines: string[] }
const sections: Section[] = []
for (const line of filteredLines) {
if ((line.kind === "code" || line.kind === "markdown") && sections.at(-1)?.kind !== line.kind) {
sections.push({ kind: line.kind, lines: [] })
}
sections.at(-1)?.lines.push(line.content)
}
const content = sections.map((section) => {
const inner = section.lines.join("\n")
return section.kind === "code" ? `\`\`\`ts\n${inner}\`\`\`` : inner
}).join("\n\n")
return { frontmatter, content }
}
function assertNever(value: never) {
throw new Error(`Expected to never be called; got ${Deno.inspect(value)}`)
}