Repository navigation
Expand file tree
/
Copy pathinlineDocument.ts
More file actions
105 lines (93 loc) · 3.29 KB
/
Copy pathinlineDocument.ts
File metadata and controls
105 lines (93 loc) · 3.29 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inlineDocument.ts :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlesieur <dlesieur@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/11 21:03:00 by dlesieur #+# #+# */
/* Updated: 2026/05/11 21:03:59 by dlesieur ### ########.fr */
/* */
/* ************************************************************************** */
import type { InlineNode } from "./markdown/ast";
import { parseInline } from "./markdown/parser";
import {
renderInlineNodesToHtml,
type InlineHtmlOptions,
} from "./markdown/renderers/inlineHtml";
import { normalizeInlineNodes, serializeInlineNodes } from "./inlineAst";
import {
applyInlineFormattingToNodes,
type InlineFormattingCommand,
type InlineTextSelection,
} from "./inlineFormatting";
import {
applyInlineTextEditToNodes,
type InlineTextEditCommand,
} from "./inlineTextEditing";
export interface InlineDocumentEditResult {
doc: InlineDocument;
selection: InlineTextSelection;
}
export class InlineDocument {
private sourceCache: string | null;
private defaultHtmlCache: string | null = null;
private htmlOptionsCache: InlineHtmlOptions | null = null;
private htmlCache: string | null = null;
private constructor(
private readonly inlineNodes: InlineNode[],
sourceCache: string | null = null,
) {
this.sourceCache = sourceCache;
}
static fromSource(source: string): InlineDocument {
return new InlineDocument(normalizeInlineNodes(parseInline(source)), source);
}
applyFormatting(
selection: InlineTextSelection,
command: InlineFormattingCommand,
): InlineDocument {
const nextNodes = applyInlineFormattingToNodes(
this.inlineNodes,
selection,
command,
);
return nextNodes === this.inlineNodes ? this : new InlineDocument(nextNodes);
}
applyEdit(
selection: InlineTextSelection,
command: InlineTextEditCommand,
): InlineDocumentEditResult {
const result = applyInlineTextEditToNodes(
this.inlineNodes,
selection,
command,
);
return {
doc:
result.nodes === this.inlineNodes
? this
: new InlineDocument(result.nodes),
selection: result.selection,
};
}
toSource(): string {
this.sourceCache ??= serializeInlineNodes(this.inlineNodes);
return this.sourceCache;
}
toHtml(options?: InlineHtmlOptions): string {
if (!options) {
this.defaultHtmlCache ??= renderInlineNodesToHtml(this.inlineNodes);
return this.defaultHtmlCache;
}
if (this.htmlOptionsCache === options && this.htmlCache !== null) {
return this.htmlCache;
}
this.htmlOptionsCache = options;
this.htmlCache = renderInlineNodesToHtml(this.inlineNodes, options);
return this.htmlCache;
}
nodes(): readonly InlineNode[] {
return this.inlineNodes;
}
}