-
Notifications
You must be signed in to change notification settings - Fork 286
/
pandoc-partition.ts
111 lines (99 loc) · 3.02 KB
/
pandoc-partition.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
/*
* markdown.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { PandocAttr, PartitionedMarkdown } from "./types.ts";
import { lines } from "../text.ts";
import { partitionYamlFrontMatter, readYamlFromMarkdown } from "../yaml.ts";
import { pandocAttrParseText } from "./pandoc-attr.ts";
export function firstHeadingFromMarkdownFile(file: string): string | undefined {
return firstHeadingFromMarkdown(Deno.readTextFileSync(file));
}
export function firstHeadingFromMarkdown(markdown: string): string | undefined {
const partitioned = partitionMarkdown(markdown);
return partitioned.headingText;
}
const kPandocTitleRegex = /^\#{1,}\s(.*)\s\{(.*)\}$/;
const kRemoveHeadingRegex = /^#{1,}\s*/;
export function parsePandocTitle(title: string) {
// trim any whitespace
title = title ? title.trim() : title;
const match = title.match(kPandocTitleRegex);
if (match) {
const titleRaw = match[1];
const attrRaw = match[2];
const parsed = pandocAttrParseText(attrRaw);
if (parsed) {
return {
heading: titleRaw,
attr: parsed,
};
} else {
return {
heading: titleRaw,
};
}
} else {
return {
heading: title.replace(kRemoveHeadingRegex, "").trim(),
};
}
}
// partition markdown into yaml, the first heading, and the rest of the markdown text
export function partitionMarkdown(markdown: string): PartitionedMarkdown {
// partition out yaml
const partitioned = partitionYamlFrontMatter(markdown);
markdown = partitioned ? partitioned.markdown : markdown;
// extract heading
const { lines, headingText, headingAttr } = markdownWithExtractedHeading(
markdown,
);
// does this contain refs?
const containsRefs = lines.some((line) =>
/^:::\s*{#refs([\s}]|.*?})\s*$/.test(line)
);
return {
yaml: (partitioned ? readYamlFromMarkdown(partitioned.yaml) : undefined),
headingText,
headingAttr,
containsRefs,
markdown: lines.join("\n"),
srcMarkdownNoYaml: partitioned?.markdown || "",
};
}
export function markdownWithExtractedHeading(markdown: string) {
const mdLines: string[] = [];
let headingText: string | undefined;
let headingAttr: PandocAttr | undefined;
let contentBeforeHeading = false;
for (const line of lines(markdown)) {
if (!headingText) {
if (line.match(/^\#{1,}\s/)) {
const parsedHeading = parsePandocTitle(line);
headingText = parsedHeading.heading;
headingAttr = parsedHeading.attr;
contentBeforeHeading = mdLines.length !== 0;
} else if (line.match(/^=+\s*$/) || line.match(/^-+\s*$/)) {
const prevLine = mdLines[mdLines.length - 1];
if (prevLine) {
headingText = prevLine;
mdLines.splice(mdLines.length - 1);
contentBeforeHeading = mdLines.length !== 0;
} else {
mdLines.push(line);
}
} else {
mdLines.push(line);
}
} else {
mdLines.push(line);
}
}
return {
lines: mdLines,
headingText,
headingAttr,
contentBeforeHeading,
};
}