@lexical/mdast 0.0.0-bootstrap.0 → 0.47.0
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/README.md +207 -2
- package/dist/LexicalMdast.dev.js +2620 -0
- package/dist/LexicalMdast.dev.mjs +2594 -0
- package/dist/LexicalMdast.js +11 -0
- package/dist/LexicalMdast.js.flow +228 -0
- package/dist/LexicalMdast.mjs +36 -0
- package/dist/LexicalMdast.node.mjs +34 -0
- package/dist/LexicalMdast.prod.js +11 -0
- package/dist/LexicalMdast.prod.mjs +11 -0
- package/dist/MdastExport.d.ts +20 -0
- package/dist/MdastExportExtension.d.ts +80 -0
- package/dist/MdastExtension.d.ts +21 -0
- package/dist/MdastGfmExtension.d.ts +20 -0
- package/dist/MdastImport.d.ts +48 -0
- package/dist/MdastImportExtension.d.ts +264 -0
- package/dist/MdastShortcuts.d.ts +27 -0
- package/dist/MdastStream.d.ts +70 -0
- package/dist/MdastTableExtension.d.ts +29 -0
- package/dist/compile.d.ts +18 -0
- package/dist/handlers.d.ts +84 -0
- package/dist/index.d.ts +15 -0
- package/dist/state.d.ts +58 -0
- package/dist/types.d.ts +165 -0
- package/dist/typescript-too-old.d.ts +18 -0
- package/package.json +90 -6
- package/src/MdastExport.ts +545 -0
- package/src/MdastExportExtension.ts +122 -0
- package/src/MdastExtension.ts +30 -0
- package/src/MdastGfmExtension.ts +38 -0
- package/src/MdastImport.ts +236 -0
- package/src/MdastImportExtension.ts +635 -0
- package/src/MdastShortcuts.ts +453 -0
- package/src/MdastStream.ts +187 -0
- package/src/MdastTableExtension.ts +144 -0
- package/src/compile.ts +44 -0
- package/src/handlers.ts +648 -0
- package/src/index.ts +69 -0
- package/src/state.ts +124 -0
- package/src/types.ts +197 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {MdastExportHandler, MdastImportHandler} from './types';
|
|
10
|
+
import type {AlignType, Table, TableCell, TableRow} from 'mdast';
|
|
11
|
+
|
|
12
|
+
import {configExtension} from '@lexical/extension';
|
|
13
|
+
import {
|
|
14
|
+
$createTableCellNode,
|
|
15
|
+
$createTableNode,
|
|
16
|
+
$createTableRowNode,
|
|
17
|
+
$isTableCellNode,
|
|
18
|
+
$isTableNode,
|
|
19
|
+
$isTableRowNode,
|
|
20
|
+
TableCellHeaderStates,
|
|
21
|
+
TableCellNode,
|
|
22
|
+
TableNode,
|
|
23
|
+
TableRowNode,
|
|
24
|
+
} from '@lexical/table';
|
|
25
|
+
import {
|
|
26
|
+
$createParagraphNode,
|
|
27
|
+
$getState,
|
|
28
|
+
$isElementNode,
|
|
29
|
+
$setState,
|
|
30
|
+
createState,
|
|
31
|
+
defineExtension,
|
|
32
|
+
} from 'lexical';
|
|
33
|
+
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table';
|
|
34
|
+
import {gfmTable} from 'micromark-extension-gfm-table';
|
|
35
|
+
|
|
36
|
+
import {$append} from './handlers';
|
|
37
|
+
import {MdastImportExtension} from './MdastImportExtension';
|
|
38
|
+
|
|
39
|
+
/** The per-column alignment (`| :-: |`) a table's delimiter row declared. */
|
|
40
|
+
const tableAlignState = /* @__PURE__ */ createState('mdastTableAlign', {
|
|
41
|
+
parse: (v): AlignType[] =>
|
|
42
|
+
Array.isArray(v)
|
|
43
|
+
? v.map(a => (a === 'center' || a === 'left' || a === 'right' ? a : null))
|
|
44
|
+
: [],
|
|
45
|
+
resetOnCopyNode: true,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const $importTable: MdastImportHandler<Table> = (node, ctx) => {
|
|
49
|
+
const table = $createTableNode();
|
|
50
|
+
if (node.align && node.align.some(a => a != null)) {
|
|
51
|
+
$setState(table, tableAlignState, node.align);
|
|
52
|
+
}
|
|
53
|
+
node.children.forEach((row, rowIndex) => {
|
|
54
|
+
const rowNode = $createTableRowNode();
|
|
55
|
+
for (const cell of row.children) {
|
|
56
|
+
const cellNode = $createTableCellNode(
|
|
57
|
+
rowIndex === 0
|
|
58
|
+
? TableCellHeaderStates.ROW
|
|
59
|
+
: TableCellHeaderStates.NO_STATUS,
|
|
60
|
+
);
|
|
61
|
+
const paragraph = $createParagraphNode();
|
|
62
|
+
$append(paragraph, ctx.importChildren(cell));
|
|
63
|
+
$append(cellNode, [paragraph]);
|
|
64
|
+
$append(rowNode, [cellNode]);
|
|
65
|
+
}
|
|
66
|
+
$append(table, [rowNode]);
|
|
67
|
+
});
|
|
68
|
+
return table;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const $exportTable: MdastExportHandler = (node, ctx) => {
|
|
72
|
+
if (!$isTableNode(node)) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const rows: TableRow[] = [];
|
|
76
|
+
for (const row of node.getChildren()) {
|
|
77
|
+
// Structural iteration bypasses the walk's selection filter, so rows a
|
|
78
|
+
// selection export does not reach are skipped here. Cells stay: dropping
|
|
79
|
+
// one would shift the remaining cells into other columns.
|
|
80
|
+
if (!$isTableRowNode(row) || !ctx.isIncluded(row)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const cells: TableCell[] = [];
|
|
84
|
+
for (const cell of row.getChildren()) {
|
|
85
|
+
if (!$isTableCellNode(cell)) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const children: TableCell['children'] = [];
|
|
89
|
+
for (const child of cell.getChildren()) {
|
|
90
|
+
if ($isElementNode(child)) {
|
|
91
|
+
// GFM cells hold a single line of phrasing content; multiple block
|
|
92
|
+
// children (paragraphs from Enter inside the cell) are joined with
|
|
93
|
+
// hard breaks, which gfm-table serializes as spaces inside the cell.
|
|
94
|
+
if (children.length > 0) {
|
|
95
|
+
children.push({type: 'break'});
|
|
96
|
+
}
|
|
97
|
+
children.push(...ctx.exportInline(child));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
cells.push({children, type: 'tableCell'});
|
|
101
|
+
}
|
|
102
|
+
rows.push({children: cells, type: 'tableRow'});
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
align: $getState(node, tableAlignState),
|
|
106
|
+
children: rows,
|
|
107
|
+
type: 'table',
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* GFM tables, mapped to `@lexical/table` nodes. Opt-in (not part of
|
|
113
|
+
* {@link MdastCommonMarkExtension}) because it pulls in the `@lexical/table`
|
|
114
|
+
* nodes it ships. The first table row is treated as the header row in both
|
|
115
|
+
* directions.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* import {MdastShortcutsExtension, MdastTableExtension} from '@lexical/mdast';
|
|
120
|
+
* import {buildEditorFromExtensions} from '@lexical/extension';
|
|
121
|
+
* import {defineExtension} from 'lexical';
|
|
122
|
+
*
|
|
123
|
+
* const editor = buildEditorFromExtensions(
|
|
124
|
+
* defineExtension({
|
|
125
|
+
* dependencies: [MdastShortcutsExtension, MdastTableExtension],
|
|
126
|
+
* name: '[root]',
|
|
127
|
+
* }),
|
|
128
|
+
* );
|
|
129
|
+
* ```
|
|
130
|
+
* @experimental
|
|
131
|
+
*/
|
|
132
|
+
export const MdastTableExtension = /* @__PURE__ */ defineExtension({
|
|
133
|
+
dependencies: [
|
|
134
|
+
/* @__PURE__ */ configExtension(MdastImportExtension, {
|
|
135
|
+
exportRules: [{$export: $exportTable, type: 'table'}],
|
|
136
|
+
importRules: [{$import: $importTable, type: 'table'}],
|
|
137
|
+
mdastExtensions: [/* @__PURE__ */ gfmTableFromMarkdown()],
|
|
138
|
+
micromarkExtensions: [/* @__PURE__ */ gfmTable()],
|
|
139
|
+
toMarkdownExtensions: [/* @__PURE__ */ gfmTableToMarkdown()],
|
|
140
|
+
}),
|
|
141
|
+
],
|
|
142
|
+
name: '@lexical/mdast/Table',
|
|
143
|
+
nodes: [TableNode, TableRowNode, TableCellNode],
|
|
144
|
+
});
|
package/src/compile.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {MdastConfig} from './MdastImportExtension';
|
|
10
|
+
import type {CompiledMdast} from './types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Compiles the raw contribution arrays held in {@link MdastConfig} into the
|
|
14
|
+
* indexed registry used at runtime. Rules earlier in the arrays win for a
|
|
15
|
+
* given node `type`; since {@link MdastImportExtension}'s `mergeConfig` prepends the
|
|
16
|
+
* rules contributed by extensions merged later (closer to the editor root),
|
|
17
|
+
* those higher-priority rules take precedence — mirroring the dispatch order
|
|
18
|
+
* of `@lexical/html`'s `DOMImportExtension`.
|
|
19
|
+
*/
|
|
20
|
+
export function compileMdast(config: MdastConfig): CompiledMdast {
|
|
21
|
+
const importHandlers: CompiledMdast['importHandlers'] = new Map();
|
|
22
|
+
const exportHandlers: CompiledMdast['exportHandlers'] = new Map();
|
|
23
|
+
|
|
24
|
+
for (const rule of config.importRules) {
|
|
25
|
+
if (!importHandlers.has(rule.type)) {
|
|
26
|
+
importHandlers.set(rule.type, rule.$import);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const rule of config.exportRules) {
|
|
30
|
+
if (!exportHandlers.has(rule.type)) {
|
|
31
|
+
exportHandlers.set(rule.type, rule.$export);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
exportHandlers,
|
|
37
|
+
importHandlers,
|
|
38
|
+
inlineShortcutTriggers: new Set(config.inlineShortcutTriggers),
|
|
39
|
+
inlineShortcutTypes: new Set(config.inlineShortcutTypes),
|
|
40
|
+
mdastExtensions: [...config.mdastExtensions],
|
|
41
|
+
micromarkExtensions: [...config.micromarkExtensions],
|
|
42
|
+
toMarkdownExtensions: [...config.toMarkdownExtensions],
|
|
43
|
+
};
|
|
44
|
+
}
|