-
Notifications
You must be signed in to change notification settings - Fork 4
/
reflow.ts
367 lines (328 loc) · 10.5 KB
/
reflow.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { getLogger, Logger } from "log/mod.ts";
import {
isLiteralNode,
isParentNode,
newTextNode,
Node,
nodeLength,
NodeType,
ParentNode,
} from "../../markdown/ast.ts";
import { escapeContent } from "../../lib/diff.ts";
/**
* End of sentence is marked by a period, exclamation point, question mark, colon, or semicolon.
* Except for colon or semicolon, a final or preceding parenthesis or quote is allowed.
*
* If the word preceding the end-of-sentence punctuation is a character set of less than
* length 2, it's probably an acronym (like 'e.g.'), so it's not the end of a sentence.
*/
function isEndOfSentenceWord(word: string): boolean {
// To understand this regexp, pop it into https://regexr.com/
return !!word.match(
/(([a-zA-Z]{2,}|^)[.?!]['"’”)]?|['"’”)][.?!]|[:]) *$/,
);
}
/**
* Split text into words, treating all whitespace as a break. Leading and trailing
* spaces are preserved and go onto the first and last words.
*/
function splitWords(text: string): string[] {
const trimmed = text.trim();
if (trimmed === "") {
return [" "];
}
const words = trimmed.replace(/\s+/g, " ").split(" ");
if (text.match(/^\s+/)) {
// add back trimmed whitespace to start
words[0] = ` ${words[0]}`;
}
if (text.match(/\s+$/)) {
// add back trimmed whitespace to end
words[words.length - 1] = `${words[words.length - 1]} `;
}
return words;
}
/**
* ReflowParagraphState tracks the state of a paragraph being reflowed.
*/
class ReflowParagraphState {
log: Logger;
constructor(log: Logger) {
this.log = log;
}
/**
* Add text to the paragraph.
*
* @param text value of text node
* @param plainTree whether or not the text is inside another node
*/
addText(text: string, plainTree: boolean) {
this.currentLine = [];
this.lines = [this.currentLine];
const words = splitWords(text);
for (const [j, word] of words.entries()) {
if ((j > 0) || words.length === 1) {
this.breakAllowed = true;
}
this.appendWord(word, plainTree);
}
}
/**
* Add a node that should not be processed to have line breaks to the paragraph.
*
* @param node markdown AST node
* @returns whether or not a line break should follow
*/
addRawNode(node: Node) {
// Unbreakable nodes still count towards length of a line.
if (this.sentenceEnded) {
this.breakLine({ rawNode: true });
}
if (
!isLiteralNode(node) && isParentNode(node) && node.children.length == 1
) {
// If this is a parent node with no direct value, get the length of its child that
// represents the direct value.
const content = node.children[0];
this.currentLineColumn += nodeLength(content);
// Also strip line breaks from this child node's value, if possible, to normalize
// raw elements to have single lines.
if (isLiteralNode(content)) {
content.value = content.value.replaceAll("\n", " ");
}
} else {
this.currentLineColumn += nodeLength(node);
}
return this.sentenceEnded ? this.breakLine({ rawNode: true }) : false;
}
/**
* Export the current lines.
*/
render(opts?: { isLastChild?: boolean; isUnbreakable?: boolean }): string {
// If this is the last child, drop trailing empty line before rendering.
if (
opts?.isLastChild && this.lines.length > 1 &&
this.currentLine.length === 0
) {
this.lines.pop();
}
let rendered = this.lines.map((line) => line.join(" ")).join("\n");
// Preserve spacing if this is an unbreakable node.
if (opts?.isUnbreakable) {
rendered += " ";
}
this.log.debug(
`Rendered ${JSON.stringify(this.lines)} to '${escapeContent(rendered)}'`,
);
return rendered;
}
/**
* Set containing all processed lines within the paragraph
*/
private lines: string[][] = [];
/**
* Previous line's column position
*/
private previousLineColumn = 0;
/**
* Current line's column position
*/
private currentLineColumn = 0;
/**
* Words that make up the current line
*/
private currentLine: string[] = [];
/**
* Whether a break is allowed in the current line
*/
private breakAllowed = false;
/**
* Whether a sentence end has been detected in the current line
*/
private sentenceEnded = false;
/**
* Do a sentence wrap only after this column.
*/
private sentenceMinMargin = 45;
/**
* Returns simple representation of useful parts of the state.
*/
getState() {
return {
lineCount: this.lines.length,
previousColumn: this.previousLineColumn,
currentColumn: this.currentLineColumn,
breakAllowed: this.breakAllowed,
currentLine: JSON.stringify(this.currentLine),
};
}
/**
* Add a word to the current line.
*
* @param word individual word text
* @param isTreePlain whether this text belongs inside another node
*/
private appendWord(word: string, isTreePlain: boolean) {
this.currentLine.push(word);
this.currentLineColumn += word.length + 1;
if (isEndOfSentenceWord(word)) {
this.sentenceEnded = true;
this.breakLine({ isTreePlain });
} else if (this.sentenceEnded) {
// Adding a non-end-of-sentence after a sentence has ended
this.sentenceEnded = false;
}
}
/**
* Adds a line break.
*
* @param state information about the state
* @returns whether or not a line break was created before adding this node
*/
private breakLine(
state?: { isTreePlain?: boolean; rawNode?: boolean },
): boolean {
// If a node isPlain (i.e. not inside a strong/emphasis/link) it's fine to break
// immediately. If not (i.e. is strong/emphasis/link formatted), we can't break it on
// the first character, so have to wait until next opportunity.
const canBreakLine = state?.isTreePlain || this.currentLineColumn > 0;
// Decide a line break strategy if allowed.
if (this.breakAllowed && canBreakLine) {
// Prepare current line.
this.trimCurrentLine();
// Check if this sentence can be merged into the previous sentence instead, if a
// previous sentence has been tracked already.
const mergedLength = this.previousLineColumn + this.currentLineColumn;
const shouldMerge = this.lines.length >= 2 &&
mergedLength < this.sentenceMinMargin;
if (shouldMerge) {
const current = this.lines.pop();
const previous = this.lines.pop();
if (current && previous) {
this.log.debug("Breaking line by merging", {
current,
previous,
state,
});
this.lines.push([...previous, ...current]);
this.previousLineColumn = mergedLength;
} else {
throw new Error(
"Unexpected absence of current and/or previous when merging lines",
);
}
} else {
this.log.info("Breaking line by starting anew", { state });
this.previousLineColumn = this.currentLineColumn;
}
// Start collecting a new line, now that the current line has been handled.
// If this is a raw node, the current line hasn't actually been handled, so we don't
// reset the line yet.
if (!state?.rawNode) {
this.currentLine = [];
this.currentLineColumn = this.currentLine.length;
this.lines.push(this.currentLine); // ref to the new, blank line
}
this.breakAllowed = false; // do not break twice in a row
// Indicate a line was broken
return true;
}
return false;
}
/**
* Trim end off the current line.
*/
private trimCurrentLine() {
if (this.currentLine.length > 0) {
const length = this.currentLine.length - 1;
const last = this.currentLine[length];
this.currentLine[length] = last.trimEnd();
}
}
}
/**
* Reflow the top-level node containing a paragraph.
*
* @param paragraph
*/
function reflowParagraph(log: Logger, paragraph: ParentNode) {
const state = new ReflowParagraphState(log);
/**
* Visitor function for processing a tree of nodes that consist of parts of the paragraph.
*/
function processParent(tree: ParentNode, parentTypes: NodeType[]) {
const isTreePlain = parentTypes.length === 0;
let previous: Node | undefined;
// Process all text in this tree
for (let i = 0; i < tree.children.length; ++i) {
const isLastChild = i === (tree.children.length - 1);
const current = tree.children[i];
switch (current.type) {
// Normal text
case NodeType.Text:
// Ignore nodes that don't have a value for us to manipulate
if (!isLiteralNode(current)) {
continue;
}
state.addText(current.value, isTreePlain);
current.value = state.render({ isLastChild });
log.debug("Added text node", state.getState());
break;
// Unbreakable nodes
case NodeType.Link:
case NodeType.InlineCode: {
// Since we aren't just breaking the value of a text node, we need to make
// adjustments to the previous node or the tree.
const shouldBreakLine = state.addRawNode(current);
if (shouldBreakLine && previous) {
if (isLiteralNode(previous)) {
previous.value = state.render({
isLastChild,
isUnbreakable: true,
});
} else {
tree.children.splice(i, 0, newTextNode("\n", previous.position));
}
}
log.debug(
`Added unbreakable '${current.type}' node`,
state.getState(),
);
break;
}
// Unhandled nodes
default:
// Try to process
if (isParentNode(current)) {
log.debug(`Handling children of '${current.type}' node`);
processParent(current, parentTypes.concat(current.type));
} else {
log.debug(`Ignored '${current.type}' node`);
}
}
previous = current;
}
}
processParent(paragraph, []);
}
export default function reflow() {
const log = getLogger("reflow");
// Traverse tree looking for the right group of nodes to process
function visit(node: Node) {
if (!isParentNode(node)) {
return;
}
switch (node.type) {
case NodeType.Paragraph:
reflowParagraph(log, node);
break;
default:
node.children.forEach((child) => visit(child));
}
}
// Plugin function
return function transformer(node: Node) {
visit(node);
return node;
};
}