-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.ts
152 lines (133 loc) · 4.54 KB
/
parser.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {ContainerBlock, FootnotesAreaBlock, RootBlock} from './blocks/containerBlocks'
import {FootnoteDefBlock, FrontMatterBlock, HeadingBlock, LinkRefDefBlock} from './blocks/leafBlocks'
import {Block} from './blocks/block'
import {EscapeUtils, replaceAll} from './utils'
import {InlineNode} from './inlines/inlineNode'
import {getDefaultRenderOptions, RenderOptions} from './RenderOptions'
export type LinkReference = {
url: string,
title?: string,
}
export type FootnoteReference = {
index: number,
referencedPlaces: Array<{ refHref: string, defHref: string }>,
}
export class RenderContext {
parent: Block | InlineNode | null = null
stage: 'normal' | 'footnote'
footnoteReferences: Map<string, FootnoteReference>
constructor(
public linkReferences: Map<string, LinkReference>,
public renderOptions: RenderOptions,
public tocEntries: HeadingBlock[],
footnoteDefBlocks: FootnoteDefBlock[],
) {
this.footnoteReferences = new Map(footnoteDefBlocks.map((it, i) => [it.label, {
index: i + 1,
referencedPlaces: [],
}]))
}
}
export class TyporaParseResult {
ast: RootBlock
frontMatter: string | null = null
linkReferences: Map<string, LinkReference>
footnoteDefBlocks: FootnoteDefBlock[]
tocEntries: HeadingBlock[]
headings: HeadingBlock[]
private genHeadingIDs(context: RenderContext) {
// heading id to a list of headings have the same id
const buckets = new Map<string, HeadingBlock[]>()
for (const heading of this.headings) {
heading.genID(context)
const bucket = buckets.get(heading.id)
if (bucket) {
bucket.push(heading)
} else {
buckets.set(heading.id, [heading])
}
}
for (const [id, headings] of buckets.entries()) {
if (headings.length > 1) {
headings.forEach((heading, i) => {
heading.id = `${id}-${i + 1}`
})
}
}
}
renderHTML(options?: Partial<RenderOptions>): string {
const context = new RenderContext(this.linkReferences, Object.assign(getDefaultRenderOptions(), options), this.tocEntries, this.footnoteDefBlocks)
this.genHeadingIDs(context)
let html = this.ast.render(context)
html += new FootnotesAreaBlock(this.footnoteDefBlocks).render(context)
html = EscapeUtils.unEscapeMarkdown(html)
if (!context.renderOptions.vanillaHTML) {
html = `<div class='typora-export-content'>\n<div id='write' class=''>${html}</div></div>\n`
}
if (context.renderOptions.includeHead) {
const titleHtml = context.renderOptions.title === null ? '' : `<title>${context.renderOptions.title}</title>`
if (context.renderOptions.vanillaHTML) {
html = `<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
${context.renderOptions.extraHeadTags ?? ''}
${titleHtml}
</head>
<body>${html}</body>
</html>`
} else {
html = `<!doctype html>
<html>
<head>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
${context.renderOptions.extraHeadTags ?? ''}
${titleHtml}
</head>
<body class='typora-export'>${html}</body>
</html>`
}
}
return html
}
}
const newLineRegex = /\r\n|\n/
export function parse(markdown: string): TyporaParseResult {
markdown = replaceAll(markdown, '\u0000', '\uFFFD')
markdown = EscapeUtils.escapeMarkdown(markdown)
let lines = markdown.split(newLineRegex)
const {frontMatter, remaining} = FrontMatterBlock.process(lines)
lines = remaining
const rootBlock = new RootBlock(lines)
rootBlock.close()
const linkReferences = new Map<string, LinkReference>()
const footnoteDefBlocks: FootnoteDefBlock[] = []
const headings: HeadingBlock[] = []
{
const walk = function (block: Block) {
if (block instanceof LinkRefDefBlock) {
linkReferences.set(block.label, block.def)
} else if (block instanceof FootnoteDefBlock) {
footnoteDefBlocks.push(block)
} else if (block instanceof HeadingBlock) {
headings.push(block)
} else if (block instanceof ContainerBlock) {
for (const child of block.children) {
walk(child)
}
}
}
walk(rootBlock)
}
const tocEntries = rootBlock.children.filter(it => it instanceof HeadingBlock) as HeadingBlock[]
const result = new TyporaParseResult()
result.ast = rootBlock
result.linkReferences = linkReferences
result.headings = headings
result.tocEntries = tocEntries
result.footnoteDefBlocks = footnoteDefBlocks
if (frontMatter) {
result.frontMatter = frontMatter.lines.join('\n')
}
return result
}