@valkyrianlabs/payload-markdown 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +222 -0
- package/dist/blocks/MarkdownBlock/Component.d.ts +2 -0
- package/dist/blocks/MarkdownBlock/Component.js +13 -0
- package/dist/blocks/MarkdownBlock/Component.js.map +1 -0
- package/dist/blocks/MarkdownBlock/config.d.ts +2 -0
- package/dist/blocks/MarkdownBlock/config.js +20 -0
- package/dist/blocks/MarkdownBlock/config.js.map +1 -0
- package/dist/blocks/MarkdownBlock/types.d.js +3 -0
- package/dist/blocks/MarkdownBlock/types.d.js.map +1 -0
- package/dist/components/MarkdownRenderer/Component.client.d.ts +5 -0
- package/dist/components/MarkdownRenderer/Component.client.js +163 -0
- package/dist/components/MarkdownRenderer/Component.client.js.map +1 -0
- package/dist/components/MarkdownRenderer/Component.d.ts +3 -0
- package/dist/components/MarkdownRenderer/Component.js +87 -0
- package/dist/components/MarkdownRenderer/Component.js.map +1 -0
- package/dist/components/MarkdownRenderer/index.css +69 -0
- package/dist/components/MarkdownRenderer/index.module.css +45 -0
- package/dist/components/MarkdownRenderer/index.module.scss +43 -0
- package/dist/components/MarkdownRenderer/index.scss +83 -0
- package/dist/components/MarkdownRenderer/types.d.js +5 -0
- package/dist/components/MarkdownRenderer/types.d.js.map +1 -0
- package/dist/core/codeConfig.d.ts +6 -0
- package/dist/core/codeConfig.js +45 -0
- package/dist/core/codeConfig.js.map +1 -0
- package/dist/core/codeToHtml.d.ts +5 -0
- package/dist/core/codeToHtml.js +180 -0
- package/dist/core/codeToHtml.js.map +1 -0
- package/dist/core/plugins/rehypeApplyLayoutClasses.d.ts +4 -0
- package/dist/core/plugins/rehypeApplyLayoutClasses.js +72 -0
- package/dist/core/plugins/rehypeApplyLayoutClasses.js.map +1 -0
- package/dist/core/plugins/rehypeStripAuthoredInlineStyles.d.ts +3 -0
- package/dist/core/plugins/rehypeStripAuthoredInlineStyles.js +17 -0
- package/dist/core/plugins/rehypeStripAuthoredInlineStyles.js.map +1 -0
- package/dist/core/plugins/remarkCompileLayouts.d.ts +3 -0
- package/dist/core/plugins/remarkCompileLayouts.js +169 -0
- package/dist/core/plugins/remarkCompileLayouts.js.map +1 -0
- package/dist/core/plugins/remarkHeadingAnchorsAndToc.d.ts +3 -0
- package/dist/core/plugins/remarkHeadingAnchorsAndToc.js +68 -0
- package/dist/core/plugins/remarkHeadingAnchorsAndToc.js.map +1 -0
- package/dist/core/plugins/remarkLayoutDirectives.d.ts +3 -0
- package/dist/core/plugins/remarkLayoutDirectives.js +26 -0
- package/dist/core/plugins/remarkLayoutDirectives.js.map +1 -0
- package/dist/core/plugins/remarkLayoutSentinels.d.ts +3 -0
- package/dist/core/plugins/remarkLayoutSentinels.js +184 -0
- package/dist/core/plugins/remarkLayoutSentinels.js.map +1 -0
- package/dist/core/plugins/remarkLiftLayoutDirectives.d.ts +3 -0
- package/dist/core/plugins/remarkLiftLayoutDirectives.js +93 -0
- package/dist/core/plugins/remarkLiftLayoutDirectives.js.map +1 -0
- package/dist/core/plugins/remarkNormalizeLayoutSyntax.d.ts +3 -0
- package/dist/core/plugins/remarkValidateDirectiveThemes.d.ts +4 -0
- package/dist/core/plugins/remarkValidateDirectiveThemes.js +28 -0
- package/dist/core/plugins/remarkValidateDirectiveThemes.js.map +1 -0
- package/dist/core/renderMarkdown.d.ts +2 -0
- package/dist/core/renderMarkdown.js +270 -0
- package/dist/core/renderMarkdown.js.map +1 -0
- package/dist/core/types.d.js +7 -0
- package/dist/core/types.d.js.map +1 -0
- package/dist/core/types.d.ts +238 -0
- package/dist/core/types.js +5 -0
- package/dist/core/types.js.map +1 -0
- package/dist/directives/attributes.d.ts +14 -0
- package/dist/directives/attributes.js +121 -0
- package/dist/directives/attributes.js.map +1 -0
- package/dist/directives/definitions/callout.d.ts +6 -0
- package/dist/directives/definitions/callout.js +107 -0
- package/dist/directives/definitions/callout.js.map +1 -0
- package/dist/directives/definitions/card.d.ts +4 -0
- package/dist/directives/definitions/card.js +120 -0
- package/dist/directives/definitions/card.js.map +1 -0
- package/dist/directives/definitions/cards.d.ts +7 -0
- package/dist/directives/definitions/cards.js +72 -0
- package/dist/directives/definitions/cards.js.map +1 -0
- package/dist/directives/definitions/cell.d.ts +4 -0
- package/dist/directives/definitions/cell.js +44 -0
- package/dist/directives/definitions/cell.js.map +1 -0
- package/dist/directives/definitions/columns.d.ts +4 -0
- package/dist/directives/definitions/columns.js +89 -0
- package/dist/directives/definitions/columns.js.map +1 -0
- package/dist/directives/definitions/details.d.ts +2 -0
- package/dist/directives/definitions/details.js +76 -0
- package/dist/directives/definitions/details.js.map +1 -0
- package/dist/directives/definitions/section.d.ts +2 -0
- package/dist/directives/definitions/section.js +32 -0
- package/dist/directives/definitions/section.js.map +1 -0
- package/dist/directives/definitions/steps.d.ts +2 -0
- package/dist/directives/definitions/steps.js +242 -0
- package/dist/directives/definitions/steps.js.map +1 -0
- package/dist/directives/definitions/tab.d.ts +8 -0
- package/dist/directives/definitions/tab.js +59 -0
- package/dist/directives/definitions/tab.js.map +1 -0
- package/dist/directives/definitions/tabs.d.ts +2 -0
- package/dist/directives/definitions/tabs.js +197 -0
- package/dist/directives/definitions/tabs.js.map +1 -0
- package/dist/directives/definitions/toc.d.ts +7 -0
- package/dist/directives/definitions/toc.js +59 -0
- package/dist/directives/definitions/toc.js.map +1 -0
- package/dist/directives/diagnostics.d.ts +8 -0
- package/dist/directives/diagnostics.js +167 -0
- package/dist/directives/diagnostics.js.map +1 -0
- package/dist/directives/headingAnchors.d.ts +9 -0
- package/dist/directives/headingAnchors.js +54 -0
- package/dist/directives/headingAnchors.js.map +1 -0
- package/dist/directives/index.d.ts +5 -0
- package/dist/directives/index.js +6 -0
- package/dist/directives/index.js.map +1 -0
- package/dist/directives/registry.d.ts +20 -0
- package/dist/directives/registry.js +152 -0
- package/dist/directives/registry.js.map +1 -0
- package/dist/directives/renderData.d.ts +3 -0
- package/dist/directives/renderData.js +11 -0
- package/dist/directives/renderData.js.map +1 -0
- package/dist/directives/themes.d.ts +25 -0
- package/dist/directives/themes.js +274 -0
- package/dist/directives/themes.js.map +1 -0
- package/dist/directives/types.d.ts +72 -0
- package/dist/directives/types.js +3 -0
- package/dist/directives/types.js.map +1 -0
- package/dist/editor/MarkdownCodeMirror/Component.client.d.ts +8 -0
- package/dist/editor/MarkdownCodeMirror/Component.client.js +78 -0
- package/dist/editor/MarkdownCodeMirror/Component.client.js.map +1 -0
- package/dist/editor/MarkdownCodeMirror/Component.d.ts +9 -0
- package/dist/editor/MarkdownCodeMirror/Component.js +10 -0
- package/dist/editor/MarkdownCodeMirror/Component.js.map +1 -0
- package/dist/editor/MarkdownCodeMirror.d.ts +8 -0
- package/dist/editor/MarkdownCodeMirror.js +74 -0
- package/dist/editor/MarkdownCodeMirror.js.map +1 -0
- package/dist/editor/MarkdownEditor.d.ts +10 -0
- package/dist/editor/MarkdownEditor.js +22 -0
- package/dist/editor/MarkdownEditor.js.map +1 -0
- package/dist/editor/directives/completions.d.ts +5 -0
- package/dist/editor/directives/completions.js +93 -0
- package/dist/editor/directives/completions.js.map +1 -0
- package/dist/editor/directives/diagnostics.d.ts +1 -0
- package/dist/editor/directives/diagnostics.js +12 -0
- package/dist/editor/directives/diagnostics.js.map +1 -0
- package/dist/editor/themes/payload.d.ts +27 -0
- package/dist/editor/themes/payload.js +269 -0
- package/dist/editor/themes/payload.js.map +1 -0
- package/dist/editor/themes/support/highlighters.d.ts +20 -0
- package/dist/editor/themes/support/highlighters.js +1141 -0
- package/dist/editor/themes/support/highlighters.js.map +1 -0
- package/dist/editor/themes/support/lang.d.ts +40 -0
- package/dist/editor/themes/support/lang.js +201 -0
- package/dist/editor/themes/support/lang.js.map +1 -0
- package/dist/exports/advanced.d.ts +3 -0
- package/dist/exports/advanced.js +5 -0
- package/dist/exports/advanced.js.map +1 -0
- package/dist/exports/client.d.ts +1 -0
- package/dist/exports/client.js +2 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/server.d.ts +3 -0
- package/dist/exports/server.js +5 -0
- package/dist/exports/server.js.map +1 -0
- package/dist/field/BlocksParams/config.d.ts +7 -0
- package/dist/field/BlocksParams/config.js +149 -0
- package/dist/field/BlocksParams/config.js.map +1 -0
- package/dist/field/CodeBlock/config.d.ts +7 -0
- package/dist/field/CodeBlock/config.js +321 -0
- package/dist/field/CodeBlock/config.js.map +1 -0
- package/dist/field/CodeBlockConfig/config.d.ts +7 -0
- package/dist/field/CodeBlockConfig/config.js +306 -0
- package/dist/field/CodeBlockConfig/config.js.map +1 -0
- package/dist/field/CodeBlockParams/config.d.ts +7 -0
- package/dist/field/CodeBlockParams/config.js +321 -0
- package/dist/field/CodeBlockParams/config.js.map +1 -0
- package/dist/field/Config/config.d.ts +7 -0
- package/dist/field/Config/config.js +149 -0
- package/dist/field/Config/config.js.map +1 -0
- package/dist/field/MarkdownField/Component.d.ts +2 -0
- package/dist/field/MarkdownField/Component.js +42 -0
- package/dist/field/MarkdownField/Component.js.map +1 -0
- package/dist/field/MarkdownField/config.d.ts +3 -0
- package/dist/field/MarkdownField/config.js +20 -0
- package/dist/field/MarkdownField/config.js.map +1 -0
- package/dist/field/Tailwind/config.d.ts +9 -0
- package/dist/field/Tailwind/config.js +13 -0
- package/dist/field/Tailwind/config.js.map +1 -0
- package/dist/field/TailwindField/config.d.ts +9 -0
- package/dist/field/TailwindField/config.js +13 -0
- package/dist/field/TailwindField/config.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime/index.d.ts +36 -0
- package/dist/runtime/index.js +124 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/styles/directiveSurface.d.ts +11 -0
- package/dist/styles/directiveSurface.js +38 -0
- package/dist/styles/directiveSurface.js.map +1 -0
- package/dist/types/core.d.ts +285 -0
- package/dist/types/core.js +5 -0
- package/dist/types/core.js.map +1 -0
- package/dist/types/layoutToken.d.ts +1 -0
- package/dist/types/layoutToken.js +3 -0
- package/dist/types/layoutToken.js.map +1 -0
- package/dist/types/mdast.d.js +5 -0
- package/dist/types/mdast.d.js.map +1 -0
- package/dist/types.d.js +3 -0
- package/dist/types.d.js.map +1 -0
- package/dist/types.d.ts +80 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +181 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { layoutDirectiveRegistry } from '../../directives/registry.js';
|
|
2
|
+
function isHeading(node) {
|
|
3
|
+
return node.type === 'heading';
|
|
4
|
+
}
|
|
5
|
+
function isLayoutToken(node) {
|
|
6
|
+
return node.type === 'vlLayoutToken';
|
|
7
|
+
}
|
|
8
|
+
function isAppendableRootContent(node) {
|
|
9
|
+
return node.type !== 'vlLayoutToken';
|
|
10
|
+
}
|
|
11
|
+
function makeDirective(name, attributes = {}, parentHeadingDepth, cellHeadingDepth) {
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
type: 'containerDirective',
|
|
15
|
+
attributes: Object.fromEntries(Object.entries(attributes).map(([key, value])=>[
|
|
16
|
+
key,
|
|
17
|
+
String(value)
|
|
18
|
+
])),
|
|
19
|
+
children: [],
|
|
20
|
+
data: {
|
|
21
|
+
vlCellHeadingDepth: cellHeadingDepth,
|
|
22
|
+
vlParentHeadingDepth: parentHeadingDepth
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function getChildren(node) {
|
|
27
|
+
return node.children;
|
|
28
|
+
}
|
|
29
|
+
function top(arr) {
|
|
30
|
+
return arr[arr.length - 1];
|
|
31
|
+
}
|
|
32
|
+
function isGridName(name) {
|
|
33
|
+
return layoutDirectiveRegistry.isGridName(name);
|
|
34
|
+
}
|
|
35
|
+
function findNearestSectionIndex(stack) {
|
|
36
|
+
for(let i = stack.length - 1; i >= 0; --i)if (stack[i]?.name === 'section') return i;
|
|
37
|
+
return -1;
|
|
38
|
+
}
|
|
39
|
+
function findNearestGridIndex(stack) {
|
|
40
|
+
for(let i = stack.length - 1; i >= 0; --i)if (isGridName(stack[i]?.name)) return i;
|
|
41
|
+
return -1;
|
|
42
|
+
}
|
|
43
|
+
function closeTop(stack) {
|
|
44
|
+
if (stack.length > 1) stack.pop();
|
|
45
|
+
}
|
|
46
|
+
function closeActiveGrid(stack) {
|
|
47
|
+
const gridIndex = findNearestGridIndex(stack);
|
|
48
|
+
if (gridIndex < 0) return false;
|
|
49
|
+
while(stack.length - 1 >= gridIndex)stack.pop();
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
function closeActiveGridInsideSection(stack) {
|
|
53
|
+
const gridIndex = findNearestGridIndex(stack);
|
|
54
|
+
const sectionIndex = findNearestSectionIndex(stack);
|
|
55
|
+
if (gridIndex < 0 || sectionIndex < 0) return false;
|
|
56
|
+
if (gridIndex < sectionIndex) return false;
|
|
57
|
+
while(stack.length - 1 >= gridIndex)stack.pop();
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
function closeThroughSection(stack) {
|
|
61
|
+
const sectionIndex = findNearestSectionIndex(stack);
|
|
62
|
+
if (sectionIndex < 0) return false;
|
|
63
|
+
while(stack.length - 1 >= sectionIndex)stack.pop();
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
export const remarkCompileLayouts = ()=>{
|
|
67
|
+
return (tree, file)=>{
|
|
68
|
+
const input = [
|
|
69
|
+
...tree.children
|
|
70
|
+
];
|
|
71
|
+
const rebuiltRoot = {
|
|
72
|
+
...tree,
|
|
73
|
+
children: []
|
|
74
|
+
};
|
|
75
|
+
const stack = [
|
|
76
|
+
{
|
|
77
|
+
name: 'root',
|
|
78
|
+
node: rebuiltRoot
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
const warnings = [];
|
|
82
|
+
let currentHeadingDepth;
|
|
83
|
+
const append = (node)=>getChildren(top(stack).node).push(node);
|
|
84
|
+
for (const node of input){
|
|
85
|
+
if (isLayoutToken(node)) {
|
|
86
|
+
if (node.action === 'open') {
|
|
87
|
+
const definition = layoutDirectiveRegistry.get(node.name);
|
|
88
|
+
if (definition?.kind === 'section') {
|
|
89
|
+
const next = makeDirective('section', node.attributes);
|
|
90
|
+
append(next);
|
|
91
|
+
stack.push({
|
|
92
|
+
name: 'section',
|
|
93
|
+
node: next
|
|
94
|
+
});
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (definition?.kind === 'cell') {
|
|
98
|
+
const next = makeDirective('cell', node.attributes);
|
|
99
|
+
append(next);
|
|
100
|
+
stack.push({
|
|
101
|
+
name: 'cell',
|
|
102
|
+
node: next
|
|
103
|
+
});
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (definition?.kind !== 'grid') {
|
|
107
|
+
const next = makeDirective(node.name, node.attributes);
|
|
108
|
+
append(next);
|
|
109
|
+
stack.push({
|
|
110
|
+
name: node.name,
|
|
111
|
+
node: next
|
|
112
|
+
});
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
closeActiveGridInsideSection(stack);
|
|
116
|
+
const parentDepth = currentHeadingDepth ?? 1;
|
|
117
|
+
const cellDepth = parentDepth + 1;
|
|
118
|
+
const next = makeDirective(node.name, node.attributes, parentDepth, cellDepth);
|
|
119
|
+
append(next);
|
|
120
|
+
stack.push({
|
|
121
|
+
name: node.name,
|
|
122
|
+
cellHeadingDepth: cellDepth,
|
|
123
|
+
node: next,
|
|
124
|
+
parentHeadingDepth: parentDepth
|
|
125
|
+
});
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (node.action === 'close') {
|
|
129
|
+
if (stack.length === 1) {
|
|
130
|
+
warnings.push('Encountered ::: with no open layout block.');
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
closeTop(stack);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (node.action === 'closeGrid') {
|
|
137
|
+
if (!closeActiveGrid(stack)) {
|
|
138
|
+
warnings.push('Encountered :::endcol with no open grid.');
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
if (node.action === 'closeSection') {
|
|
144
|
+
if (!closeThroughSection(stack)) warnings.push('Encountered :::end or :::endsection with no open section.');
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (isHeading(node)) {
|
|
149
|
+
const currentFrame = top(stack);
|
|
150
|
+
if (isGridName(currentFrame.name)) {
|
|
151
|
+
const parentDepth = currentFrame.parentHeadingDepth;
|
|
152
|
+
if (typeof parentDepth === 'number' && node.depth < parentDepth) closeTop(stack);
|
|
153
|
+
}
|
|
154
|
+
currentHeadingDepth = node.depth;
|
|
155
|
+
append(node);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (isAppendableRootContent(node)) append(node);
|
|
159
|
+
}
|
|
160
|
+
while(stack.length > 1){
|
|
161
|
+
warnings.push(`Auto-closing unclosed layout block: ${top(stack).name}`);
|
|
162
|
+
stack.pop();
|
|
163
|
+
}
|
|
164
|
+
tree.children = rebuiltRoot.children;
|
|
165
|
+
for (const warning of warnings)file.message(warning);
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
//# sourceMappingURL=remarkCompileLayouts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/plugins/remarkCompileLayouts.ts"],"sourcesContent":["import type { Heading, Root, RootContent } from 'mdast'\nimport type { ContainerDirective } from 'mdast-util-directive'\nimport type { Plugin } from 'unified'\n\nimport type { LayoutName, LayoutToken } from '../../directives/types.js'\n\nimport { layoutDirectiveRegistry } from '../../directives/registry.js'\n\ntype LayoutNode = ContainerDirective | Root\n\ntype AppendableRootContent = Exclude<RootContent, LayoutToken>\n\ntype LayoutFrame = {\n cellHeadingDepth?: number\n name: 'root' | LayoutName\n node: LayoutNode\n parentHeadingDepth?: number\n}\n\nfunction isHeading(node: RootContent): node is Heading {\n return node.type === 'heading'\n}\n\nfunction isLayoutToken(node: RootContent): node is LayoutToken {\n return node.type === 'vlLayoutToken'\n}\n\nfunction isAppendableRootContent(node: RootContent): node is AppendableRootContent {\n return node.type !== 'vlLayoutToken'\n}\n\nfunction makeDirective(\n name: LayoutName,\n attributes: Record<string, boolean | string> = {},\n parentHeadingDepth?: number,\n cellHeadingDepth?: number,\n): ContainerDirective {\n return {\n name,\n type: 'containerDirective',\n attributes: Object.fromEntries(\n Object.entries(attributes).map(([key, value]) => [key, String(value)]),\n ),\n children: [],\n data: {\n vlCellHeadingDepth: cellHeadingDepth,\n vlParentHeadingDepth: parentHeadingDepth,\n },\n }\n}\n\nfunction getChildren(node: LayoutNode): AppendableRootContent[] {\n return node.children as AppendableRootContent[]\n}\n\nfunction top<T>(arr: T[]): T {\n return arr[arr.length - 1]\n}\n\nfunction isGridName(name: LayoutFrame['name']): name is '2col' | '3col' {\n return layoutDirectiveRegistry.isGridName(name)\n}\n\nfunction findNearestSectionIndex(stack: LayoutFrame[]): number {\n for (let i = stack.length - 1; i >= 0; --i) if (stack[i]?.name === 'section') return i\n\n return -1\n}\n\nfunction findNearestGridIndex(stack: LayoutFrame[]): number {\n for (let i = stack.length - 1; i >= 0; --i) if (isGridName(stack[i]?.name)) return i\n\n return -1\n}\n\nfunction closeTop(stack: LayoutFrame[]) {\n if (stack.length > 1) stack.pop()\n}\n\nfunction closeActiveGrid(stack: LayoutFrame[]): boolean {\n const gridIndex = findNearestGridIndex(stack)\n if (gridIndex < 0) return false\n\n while (stack.length - 1 >= gridIndex) stack.pop()\n\n return true\n}\n\nfunction closeActiveGridInsideSection(stack: LayoutFrame[]): boolean {\n const gridIndex = findNearestGridIndex(stack)\n const sectionIndex = findNearestSectionIndex(stack)\n\n if (gridIndex < 0 || sectionIndex < 0) return false\n if (gridIndex < sectionIndex) return false\n\n while (stack.length - 1 >= gridIndex) stack.pop()\n\n return true\n}\n\nfunction closeThroughSection(stack: LayoutFrame[]): boolean {\n const sectionIndex = findNearestSectionIndex(stack)\n if (sectionIndex < 0) return false\n\n while (stack.length - 1 >= sectionIndex) stack.pop()\n\n return true\n}\n\nexport const remarkCompileLayouts: Plugin<[], Root> = () => {\n return (tree, file) => {\n const input = [...tree.children]\n const rebuiltRoot: Root = { ...tree, children: [] }\n const stack: LayoutFrame[] = [{ name: 'root', node: rebuiltRoot }]\n const warnings: string[] = []\n\n let currentHeadingDepth: number | undefined\n\n const append = (node: AppendableRootContent) => getChildren(top(stack).node).push(node)\n\n for (const node of input) {\n if (isLayoutToken(node)) {\n if (node.action === 'open') {\n const definition = layoutDirectiveRegistry.get(node.name)\n\n if (definition?.kind === 'section') {\n const next = makeDirective('section', node.attributes)\n append(next)\n stack.push({ name: 'section', node: next })\n continue\n }\n\n if (definition?.kind === 'cell') {\n const next = makeDirective('cell', node.attributes)\n append(next)\n stack.push({ name: 'cell', node: next })\n continue\n }\n\n if (definition?.kind !== 'grid') {\n const next = makeDirective(node.name, node.attributes)\n append(next)\n stack.push({ name: node.name, node: next })\n continue\n }\n\n closeActiveGridInsideSection(stack)\n\n const parentDepth = currentHeadingDepth ?? 1\n const cellDepth = parentDepth + 1\n const next = makeDirective(node.name, node.attributes, parentDepth, cellDepth)\n\n append(next)\n stack.push({\n name: node.name,\n cellHeadingDepth: cellDepth,\n node: next,\n parentHeadingDepth: parentDepth,\n })\n continue\n }\n\n if (node.action === 'close') {\n if (stack.length === 1) {\n warnings.push('Encountered ::: with no open layout block.')\n continue\n }\n\n closeTop(stack)\n continue\n }\n\n if (node.action === 'closeGrid') {\n if (!closeActiveGrid(stack)) {\n warnings.push('Encountered :::endcol with no open grid.')\n continue\n }\n\n continue\n }\n\n if (node.action === 'closeSection') {\n if (!closeThroughSection(stack))\n warnings.push('Encountered :::end or :::endsection with no open section.')\n\n continue\n }\n }\n\n if (isHeading(node)) {\n const currentFrame = top(stack)\n\n if (isGridName(currentFrame.name)) {\n const parentDepth = currentFrame.parentHeadingDepth\n\n if (typeof parentDepth === 'number' && node.depth < parentDepth) closeTop(stack)\n }\n\n currentHeadingDepth = node.depth\n append(node)\n continue\n }\n\n if (isAppendableRootContent(node)) append(node)\n }\n\n while (stack.length > 1) {\n warnings.push(`Auto-closing unclosed layout block: ${top(stack).name}`)\n stack.pop()\n }\n\n tree.children = rebuiltRoot.children\n\n for (const warning of warnings) file.message(warning)\n }\n}\n"],"names":["layoutDirectiveRegistry","isHeading","node","type","isLayoutToken","isAppendableRootContent","makeDirective","name","attributes","parentHeadingDepth","cellHeadingDepth","Object","fromEntries","entries","map","key","value","String","children","data","vlCellHeadingDepth","vlParentHeadingDepth","getChildren","top","arr","length","isGridName","findNearestSectionIndex","stack","i","findNearestGridIndex","closeTop","pop","closeActiveGrid","gridIndex","closeActiveGridInsideSection","sectionIndex","closeThroughSection","remarkCompileLayouts","tree","file","input","rebuiltRoot","warnings","currentHeadingDepth","append","push","action","definition","get","kind","next","parentDepth","cellDepth","currentFrame","depth","warning","message"],"mappings":"AAMA,SAASA,uBAAuB,QAAQ,+BAA8B;AAatE,SAASC,UAAUC,IAAiB;IAClC,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASC,cAAcF,IAAiB;IACtC,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASE,wBAAwBH,IAAiB;IAChD,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASG,cACPC,IAAgB,EAChBC,aAA+C,CAAC,CAAC,EACjDC,kBAA2B,EAC3BC,gBAAyB;IAEzB,OAAO;QACLH;QACAJ,MAAM;QACNK,YAAYG,OAAOC,WAAW,CAC5BD,OAAOE,OAAO,CAACL,YAAYM,GAAG,CAAC,CAAC,CAACC,KAAKC,MAAM,GAAK;gBAACD;gBAAKE,OAAOD;aAAO;QAEvEE,UAAU,EAAE;QACZC,MAAM;YACJC,oBAAoBV;YACpBW,sBAAsBZ;QACxB;IACF;AACF;AAEA,SAASa,YAAYpB,IAAgB;IACnC,OAAOA,KAAKgB,QAAQ;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
|
2
|
+
import { resolveTocDepth, resolveTocTitle } from '../../directives/definitions/toc.js';
|
|
3
|
+
import { applyHeadingAnchors } from '../../directives/headingAnchors.js';
|
|
4
|
+
function isContainerDirective(node) {
|
|
5
|
+
return Boolean(node && typeof node === 'object' && 'type' in node && node.type === 'containerDirective');
|
|
6
|
+
}
|
|
7
|
+
function makeParagraph(value) {
|
|
8
|
+
return {
|
|
9
|
+
type: 'paragraph',
|
|
10
|
+
children: [
|
|
11
|
+
{
|
|
12
|
+
type: 'text',
|
|
13
|
+
value
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function makeTocLink(heading) {
|
|
19
|
+
return {
|
|
20
|
+
type: 'link',
|
|
21
|
+
children: [
|
|
22
|
+
{
|
|
23
|
+
type: 'text',
|
|
24
|
+
value: heading.text
|
|
25
|
+
}
|
|
26
|
+
],
|
|
27
|
+
url: `#${heading.id}`
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function makeTocList(headings) {
|
|
31
|
+
return {
|
|
32
|
+
type: 'list',
|
|
33
|
+
children: headings.map((heading)=>({
|
|
34
|
+
type: 'listItem',
|
|
35
|
+
children: [
|
|
36
|
+
{
|
|
37
|
+
type: 'paragraph',
|
|
38
|
+
children: [
|
|
39
|
+
makeTocLink(heading)
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
spread: false
|
|
44
|
+
})),
|
|
45
|
+
ordered: true,
|
|
46
|
+
spread: false
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function applyTocContent(tree, headings) {
|
|
50
|
+
visit(tree, (node)=>{
|
|
51
|
+
if (!isContainerDirective(node) || node.name !== 'toc') return;
|
|
52
|
+
const depth = resolveTocDepth(node);
|
|
53
|
+
const title = resolveTocTitle(node);
|
|
54
|
+
const visibleHeadings = headings.filter((heading)=>heading.depth <= depth);
|
|
55
|
+
node.children = [
|
|
56
|
+
makeParagraph(title),
|
|
57
|
+
makeTocList(visibleHeadings)
|
|
58
|
+
];
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export const remarkHeadingAnchorsAndToc = ()=>{
|
|
62
|
+
return (tree)=>{
|
|
63
|
+
const headings = applyHeadingAnchors(tree);
|
|
64
|
+
applyTocContent(tree, headings);
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=remarkHeadingAnchorsAndToc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/plugins/remarkHeadingAnchorsAndToc.ts"],"sourcesContent":["import type { Link, List, ListItem, Paragraph, Root } from 'mdast'\nimport type { ContainerDirective } from 'mdast-util-directive'\nimport type { Plugin } from 'unified'\n\nimport { visit } from 'unist-util-visit'\n\nimport type { HeadingAnchor } from '../../directives/headingAnchors.js'\n\nimport { resolveTocDepth, resolveTocTitle } from '../../directives/definitions/toc.js'\nimport { applyHeadingAnchors } from '../../directives/headingAnchors.js'\n\nfunction isContainerDirective(node: unknown): node is ContainerDirective {\n return Boolean(\n node &&\n typeof node === 'object' &&\n 'type' in node &&\n (node as { type?: string }).type === 'containerDirective',\n )\n}\n\nfunction makeParagraph(value: string): Paragraph {\n return {\n type: 'paragraph',\n children: [{ type: 'text', value }],\n }\n}\n\nfunction makeTocLink(heading: HeadingAnchor): Link {\n return {\n type: 'link',\n children: [{ type: 'text', value: heading.text }],\n url: `#${heading.id}`,\n }\n}\n\nfunction makeTocList(headings: HeadingAnchor[]): List {\n return {\n type: 'list',\n children: headings.map(\n (heading): ListItem => ({\n type: 'listItem',\n children: [\n {\n type: 'paragraph',\n children: [makeTocLink(heading)],\n },\n ],\n spread: false,\n }),\n ),\n ordered: true,\n spread: false,\n }\n}\n\nfunction applyTocContent(tree: Root, headings: HeadingAnchor[]) {\n visit(tree, (node) => {\n if (!isContainerDirective(node) || node.name !== 'toc') return\n\n const depth = resolveTocDepth(node)\n const title = resolveTocTitle(node)\n const visibleHeadings = headings.filter((heading) => heading.depth <= depth)\n\n node.children = [makeParagraph(title), makeTocList(visibleHeadings)]\n })\n}\n\nexport const remarkHeadingAnchorsAndToc: Plugin<[], Root> = () => {\n return (tree) => {\n const headings = applyHeadingAnchors(tree)\n\n applyTocContent(tree, headings)\n }\n}\n"],"names":["visit","resolveTocDepth","resolveTocTitle","applyHeadingAnchors","isContainerDirective","node","Boolean","type","makeParagraph","value","children","makeTocLink","heading","text","url","id","makeTocList","headings","map","spread","ordered","applyTocContent","tree","name","depth","title","visibleHeadings","filter","remarkHeadingAnchorsAndToc"],"mappings":"AAIA,SAASA,KAAK,QAAQ,mBAAkB;AAIxC,SAASC,eAAe,EAAEC,eAAe,QAAQ,sCAAqC;AACtF,SAASC,mBAAmB,QAAQ,qCAAoC;AAExE,SAASC,qBAAqBC,IAAa;IACzC,OAAOC,QACLD,QACE,OAAOA,SAAS,YAChB,UAAUA,QACV,AAACA,KAA2BE,IAAI,KAAK;AAE3C;AAEA,SAASC,cAAcC,KAAa;IAClC,OAAO;QACLF,MAAM;QACNG,UAAU;YAAC;gBAAEH,MAAM;gBAAQE;YAAM;SAAE;IACrC;AACF;AAEA,SAASE,YAAYC,OAAsB;IACzC,OAAO;QACLL,MAAM;QACNG,UAAU;YAAC;gBAAEH,MAAM;gBAAQE,OAAOG,QAAQC,IAAI;YAAC;SAAE;QACjDC,KAAK,CAAC,CAAC,EAAEF,QAAQG,EAAE,EAAE;IACvB;AACF;AAEA,SAASC,YAAYC,QAAyB;IAC5C,OAAO;QACLV,MAAM;QACNG,UAAUO,SAASC,GAAG,CACpB,CAACN,UAAuB,CAAA;gBACtBL,MAAM;gBACNG,UAAU;oBACR;wBACEH,MAAM;wBACNG,UAAU;4BAACC,YAAYC;yBAAS;oBAClC;iBACD;gBACDO,QAAQ;YACV,CAAA;QAEFC,SAAS;QACTD,QAAQ;IACV;AACF;AAEA,SAASE,gBAAgBC,IAAU,EAAEL,QAAyB;IAC5DjB,MAAMsB,MAAM,CAACjB;QACX,IAAI,CAACD,qBAAqBC,SAASA,KAAKkB,IAAI,KAAK,OAAO;QAExD,MAAMC,QAAQvB,gBAAgBI;QAC9B,MAAMoB,QAAQvB,gBAAgBG;QAC9B,MAAMqB,kBAAkBT,SAASU,MAAM,CAAC,CAACf,UAAYA,QAAQY,KAAK,IAAIA;QAEtEnB,KAAKK,QAAQ,GAAG;YAACF,cAAciB;YAAQT,YAAYU;SAAiB;IACtE;AACF;AAEA,OAAO,MAAME,6BAA+C;IAC1D,OAAO,CAACN;QACN,MAAML,WAAWd,oBAAoBmB;QAErCD,gBAAgBC,MAAML;IACxB;AACF,EAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
import { layoutDirectiveRegistry } from '../../directives/registry.js';
|
|
3
|
+
import { setDirectiveRenderData } from '../../directives/renderData.js';
|
|
4
|
+
function isContainerDirective(node) {
|
|
5
|
+
return Boolean(node && typeof node === 'object' && 'type' in node && node.type === 'containerDirective');
|
|
6
|
+
}
|
|
7
|
+
function transformDirective(node, file) {
|
|
8
|
+
const definition = layoutDirectiveRegistry.get(node.name);
|
|
9
|
+
if (!definition) return;
|
|
10
|
+
for (const warning of definition.validateMdast?.(node) ?? [])file.message(warning);
|
|
11
|
+
definition.transformMdast?.(node, {
|
|
12
|
+
isSupportedDirectiveName: (name)=>layoutDirectiveRegistry.isSupportedDirectiveName(name)
|
|
13
|
+
});
|
|
14
|
+
setDirectiveRenderData(node, definition, definition.getMdastRenderProperties?.(node));
|
|
15
|
+
}
|
|
16
|
+
export const remarkLayoutDirectives = ()=>{
|
|
17
|
+
return (tree, file)=>{
|
|
18
|
+
visit(tree, (node)=>{
|
|
19
|
+
if (!isContainerDirective(node)) return;
|
|
20
|
+
if (!layoutDirectiveRegistry.isSupportedDirectiveName(node.name)) return;
|
|
21
|
+
transformDirective(node, file);
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=remarkLayoutDirectives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/plugins/remarkLayoutDirectives.ts"],"sourcesContent":["import type { Root } from 'mdast'\nimport type { ContainerDirective } from 'mdast-util-directive'\nimport type { Plugin } from 'unified'\n\nimport { visit } from 'unist-util-visit'\n\nimport { layoutDirectiveRegistry } from '../../directives/registry.js'\nimport { setDirectiveRenderData } from '../../directives/renderData.js'\n\nfunction isContainerDirective(node: unknown): node is ContainerDirective {\n return Boolean(\n node &&\n typeof node === 'object' &&\n 'type' in node &&\n (node as { type?: string }).type === 'containerDirective',\n )\n}\n\ntype WarningSink = {\n message: (reason: string) => unknown\n}\n\nfunction transformDirective(node: ContainerDirective, file: WarningSink) {\n const definition = layoutDirectiveRegistry.get(node.name)\n\n if (!definition) return\n\n for (const warning of definition.validateMdast?.(node) ?? []) file.message(warning)\n\n definition.transformMdast?.(node, {\n isSupportedDirectiveName: (name) => layoutDirectiveRegistry.isSupportedDirectiveName(name),\n })\n\n setDirectiveRenderData(node, definition, definition.getMdastRenderProperties?.(node))\n}\n\nexport const remarkLayoutDirectives: Plugin<[], Root> = () => {\n return (tree: Root, file) => {\n visit(tree, (node) => {\n if (!isContainerDirective(node)) return\n if (!layoutDirectiveRegistry.isSupportedDirectiveName(node.name)) return\n\n transformDirective(node, file)\n })\n }\n}\n"],"names":["visit","layoutDirectiveRegistry","setDirectiveRenderData","isContainerDirective","node","Boolean","type","transformDirective","file","definition","get","name","warning","validateMdast","message","transformMdast","isSupportedDirectiveName","getMdastRenderProperties","remarkLayoutDirectives","tree"],"mappings":"AAIA,SAASA,KAAK,QAAQ,mBAAkB;AAExC,SAASC,uBAAuB,QAAQ,+BAA8B;AACtE,SAASC,sBAAsB,QAAQ,iCAAgC;AAEvE,SAASC,qBAAqBC,IAAa;IACzC,OAAOC,QACLD,QACE,OAAOA,SAAS,YAChB,UAAUA,QACV,AAACA,KAA2BE,IAAI,KAAK;AAE3C;AAMA,SAASC,mBAAmBH,IAAwB,EAAEI,IAAiB;IACrE,MAAMC,aAAaR,wBAAwBS,GAAG,CAACN,KAAKO,IAAI;IAExD,IAAI,CAACF,YAAY;IAEjB,KAAK,MAAMG,WAAWH,WAAWI,aAAa,GAAGT,SAAS,EAAE,CAAEI,KAAKM,OAAO,CAACF;IAE3EH,WAAWM,cAAc,GAAGX,MAAM;QAChCY,0BAA0B,CAACL,OAASV,wBAAwBe,wBAAwB,CAACL;IACvF;IAEAT,uBAAuBE,MAAMK,YAAYA,WAAWQ,wBAAwB,GAAGb;AACjF;AAEA,OAAO,MAAMc,yBAA2C;IACtD,OAAO,CAACC,MAAYX;QAClBR,MAAMmB,MAAM,CAACf;YACX,IAAI,CAACD,qBAAqBC,OAAO;YACjC,IAAI,CAACH,wBAAwBe,wBAAwB,CAACZ,KAAKO,IAAI,GAAG;YAElEJ,mBAAmBH,MAAMI;QAC3B;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
function isParagraph(node) {
|
|
2
|
+
return node.type === 'paragraph';
|
|
3
|
+
}
|
|
4
|
+
function isHeading(node) {
|
|
5
|
+
return node.type === 'heading';
|
|
6
|
+
}
|
|
7
|
+
function isText(node) {
|
|
8
|
+
return node.type === 'text';
|
|
9
|
+
}
|
|
10
|
+
function getParagraphText(node) {
|
|
11
|
+
if (!node.children.every(isText)) return null;
|
|
12
|
+
return node.children.map((child)=>child.value).join('').trim();
|
|
13
|
+
}
|
|
14
|
+
function getSentinel(node) {
|
|
15
|
+
if (!isParagraph(node)) return null;
|
|
16
|
+
const text = getParagraphText(node);
|
|
17
|
+
if (!text) return null;
|
|
18
|
+
if (text === '%%VL_OPEN:section%%') return {
|
|
19
|
+
name: 'section',
|
|
20
|
+
type: 'open'
|
|
21
|
+
};
|
|
22
|
+
if (text === '%%VL_OPEN:2col%%') return {
|
|
23
|
+
name: '2col',
|
|
24
|
+
type: 'open'
|
|
25
|
+
};
|
|
26
|
+
if (text === '%%VL_OPEN:3col%%') return {
|
|
27
|
+
name: '3col',
|
|
28
|
+
type: 'open'
|
|
29
|
+
};
|
|
30
|
+
if (text === '%%VL_CLOSE%%') return {
|
|
31
|
+
type: 'close'
|
|
32
|
+
};
|
|
33
|
+
if (text === '%%VL_CLOSE_GRID%%') return {
|
|
34
|
+
type: 'closeGrid'
|
|
35
|
+
};
|
|
36
|
+
if (text === '%%VL_CLOSE_SECTION%%') return {
|
|
37
|
+
type: 'closeSection'
|
|
38
|
+
};
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
function makeDirective(name, parentHeadingDepth, cellHeadingDepth) {
|
|
42
|
+
return {
|
|
43
|
+
name,
|
|
44
|
+
type: 'containerDirective',
|
|
45
|
+
attributes: {},
|
|
46
|
+
children: [],
|
|
47
|
+
data: {
|
|
48
|
+
vlCellHeadingDepth: cellHeadingDepth,
|
|
49
|
+
vlParentHeadingDepth: parentHeadingDepth
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function getChildren(node) {
|
|
54
|
+
return node.children;
|
|
55
|
+
}
|
|
56
|
+
function top(arr) {
|
|
57
|
+
return arr[arr.length - 1];
|
|
58
|
+
}
|
|
59
|
+
function isGridName(name) {
|
|
60
|
+
return name === '2col' || name === '3col';
|
|
61
|
+
}
|
|
62
|
+
function findNearestSectionIndex(stack) {
|
|
63
|
+
for(let i = stack.length - 1; i >= 0; --i)if (stack[i]?.name === 'section') return i;
|
|
64
|
+
return -1;
|
|
65
|
+
}
|
|
66
|
+
function findNearestGridIndex(stack) {
|
|
67
|
+
for(let i = stack.length - 1; i >= 0; --i)if (isGridName(stack[i]?.name)) return i;
|
|
68
|
+
return -1;
|
|
69
|
+
}
|
|
70
|
+
function closeTop(stack) {
|
|
71
|
+
if (stack.length > 1) stack.pop();
|
|
72
|
+
}
|
|
73
|
+
function closeActiveGrid(stack) {
|
|
74
|
+
const gridIndex = findNearestGridIndex(stack);
|
|
75
|
+
if (gridIndex < 0) return false;
|
|
76
|
+
while(stack.length - 1 >= gridIndex)stack.pop();
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
function closeActiveGridInsideSection(stack) {
|
|
80
|
+
const gridIndex = findNearestGridIndex(stack);
|
|
81
|
+
const sectionIndex = findNearestSectionIndex(stack);
|
|
82
|
+
if (gridIndex < 0 || sectionIndex < 0) return false;
|
|
83
|
+
if (gridIndex < sectionIndex) return false;
|
|
84
|
+
while(stack.length - 1 >= gridIndex)stack.pop();
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
function closeThroughSection(stack) {
|
|
88
|
+
const sectionIndex = findNearestSectionIndex(stack);
|
|
89
|
+
if (sectionIndex < 0) return false;
|
|
90
|
+
while(stack.length - 1 >= sectionIndex)stack.pop();
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
export const remarkLayoutSentinels = ()=>{
|
|
94
|
+
return (tree, file)=>{
|
|
95
|
+
const input = [
|
|
96
|
+
...tree.children
|
|
97
|
+
];
|
|
98
|
+
const rebuiltRoot = {
|
|
99
|
+
...tree,
|
|
100
|
+
children: []
|
|
101
|
+
};
|
|
102
|
+
const stack = [
|
|
103
|
+
{
|
|
104
|
+
name: 'root',
|
|
105
|
+
node: rebuiltRoot
|
|
106
|
+
}
|
|
107
|
+
];
|
|
108
|
+
const warnings = [];
|
|
109
|
+
let currentHeadingDepth;
|
|
110
|
+
const append = (node)=>getChildren(top(stack).node).push(node);
|
|
111
|
+
for (const node of input){
|
|
112
|
+
const sentinel = getSentinel(node);
|
|
113
|
+
if (sentinel) {
|
|
114
|
+
if (sentinel.type === 'open') {
|
|
115
|
+
if (sentinel.name === 'section') {
|
|
116
|
+
const next = makeDirective('section');
|
|
117
|
+
append(next);
|
|
118
|
+
stack.push({
|
|
119
|
+
name: 'section',
|
|
120
|
+
node: next
|
|
121
|
+
});
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
// Opening a new grid while already inside a grid inside a section
|
|
125
|
+
// rolls over to a sibling grid automatically.
|
|
126
|
+
closeActiveGridInsideSection(stack);
|
|
127
|
+
const parentDepth = currentHeadingDepth ?? 1;
|
|
128
|
+
const cellDepth = parentDepth + 1;
|
|
129
|
+
const next = makeDirective(sentinel.name, parentDepth, cellDepth);
|
|
130
|
+
append(next);
|
|
131
|
+
stack.push({
|
|
132
|
+
name: sentinel.name,
|
|
133
|
+
cellHeadingDepth: cellDepth,
|
|
134
|
+
node: next,
|
|
135
|
+
parentHeadingDepth: parentDepth
|
|
136
|
+
});
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (sentinel.type === 'close') {
|
|
140
|
+
if (stack.length === 1) {
|
|
141
|
+
warnings.push('Encountered ::: with no open layout block.');
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
closeTop(stack);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (sentinel.type === 'closeGrid') {
|
|
148
|
+
if (!closeActiveGrid(stack)) {
|
|
149
|
+
warnings.push('Encountered :::endcol with no open grid.');
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (sentinel.type === 'closeSection') {
|
|
155
|
+
if (!closeThroughSection(stack)) warnings.push('Encountered :::end or :::endsection with no open section.');
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (isHeading(node)) {
|
|
160
|
+
const currentFrame = top(stack);
|
|
161
|
+
if (isGridName(currentFrame.name)) {
|
|
162
|
+
const parentDepth = currentFrame.parentHeadingDepth;
|
|
163
|
+
// Same-level heading as the parent section remains inside the grid.
|
|
164
|
+
// Only a true ascent above the parent depth auto-closes the grid.
|
|
165
|
+
if (typeof parentDepth === 'number' && node.depth < parentDepth) {
|
|
166
|
+
closeTop(stack);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
currentHeadingDepth = node.depth;
|
|
170
|
+
append(node);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
append(node);
|
|
174
|
+
}
|
|
175
|
+
while(stack.length > 1){
|
|
176
|
+
warnings.push(`Auto-closing unclosed layout block: ${top(stack).name}`);
|
|
177
|
+
stack.pop();
|
|
178
|
+
}
|
|
179
|
+
tree.children = rebuiltRoot.children;
|
|
180
|
+
for (const warning of warnings)file.message(warning);
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
//# sourceMappingURL=remarkLayoutSentinels.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/plugins/remarkLayoutSentinels.ts"],"sourcesContent":["import type { Content, Heading, Paragraph, Root, Text } from 'mdast'\nimport type { ContainerDirective } from 'mdast-util-directive'\nimport type { Plugin } from 'unified'\n\ntype LayoutName = '2col' | '3col' | 'section'\ntype LayoutNode = ContainerDirective | Root\n\ntype LayoutFrame = {\n cellHeadingDepth?: number\n name: 'root' | LayoutName\n node: LayoutNode\n parentHeadingDepth?: number\n}\n\ntype Sentinel =\n | { name: LayoutName; type: 'open' }\n | { type: 'close' }\n | { type: 'closeGrid' }\n | { type: 'closeSection' }\n\nfunction isParagraph(node: Content): node is Paragraph {\n return node.type === 'paragraph'\n}\n\nfunction isHeading(node: Content): node is Heading {\n return node.type === 'heading'\n}\n\nfunction isText(node: Paragraph['children'][number]): node is Text {\n return node.type === 'text'\n}\n\nfunction getParagraphText(node: Paragraph): null | string {\n if (!node.children.every(isText)) return null\n return node.children\n .map((child) => child.value)\n .join('')\n .trim()\n}\n\nfunction getSentinel(node: Content): null | Sentinel {\n if (!isParagraph(node)) return null\n\n const text = getParagraphText(node)\n if (!text) return null\n\n if (text === '%%VL_OPEN:section%%') return { name: 'section', type: 'open' }\n if (text === '%%VL_OPEN:2col%%') return { name: '2col', type: 'open' }\n if (text === '%%VL_OPEN:3col%%') return { name: '3col', type: 'open' }\n if (text === '%%VL_CLOSE%%') return { type: 'close' }\n if (text === '%%VL_CLOSE_GRID%%') return { type: 'closeGrid' }\n if (text === '%%VL_CLOSE_SECTION%%') return { type: 'closeSection' }\n\n return null\n}\n\nfunction makeDirective(\n name: LayoutName,\n parentHeadingDepth?: number,\n cellHeadingDepth?: number,\n): ContainerDirective {\n return {\n name,\n type: 'containerDirective',\n attributes: {},\n children: [],\n data: {\n vlCellHeadingDepth: cellHeadingDepth,\n vlParentHeadingDepth: parentHeadingDepth,\n },\n }\n}\n\nfunction getChildren(node: LayoutNode): Content[] {\n return node.children as Content[]\n}\n\nfunction top<T>(arr: T[]): T {\n return arr[arr.length - 1]\n}\n\nfunction isGridName(name: LayoutFrame['name']): name is '2col' | '3col' {\n return name === '2col' || name === '3col'\n}\n\nfunction findNearestSectionIndex(stack: LayoutFrame[]): number {\n for (let i = stack.length - 1; i >= 0; --i) if (stack[i]?.name === 'section') return i\n\n return -1\n}\n\nfunction findNearestGridIndex(stack: LayoutFrame[]): number {\n for (let i = stack.length - 1; i >= 0; --i) if (isGridName(stack[i]?.name)) return i\n\n return -1\n}\n\nfunction closeTop(stack: LayoutFrame[]) {\n if (stack.length > 1) stack.pop()\n}\n\nfunction closeActiveGrid(stack: LayoutFrame[]): boolean {\n const gridIndex = findNearestGridIndex(stack)\n if (gridIndex < 0) return false\n\n while (stack.length - 1 >= gridIndex) stack.pop()\n\n return true\n}\n\nfunction closeActiveGridInsideSection(stack: LayoutFrame[]): boolean {\n const gridIndex = findNearestGridIndex(stack)\n const sectionIndex = findNearestSectionIndex(stack)\n\n if (gridIndex < 0 || sectionIndex < 0) return false\n if (gridIndex < sectionIndex) return false\n\n while (stack.length - 1 >= gridIndex) stack.pop()\n\n return true\n}\n\nfunction closeThroughSection(stack: LayoutFrame[]): boolean {\n const sectionIndex = findNearestSectionIndex(stack)\n if (sectionIndex < 0) return false\n\n while (stack.length - 1 >= sectionIndex) stack.pop()\n\n return true\n}\n\nexport const remarkLayoutSentinels: Plugin<[], Root> = () => {\n return (tree: Root, file) => {\n const input = [...tree.children]\n const rebuiltRoot: Root = { ...tree, children: [] }\n const stack: LayoutFrame[] = [{ name: 'root', node: rebuiltRoot }]\n const warnings: string[] = []\n\n let currentHeadingDepth: number | undefined\n\n const append = (node: Content) => getChildren(top(stack).node).push(node)\n\n for (const node of input) {\n const sentinel = getSentinel(node)\n\n if (sentinel) {\n if (sentinel.type === 'open') {\n if (sentinel.name === 'section') {\n const next = makeDirective('section')\n append(next)\n stack.push({ name: 'section', node: next })\n continue\n }\n\n // Opening a new grid while already inside a grid inside a section\n // rolls over to a sibling grid automatically.\n closeActiveGridInsideSection(stack)\n\n const parentDepth = currentHeadingDepth ?? 1\n const cellDepth = parentDepth + 1\n const next = makeDirective(sentinel.name, parentDepth, cellDepth)\n\n append(next)\n stack.push({\n name: sentinel.name,\n cellHeadingDepth: cellDepth,\n node: next,\n parentHeadingDepth: parentDepth,\n })\n continue\n }\n\n if (sentinel.type === 'close') {\n if (stack.length === 1) {\n warnings.push('Encountered ::: with no open layout block.')\n continue\n }\n\n closeTop(stack)\n continue\n }\n\n if (sentinel.type === 'closeGrid') {\n if (!closeActiveGrid(stack)) {\n warnings.push('Encountered :::endcol with no open grid.')\n continue\n }\n\n continue\n }\n\n if (sentinel.type === 'closeSection') {\n if (!closeThroughSection(stack))\n warnings.push('Encountered :::end or :::endsection with no open section.')\n\n continue\n }\n }\n\n if (isHeading(node)) {\n const currentFrame = top(stack)\n\n if (isGridName(currentFrame.name)) {\n const parentDepth = currentFrame.parentHeadingDepth\n\n // Same-level heading as the parent section remains inside the grid.\n // Only a true ascent above the parent depth auto-closes the grid.\n if (typeof parentDepth === 'number' && node.depth < parentDepth) {\n closeTop(stack)\n }\n }\n\n currentHeadingDepth = node.depth\n append(node)\n continue\n }\n\n append(node)\n }\n\n while (stack.length > 1) {\n warnings.push(`Auto-closing unclosed layout block: ${top(stack).name}`)\n stack.pop()\n }\n\n tree.children = rebuiltRoot.children\n\n for (const warning of warnings) file.message(warning)\n }\n}\n"],"names":["isParagraph","node","type","isHeading","isText","getParagraphText","children","every","map","child","value","join","trim","getSentinel","text","name","makeDirective","parentHeadingDepth","cellHeadingDepth","attributes","data","vlCellHeadingDepth","vlParentHeadingDepth","getChildren","top","arr","length","isGridName","findNearestSectionIndex","stack","i","findNearestGridIndex","closeTop","pop","closeActiveGrid","gridIndex","closeActiveGridInsideSection","sectionIndex","closeThroughSection","remarkLayoutSentinels","tree","file","input","rebuiltRoot","warnings","currentHeadingDepth","append","push","sentinel","next","parentDepth","cellDepth","currentFrame","depth","warning","message"],"mappings":"AAoBA,SAASA,YAAYC,IAAa;IAChC,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASC,UAAUF,IAAa;IAC9B,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASE,OAAOH,IAAmC;IACjD,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASG,iBAAiBJ,IAAe;IACvC,IAAI,CAACA,KAAKK,QAAQ,CAACC,KAAK,CAACH,SAAS,OAAO;IACzC,OAAOH,KAAKK,QAAQ,CACjBE,GAAG,CAAC,CAACC,QAAUA,MAAMC,KAAK,EAC1BC,IAAI,CAAC,IACLC,IAAI;AACT;AAEA,SAASC,YAAYZ,IAAa;IAChC,IAAI,CAACD,YAAYC,OAAO,OAAO;IAE/B,MAAMa,OAAOT,iBAAiBJ;IAC9B,IAAI,CAACa,MAAM,OAAO;IAElB,IAAIA,SAAS,uBAAuB,OAAO;QAAEC,MAAM;QAAWb,MAAM;IAAO;IAC3E,IAAIY,SAAS,oBAAoB,OAAO;QAAEC,MAAM;QAAQb,MAAM;IAAO;IACrE,IAAIY,SAAS,oBAAoB,OAAO;QAAEC,MAAM;QAAQb,MAAM;IAAO;IACrE,IAAIY,SAAS,gBAAgB,OAAO;QAAEZ,MAAM;IAAQ;IACpD,IAAIY,SAAS,qBAAqB,OAAO;QAAEZ,MAAM;IAAY;IAC7D,IAAIY,SAAS,wBAAwB,OAAO;QAAEZ,MAAM;IAAe;IAEnE,OAAO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,IAAI,KAAK,SAAS;oBAC7B,IAAI2B,MAAMH,MAAM,KAAK,GAAG;wBACtBkB,SAASG,IAAI,CAAC;wBACd;oBACF;oBAEAf,SAASH;oBACT;gBACF;gBAEA,IAAImB,SAAS9C,IAAI,KAAK,aAAa;oBACjC,IAAI,CAACgC,gBAAgBL,QAAQ;wBAC3Be,SAASG,IAAI,CAAC;wBACd;oBACF;oBAEA;gBACF;gBAEA,IAAIC,SAAS9C,IAAI,KAAK,gBAAgB;oBACpC,IAAI,CAACoC,oBAAoBT,QACvBe,SAASG,IAAI,CAAC;oBAEhB;gBACF;YACF;YAEA,IAAI5C,UAAUF,OAAO;gBACnB,MAAMmD,eAAe5B,IAAIK;gBAEzB,IAAIF,WAAWyB,aAAarC,IAAI,GAAG;oBACjC,MAAMmC,cAAcE,aAAanC,kBAAkB;oBAEnD,oEAAoE;oBACpE,kEAAkE;oBAClE,IAAI,OAAOiC,gBAAgB,YAAYjD,KAAKoD,KAAK,GAAGH,aAAa;wBAC/DlB,SAASH;oBACX;gBACF;gBAEAgB,sBAAsB5C,KAAKoD,KAAK;gBAChCP,OAAO7C;gBACP;YACF;YAEA6C,OAAO7C;QACT;QAEA,MAAO4B,MAAMH,MAAM,GAAG,EAAG;YACvBkB,SAASG,IAAI,CAAC,CAAC,oCAAoC,EAAEvB,IAAIK,OAAOd,IAAI,EAAE;YACtEc,MAAMI,GAAG;QACX;QAEAO,KAAKlC,QAAQ,GAAGqC,YAAYrC,QAAQ;QAEpC,KAAK,MAAMgD,WAAWV,SAAUH,KAAKc,OAAO,CAACD;IAC/C;AACF,EAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { layoutDirectiveRegistry } from '../../directives/registry.js';
|
|
2
|
+
function isParagraph(node) {
|
|
3
|
+
return node.type === 'paragraph';
|
|
4
|
+
}
|
|
5
|
+
function isText(node) {
|
|
6
|
+
return node.type === 'text';
|
|
7
|
+
}
|
|
8
|
+
function parseLayoutDirective(text) {
|
|
9
|
+
return layoutDirectiveRegistry.parseMarkdownLineDetailed(text);
|
|
10
|
+
}
|
|
11
|
+
function makeParagraph(children) {
|
|
12
|
+
return {
|
|
13
|
+
type: 'paragraph',
|
|
14
|
+
children
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function makeText(value) {
|
|
18
|
+
return {
|
|
19
|
+
type: 'text',
|
|
20
|
+
value
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function splitParagraphLines(node) {
|
|
24
|
+
const lines = [
|
|
25
|
+
[]
|
|
26
|
+
];
|
|
27
|
+
const currentLine = ()=>lines[lines.length - 1];
|
|
28
|
+
for (const child of node.children){
|
|
29
|
+
if (!isText(child)) {
|
|
30
|
+
currentLine().push(child);
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const parts = child.value.split(/\r?\n/);
|
|
34
|
+
for(let index = 0; index < parts.length; ++index){
|
|
35
|
+
if (index > 0) lines.push([]);
|
|
36
|
+
if (parts[index]) currentLine().push(makeText(parts[index]));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return lines;
|
|
40
|
+
}
|
|
41
|
+
function getTextOnlyLine(children) {
|
|
42
|
+
if (!children.every(isText)) return null;
|
|
43
|
+
return children.map((child)=>child.value).join('');
|
|
44
|
+
}
|
|
45
|
+
function appendParagraphLine(lines, children) {
|
|
46
|
+
if (lines.length > 0) lines.push([
|
|
47
|
+
makeText('\n'),
|
|
48
|
+
...children
|
|
49
|
+
]);
|
|
50
|
+
else lines.push(children);
|
|
51
|
+
}
|
|
52
|
+
function flattenParagraphLines(lines) {
|
|
53
|
+
return lines.flat();
|
|
54
|
+
}
|
|
55
|
+
function splitParagraphLayoutDirectives(node, warn) {
|
|
56
|
+
const out = [];
|
|
57
|
+
let paragraphLines = [];
|
|
58
|
+
const flushParagraph = ()=>{
|
|
59
|
+
const children = flattenParagraphLines(paragraphLines);
|
|
60
|
+
if (children.length > 0) out.push(makeParagraph(children));
|
|
61
|
+
paragraphLines = [];
|
|
62
|
+
};
|
|
63
|
+
for (const lineChildren of splitParagraphLines(node)){
|
|
64
|
+
const text = getTextOnlyLine(lineChildren);
|
|
65
|
+
const result = text ? parseLayoutDirective(text.trim()) : {
|
|
66
|
+
diagnostics: [],
|
|
67
|
+
token: null
|
|
68
|
+
};
|
|
69
|
+
for (const diagnostic of result.diagnostics)warn(diagnostic);
|
|
70
|
+
if (!result.token) {
|
|
71
|
+
appendParagraphLine(paragraphLines, lineChildren);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
flushParagraph();
|
|
75
|
+
out.push(result.token);
|
|
76
|
+
}
|
|
77
|
+
flushParagraph();
|
|
78
|
+
return out.length > 0 ? out : [
|
|
79
|
+
node
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
export const remarkLiftLayoutDirectives = ()=>{
|
|
83
|
+
return (tree, file)=>{
|
|
84
|
+
tree.children = tree.children.flatMap((node)=>{
|
|
85
|
+
if (!isParagraph(node)) return [
|
|
86
|
+
node
|
|
87
|
+
];
|
|
88
|
+
return splitParagraphLayoutDirectives(node, (message)=>file.message(message));
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//# sourceMappingURL=remarkLiftLayoutDirectives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/core/plugins/remarkLiftLayoutDirectives.ts"],"sourcesContent":["import type { Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast'\nimport type { Plugin } from 'unified'\n\nimport type { LayoutToken } from '../../types/layoutToken.js'\n\nimport { layoutDirectiveRegistry } from '../../directives/registry.js'\n\nfunction isParagraph(node: RootContent): node is Paragraph {\n return node.type === 'paragraph'\n}\n\nfunction isText(node: Paragraph['children'][number]): node is Text {\n return node.type === 'text'\n}\n\nfunction parseLayoutDirective(text: string): {\n diagnostics: string[]\n token: LayoutToken | null\n} {\n return layoutDirectiveRegistry.parseMarkdownLineDetailed(text)\n}\n\nfunction makeParagraph(children: PhrasingContent[]): Paragraph {\n return {\n type: 'paragraph',\n children,\n }\n}\n\nfunction makeText(value: string): Text {\n return {\n type: 'text',\n value,\n }\n}\n\nfunction splitParagraphLines(node: Paragraph): PhrasingContent[][] {\n const lines: PhrasingContent[][] = [[]]\n\n const currentLine = () => lines[lines.length - 1]\n\n for (const child of node.children) {\n if (!isText(child)) {\n currentLine().push(child)\n continue\n }\n\n const parts = child.value.split(/\\r?\\n/)\n\n for (let index = 0; index < parts.length; ++index) {\n if (index > 0) lines.push([])\n\n if (parts[index]) currentLine().push(makeText(parts[index]))\n }\n }\n\n return lines\n}\n\nfunction getTextOnlyLine(children: PhrasingContent[]): null | string {\n if (!children.every(isText)) return null\n\n return children.map((child) => child.value).join('')\n}\n\nfunction appendParagraphLine(lines: PhrasingContent[][], children: PhrasingContent[]) {\n if (lines.length > 0) lines.push([makeText('\\n'), ...children])\n else lines.push(children)\n}\n\nfunction flattenParagraphLines(lines: PhrasingContent[][]): PhrasingContent[] {\n return lines.flat()\n}\n\nfunction splitParagraphLayoutDirectives(\n node: Paragraph,\n warn: (message: string) => void,\n): RootContent[] {\n const out: RootContent[] = []\n let paragraphLines: PhrasingContent[][] = []\n\n const flushParagraph = () => {\n const children = flattenParagraphLines(paragraphLines)\n\n if (children.length > 0) out.push(makeParagraph(children))\n\n paragraphLines = []\n }\n\n for (const lineChildren of splitParagraphLines(node)) {\n const text = getTextOnlyLine(lineChildren)\n const result = text ? parseLayoutDirective(text.trim()) : { diagnostics: [], token: null }\n\n for (const diagnostic of result.diagnostics) warn(diagnostic)\n\n if (!result.token) {\n appendParagraphLine(paragraphLines, lineChildren)\n continue\n }\n\n flushParagraph()\n out.push(result.token)\n }\n\n flushParagraph()\n\n return out.length > 0 ? out : [node]\n}\n\nexport const remarkLiftLayoutDirectives: Plugin<[], Root> = () => {\n return (tree, file) => {\n tree.children = tree.children.flatMap((node): RootContent[] => {\n if (!isParagraph(node)) return [node]\n\n return splitParagraphLayoutDirectives(node, (message) => file.message(message))\n })\n }\n}\n"],"names":["layoutDirectiveRegistry","isParagraph","node","type","isText","parseLayoutDirective","text","parseMarkdownLineDetailed","makeParagraph","children","makeText","value","splitParagraphLines","lines","currentLine","length","child","push","parts","split","index","getTextOnlyLine","every","map","join","appendParagraphLine","flattenParagraphLines","flat","splitParagraphLayoutDirectives","warn","out","paragraphLines","flushParagraph","lineChildren","result","trim","diagnostics","token","diagnostic","remarkLiftLayoutDirectives","tree","file","flatMap","message"],"mappings":"AAKA,SAASA,uBAAuB,QAAQ,+BAA8B;AAEtE,SAASC,YAAYC,IAAiB;IACpC,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASC,OAAOF,IAAmC;IACjD,OAAOA,KAAKC,IAAI,KAAK;AACvB;AAEA,SAASE,qBAAqBC,IAAY;IAIxC,OAAON,wBAAwBO,yBAAyB,CAACD;AAC3D;AAEA,SAASE,cAAcC,QAA2B;IAChD,OAAO;QACLN,MAAM;QACNM;IACF;AACF;AAEA,SAASC,SAASC,KAAa;IAC7B,OAAO;QACLR,MAAM;QACNQ;IACF;AACF;AAEA,SAASC,oBAAoBV,IAAe;IAC1C,MAAMW,QAA6B;QAAC,EAAE;KAAC;IAEvC,MAAMC,cAAc,IAAMD,KAAK,CAACA,MAAME,MAAM,GAAG,EAAE;IAEjD,KAAK,MAAMC,SAASd,KAAKO,QAAQ,CAAE;QACjC,IAAI,CAACL,OAAOY,QAAQ;YAClBF,cAAcG,IAAI,CAACD;YACnB;QACF;QAEA,MAAME,QAAQF,MAAML,KAAK,CAACQ,KAAK,CAAC;QAEhC,IAAK,IAAIC,QAAQ,GAAGA,QAAQF,MAAMH,MAAM,EAAE,EAAEK,MAAO;YACjD,IAAIA,QAAQ,GAAGP,MAAMI,IAAI,CAAC,EAAE;YAE5B,IAAIC,KAAK,CAACE,MAAM,EAAEN,cAAcG,IAAI,CAACP,SAASQ,KAAK,CAACE,MAAM;QAC5D;IACF;IAEA,OAAOP;AACT;A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