-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutline.ts
212 lines (181 loc) · 5.67 KB
/
outline.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
interface Chunk {
type: "frontmatter" | "heading" | "paragraph" | "list";
content: string;
level: number;
}
export function splitIntoChunks(markdown: string): Chunk[] {
const lines = markdown.trim().split("\n");
const chunks: Chunk[] = [];
let currentChunk: string[] = [];
let currentType: Chunk["type"] | null = null;
let inCodeBlock = false;
let inBlockQuote = 0;
let isUnbreakableBlock = false;
function commitChunk() {
if (currentChunk.length > 0 && currentType) {
const content = currentChunk.join("\n");
const level = currentType === "heading"
? content.match(/^(#+)/)?.[0].length || 0
: 0;
chunks.push({ type: currentType, content, level });
currentChunk = [];
// console.log("committing chunk", currentType, "\n", content, "\n");
currentType = null;
}
}
for (const line of lines) {
const frontmatterDelimiter = line.match(/^---$/);
const isCodeBlockDelimiter = line.trim().startsWith("```");
const isBlockStart = line.trim().startsWith("#+BEGIN_");
const isBlockEnd = line.trim().startsWith("#+END_");
// If the first chunk is a frontmatter block, pass through it and commit as-is
if (!chunks.length && !currentType && frontmatterDelimiter) {
currentType = "frontmatter";
continue;
}
// If we're in a frontmatter block, keep adding lines until we hit the end
if (currentType === "frontmatter") {
if (frontmatterDelimiter) {
commitChunk();
} else {
currentChunk.push(line);
}
continue;
}
if (!isUnbreakableBlock && !line.trim()) {
if (currentType !== "list") {
commitChunk();
}
// currentChunk.push(line);
continue;
}
// Detect chunk type
const isHeading = line.match(/^#+\s/);
const isList = line.match(/^(-|\d+\.)\s/);
const isParagraph = line.match(/^\S/) && !isHeading && !isList;
const newType: Chunk["type"] | null = isHeading
? "heading"
: isList
? "list"
: isParagraph
? "paragraph"
: null;
// console.log(newType, ": ", line);
// Detect code block start/end
if (isCodeBlockDelimiter) {
inCodeBlock = !inCodeBlock;
}
// Detect unbreakable block start/end
if (isBlockStart) {
inBlockQuote = Math.max(inBlockQuote + 1, 1);
} else if (isBlockEnd) {
inBlockQuote = Math.max(inBlockQuote - 1, 0);
}
isUnbreakableBlock = inBlockQuote > 0 || inCodeBlock;
const isListEnd = !isList && currentType === "list";
if (isUnbreakableBlock && !isListEnd) {
const l = line.trim() ? line : "";
currentChunk.push(l);
continue;
}
// If this is a new chunk type, commit the previous chunk (always commit headings)
if (
newType && newType !== currentType ||
currentType === "heading"
) {
commitChunk();
currentType = newType;
}
// Start a new list chunk or continue existing one
if (isList && !currentType) {
currentType = "list";
}
// Add line to current chunk
if (currentType) {
currentChunk.push(line);
}
}
commitChunk();
return chunks;
}
export type OutlineOptions = {
listNesting: "none" | "paragraph" | "separate";
};
export function outlineChunks(
chunks: Chunk[],
options: OutlineOptions,
): string {
const lines: string[] = [];
const headingStack: number[] = [0];
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
if (chunk.type === "frontmatter") {
lines.push(chunk.content);
// Add a blank line after the frontmatter block
lines.push("");
continue;
}
if (chunk.type === "heading") {
// Adjust heading stack based on heading level
while (headingStack[headingStack.length - 1] >= chunk.level) {
headingStack.pop();
}
headingStack.push(chunk.level);
}
// Calculate indent level (subtract 1 to avoid double-counting the root level)
const headingLevel = headingStack.length - 1;
const indentLevel = chunk.type === "heading"
? headingLevel - 1
: headingLevel;
const indent = " ".repeat(indentLevel);
// Handle list nesting options
let listNestingIndent = "";
if (chunk.type === "list") {
const isFollowingParagraph = i > 0 && chunks[i - 1].type === "paragraph";
if (options.listNesting === "paragraph" && isFollowingParagraph) {
listNestingIndent = " ";
} else if (options.listNesting === "separate") {
// Add a single empty item before the list
lines.push(indent + "-");
listNestingIndent = " ";
}
}
// Add the chunk with proper indentation
const chunkLines = chunk.content.split("\n");
chunkLines.forEach((line, index) => {
if (!line.trim()) {
lines.push("");
} else {
const prefix = index === 0 && chunk.type !== "list"
? "- "
: chunk.type === "paragraph"
? " "
: "";
lines.push(indent + prefix + listNestingIndent + line);
}
});
}
return lines.join("\n");
}
export function outlineMarkdown(
markdown: string,
options: OutlineOptions = {
listNesting: "none",
},
): string {
const chunks = splitIntoChunks(markdown);
return outlineChunks(chunks, options);
}
if (import.meta.main) { // CLI
const inputFile = Deno.args[0];
const outputFile = Deno.args[1];
if (!inputFile || !outputFile) {
console.log(
"Usage: deno run --allow-read --allow-write outliner.ts <input-file> <output-file>",
);
Deno.exit(1);
}
const content = await Deno.readTextFile(inputFile);
const outlined = outlineMarkdown(content, { listNesting: "paragraph" });
await Deno.writeTextFile(outputFile, outlined);
}