@kerebron/extension-lsp 0.4.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 +23 -0
- package/README.md +82 -0
- package/esm/_dnt.polyfills.d.ts +101 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +127 -0
- package/esm/_dnt.shims.d.ts +6 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +61 -0
- package/esm/editor/src/CoreEditor.d.ts +40 -0
- package/esm/editor/src/CoreEditor.d.ts.map +1 -0
- package/esm/editor/src/CoreEditor.js +252 -0
- package/esm/editor/src/DummyEditorView.d.ts +60 -0
- package/esm/editor/src/DummyEditorView.d.ts.map +1 -0
- package/esm/editor/src/DummyEditorView.js +277 -0
- package/esm/editor/src/Extension.d.ts +31 -0
- package/esm/editor/src/Extension.d.ts.map +1 -0
- package/esm/editor/src/Extension.js +53 -0
- package/esm/editor/src/ExtensionManager.d.ts +32 -0
- package/esm/editor/src/ExtensionManager.d.ts.map +1 -0
- package/esm/editor/src/ExtensionManager.js +260 -0
- package/esm/editor/src/Mark.d.ts +23 -0
- package/esm/editor/src/Mark.d.ts.map +1 -0
- package/esm/editor/src/Mark.js +51 -0
- package/esm/editor/src/Node.d.ts +32 -0
- package/esm/editor/src/Node.d.ts.map +1 -0
- package/esm/editor/src/Node.js +60 -0
- package/esm/editor/src/commands/CommandManager.d.ts +23 -0
- package/esm/editor/src/commands/CommandManager.d.ts.map +1 -0
- package/esm/editor/src/commands/CommandManager.js +118 -0
- package/esm/editor/src/commands/baseCommandFactories.d.ts +3 -0
- package/esm/editor/src/commands/baseCommandFactories.d.ts.map +1 -0
- package/esm/editor/src/commands/baseCommandFactories.js +836 -0
- package/esm/editor/src/commands/createChainableState.d.ts +3 -0
- package/esm/editor/src/commands/createChainableState.d.ts.map +1 -0
- package/esm/editor/src/commands/createChainableState.js +29 -0
- package/esm/editor/src/commands/keyCommandFactories.d.ts +3 -0
- package/esm/editor/src/commands/keyCommandFactories.d.ts.map +1 -0
- package/esm/editor/src/commands/keyCommandFactories.js +10 -0
- package/esm/editor/src/commands/mod.d.ts +7 -0
- package/esm/editor/src/commands/mod.d.ts.map +1 -0
- package/esm/editor/src/commands/mod.js +76 -0
- package/esm/editor/src/commands/replaceCommandFactories.d.ts +3 -0
- package/esm/editor/src/commands/replaceCommandFactories.d.ts.map +1 -0
- package/esm/editor/src/commands/replaceCommandFactories.js +94 -0
- package/esm/editor/src/commands/types.d.ts +18 -0
- package/esm/editor/src/commands/types.d.ts.map +1 -0
- package/esm/editor/src/commands/types.js +1 -0
- package/esm/editor/src/mod.d.ts +9 -0
- package/esm/editor/src/mod.d.ts.map +1 -0
- package/esm/editor/src/mod.js +8 -0
- package/esm/editor/src/nodeToTreeString.d.ts +10 -0
- package/esm/editor/src/nodeToTreeString.d.ts.map +1 -0
- package/esm/editor/src/nodeToTreeString.js +74 -0
- package/esm/editor/src/plugins/TrackSelecionPlugin.d.ts +6 -0
- package/esm/editor/src/plugins/TrackSelecionPlugin.d.ts.map +1 -0
- package/esm/editor/src/plugins/TrackSelecionPlugin.js +24 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.js +163 -0
- package/esm/editor/src/plugins/keymap/keymap.d.ts +11 -0
- package/esm/editor/src/plugins/keymap/keymap.d.ts.map +1 -0
- package/esm/editor/src/plugins/keymap/keymap.js +125 -0
- package/esm/editor/src/plugins/keymap/w3c-keyname.d.ts +4 -0
- package/esm/editor/src/plugins/keymap/w3c-keyname.d.ts.map +1 -0
- package/esm/editor/src/plugins/keymap/w3c-keyname.js +124 -0
- package/esm/editor/src/types.d.ts +52 -0
- package/esm/editor/src/types.d.ts.map +1 -0
- package/esm/editor/src/types.js +1 -0
- package/esm/editor/src/ui.d.ts +15 -0
- package/esm/editor/src/ui.d.ts.map +1 -0
- package/esm/editor/src/ui.js +16 -0
- package/esm/editor/src/utilities/SmartOutput.d.ts +41 -0
- package/esm/editor/src/utilities/SmartOutput.d.ts.map +1 -0
- package/esm/editor/src/utilities/SmartOutput.js +228 -0
- package/esm/editor/src/utilities/createNodeFromContent.d.ts +9 -0
- package/esm/editor/src/utilities/createNodeFromContent.d.ts.map +1 -0
- package/esm/editor/src/utilities/createNodeFromContent.js +32 -0
- package/esm/editor/src/utilities/getHtmlAttributes.d.ts +9 -0
- package/esm/editor/src/utilities/getHtmlAttributes.d.ts.map +1 -0
- package/esm/editor/src/utilities/getHtmlAttributes.js +47 -0
- package/esm/editor/src/utilities/getShadowRoot.d.ts +2 -0
- package/esm/editor/src/utilities/getShadowRoot.d.ts.map +1 -0
- package/esm/editor/src/utilities/getShadowRoot.js +16 -0
- package/esm/editor/src/utilities/mod.d.ts +6 -0
- package/esm/editor/src/utilities/mod.d.ts.map +1 -0
- package/esm/editor/src/utilities/mod.js +5 -0
- package/esm/editor/src/utilities/toRawTextResult.d.ts +3 -0
- package/esm/editor/src/utilities/toRawTextResult.d.ts.map +1 -0
- package/esm/editor/src/utilities/toRawTextResult.js +21 -0
- package/esm/extension-autocomplete/src/AutocompletePlugin.d.ts +8 -0
- package/esm/extension-autocomplete/src/AutocompletePlugin.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/AutocompletePlugin.js +232 -0
- package/esm/extension-autocomplete/src/DefaultRenderer.d.ts +17 -0
- package/esm/extension-autocomplete/src/DefaultRenderer.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/DefaultRenderer.js +137 -0
- package/esm/extension-autocomplete/src/ExtensionAutocomplete.d.ts +26 -0
- package/esm/extension-autocomplete/src/ExtensionAutocomplete.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/ExtensionAutocomplete.js +30 -0
- package/esm/extension-autocomplete/src/createDefaultMatcher.d.ts +11 -0
- package/esm/extension-autocomplete/src/createDefaultMatcher.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/createDefaultMatcher.js +58 -0
- package/esm/extension-autocomplete/src/createRegexMatcher.d.ts +4 -0
- package/esm/extension-autocomplete/src/createRegexMatcher.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/createRegexMatcher.js +50 -0
- package/esm/extension-autocomplete/src/mod.d.ts +3 -0
- package/esm/extension-autocomplete/src/mod.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/mod.js +2 -0
- package/esm/extension-autocomplete/src/types.d.ts +60 -0
- package/esm/extension-autocomplete/src/types.d.ts.map +1 -0
- package/esm/extension-autocomplete/src/types.js +1 -0
- package/esm/extension-basic-editor/src/ExtensionHtml.d.ts +15 -0
- package/esm/extension-basic-editor/src/ExtensionHtml.d.ts.map +1 -0
- package/esm/extension-basic-editor/src/ExtensionHtml.js +108 -0
- package/esm/extension-lsp/src/DiagnosticPlugin.d.ts +32 -0
- package/esm/extension-lsp/src/DiagnosticPlugin.d.ts.map +1 -0
- package/esm/extension-lsp/src/DiagnosticPlugin.js +131 -0
- package/esm/extension-lsp/src/ExtensionLsp.d.ts +25 -0
- package/esm/extension-lsp/src/ExtensionLsp.d.ts.map +1 -0
- package/esm/extension-lsp/src/ExtensionLsp.js +126 -0
- package/esm/extension-lsp/src/LSPClient.d.ts +56 -0
- package/esm/extension-lsp/src/LSPClient.d.ts.map +1 -0
- package/esm/extension-lsp/src/LSPClient.js +449 -0
- package/esm/extension-lsp/src/LspWebSocketTransport.d.ts +20 -0
- package/esm/extension-lsp/src/LspWebSocketTransport.d.ts.map +1 -0
- package/esm/extension-lsp/src/LspWebSocketTransport.js +137 -0
- package/esm/extension-lsp/src/computeIncrementalChanges.d.ts +8 -0
- package/esm/extension-lsp/src/computeIncrementalChanges.d.ts.map +1 -0
- package/esm/extension-lsp/src/computeIncrementalChanges.js +82 -0
- package/esm/extension-lsp/src/createLspAutocomplete.d.ts +23 -0
- package/esm/extension-lsp/src/createLspAutocomplete.d.ts.map +1 -0
- package/esm/extension-lsp/src/createLspAutocomplete.js +68 -0
- package/esm/extension-lsp/src/mod.d.ts +5 -0
- package/esm/extension-lsp/src/mod.d.ts.map +1 -0
- package/esm/extension-lsp/src/mod.js +4 -0
- package/esm/extension-lsp/src/types.d.ts +20 -0
- package/esm/extension-lsp/src/types.d.ts.map +1 -0
- package/esm/extension-lsp/src/types.js +1 -0
- package/esm/extension-lsp/src/workspace.d.ts +62 -0
- package/esm/extension-lsp/src/workspace.d.ts.map +1 -0
- package/esm/extension-lsp/src/workspace.js +168 -0
- package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.d.ts +36 -0
- package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.d.ts.map +1 -0
- package/esm/extension-markdown/src/DocumentMarkdownInlineTokenizer.js +240 -0
- package/esm/extension-markdown/src/DocumentMarkdownTokenizer.d.ts +26 -0
- package/esm/extension-markdown/src/DocumentMarkdownTokenizer.d.ts.map +1 -0
- package/esm/extension-markdown/src/DocumentMarkdownTokenizer.js +115 -0
- package/esm/extension-markdown/src/ExtensionMarkdown.d.ts +22 -0
- package/esm/extension-markdown/src/ExtensionMarkdown.d.ts.map +1 -0
- package/esm/extension-markdown/src/ExtensionMarkdown.js +48 -0
- package/esm/extension-markdown/src/MarkdownParser.d.ts +61 -0
- package/esm/extension-markdown/src/MarkdownParser.d.ts.map +1 -0
- package/esm/extension-markdown/src/MarkdownParser.js +249 -0
- package/esm/extension-markdown/src/MarkdownSerializer.d.ts +42 -0
- package/esm/extension-markdown/src/MarkdownSerializer.d.ts.map +1 -0
- package/esm/extension-markdown/src/MarkdownSerializer.js +325 -0
- package/esm/extension-markdown/src/PositionMapper.d.ts +15 -0
- package/esm/extension-markdown/src/PositionMapper.d.ts.map +1 -0
- package/esm/extension-markdown/src/PositionMapper.js +100 -0
- package/esm/extension-markdown/src/TokenSource.d.ts +11 -0
- package/esm/extension-markdown/src/TokenSource.d.ts.map +1 -0
- package/esm/extension-markdown/src/TokenSource.js +39 -0
- package/esm/extension-markdown/src/mdToPmConverter.d.ts +5 -0
- package/esm/extension-markdown/src/mdToPmConverter.d.ts.map +1 -0
- package/esm/extension-markdown/src/mdToPmConverter.js +111 -0
- package/esm/extension-markdown/src/pmToMdConverter.d.ts +16 -0
- package/esm/extension-markdown/src/pmToMdConverter.d.ts.map +1 -0
- package/esm/extension-markdown/src/pmToMdConverter.js +433 -0
- package/esm/extension-markdown/src/token_handlers/basic_token_handlers.d.ts +4 -0
- package/esm/extension-markdown/src/token_handlers/basic_token_handlers.d.ts.map +1 -0
- package/esm/extension-markdown/src/token_handlers/basic_token_handlers.js +151 -0
- package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.d.ts +3 -0
- package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.d.ts.map +1 -0
- package/esm/extension-markdown/src/token_handlers/footnote_token_handlers.js +34 -0
- package/esm/extension-markdown/src/token_handlers/inline_token_handlers.d.ts +6 -0
- package/esm/extension-markdown/src/token_handlers/inline_token_handlers.d.ts.map +1 -0
- package/esm/extension-markdown/src/token_handlers/inline_token_handlers.js +380 -0
- package/esm/extension-markdown/src/token_handlers/lists_token_handlers.d.ts +3 -0
- package/esm/extension-markdown/src/token_handlers/lists_token_handlers.d.ts.map +1 -0
- package/esm/extension-markdown/src/token_handlers/lists_token_handlers.js +323 -0
- package/esm/extension-markdown/src/token_handlers/table_token_handlers.d.ts +9 -0
- package/esm/extension-markdown/src/token_handlers/table_token_handlers.d.ts.map +1 -0
- package/esm/extension-markdown/src/token_handlers/table_token_handlers.js +308 -0
- package/esm/extension-markdown/src/treeSitterTokenizer.d.ts +5 -0
- package/esm/extension-markdown/src/treeSitterTokenizer.d.ts.map +1 -0
- package/esm/extension-markdown/src/treeSitterTokenizer.js +769 -0
- package/esm/extension-markdown/src/types.d.ts +28 -0
- package/esm/extension-markdown/src/types.d.ts.map +1 -0
- package/esm/extension-markdown/src/types.js +131 -0
- package/esm/extension-markdown/src/utils.d.ts +8 -0
- package/esm/extension-markdown/src/utils.d.ts.map +1 -0
- package/esm/extension-markdown/src/utils.js +86 -0
- package/esm/package.json +3 -0
- package/esm/tree-sitter-shim/src/main.d.ts +15 -0
- package/esm/tree-sitter-shim/src/main.d.ts.map +1 -0
- package/esm/tree-sitter-shim/src/main.js +25 -0
- package/esm/tree-sitter-shim/src/tree_sitter/node.d.ts +6 -0
- package/esm/tree-sitter-shim/src/tree_sitter/node.d.ts.map +1 -0
- package/esm/tree-sitter-shim/src/tree_sitter/node.js +1 -0
- package/esm/tree-sitter-shim/src/tree_sitter/parser.d.ts +7 -0
- package/esm/tree-sitter-shim/src/tree_sitter/parser.d.ts.map +1 -0
- package/esm/tree-sitter-shim/src/tree_sitter/parser.js +5 -0
- package/esm/tree-sitter-shim/src/tree_sitter/tree.d.ts +6 -0
- package/esm/tree-sitter-shim/src/tree_sitter/tree.d.ts.map +1 -0
- package/esm/tree-sitter-shim/src/tree_sitter/tree.js +1 -0
- package/esm/wasm/src/mod.d.ts +12 -0
- package/esm/wasm/src/mod.d.ts.map +1 -0
- package/esm/wasm/src/mod.js +48 -0
- package/esm/wasm/wasm.d.ts +138 -0
- package/esm/wasm/wasm.d.ts.map +1 -0
- package/esm/wasm/wasm.js +120 -0
- package/package.json +31 -0
|
@@ -0,0 +1,769 @@
|
|
|
1
|
+
import { createParser } from '../../tree-sitter-shim/src/main.js';
|
|
2
|
+
import { NESTING_CLOSING, NESTING_OPENING, NESTING_SELF_CLOSING, Token, } from './types.js';
|
|
3
|
+
import { fetchWasm, getLangTreeSitter } from '../../wasm/src/mod.js';
|
|
4
|
+
function treeToTokens(tree, inlineParser) {
|
|
5
|
+
const retVal = [];
|
|
6
|
+
let blockLevel = 0;
|
|
7
|
+
const pushInlineNode = (someInlineToken, debug) => {
|
|
8
|
+
const lastBlockToken = retVal[retVal.length - 1];
|
|
9
|
+
if (lastBlockToken?.type === 'inline') {
|
|
10
|
+
if (!lastBlockToken.children) {
|
|
11
|
+
lastBlockToken.children = [];
|
|
12
|
+
}
|
|
13
|
+
lastBlockToken.children.push(someInlineToken);
|
|
14
|
+
lastBlockToken.content = someInlineToken.content;
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const blockToken = new Token('inline', '', NESTING_SELF_CLOSING);
|
|
18
|
+
blockToken.info = debug;
|
|
19
|
+
blockToken.level = blockLevel;
|
|
20
|
+
blockToken.children = [someInlineToken];
|
|
21
|
+
blockToken.content = blockToken.children
|
|
22
|
+
.map((c) => c.content || '').join('');
|
|
23
|
+
retVal.push(blockToken);
|
|
24
|
+
};
|
|
25
|
+
const walkInline = (children, startPosition = { row: 0, column: 0 }) => {
|
|
26
|
+
for (const node of children) {
|
|
27
|
+
const map = [
|
|
28
|
+
startPosition.row + node.startPosition?.row,
|
|
29
|
+
startPosition.row + node.endPosition?.row,
|
|
30
|
+
startPosition.column + node.startPosition?.column,
|
|
31
|
+
(+node.endPosition.row === +node.startPosition.row
|
|
32
|
+
? startPosition.column
|
|
33
|
+
: 0) + node.endPosition?.column,
|
|
34
|
+
];
|
|
35
|
+
if (node.type.length === 1) { // single letter type is text
|
|
36
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
37
|
+
token.map = map;
|
|
38
|
+
token.meta = 'noEscText';
|
|
39
|
+
token.content = node.text ?? '';
|
|
40
|
+
pushInlineNode(token, 'walkInline');
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
switch (node.type) {
|
|
44
|
+
case 'block_continuation':
|
|
45
|
+
case 'emphasis_delimiter':
|
|
46
|
+
case 'code_span_delimiter':
|
|
47
|
+
break;
|
|
48
|
+
case 'text':
|
|
49
|
+
{
|
|
50
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
51
|
+
token.map = map;
|
|
52
|
+
token.meta = 'noEscText';
|
|
53
|
+
token.content = node.text ?? '';
|
|
54
|
+
pushInlineNode(token, 'text');
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case 'latex_block':
|
|
59
|
+
{
|
|
60
|
+
const delimiter = node.children
|
|
61
|
+
.find((c) => c.type === 'latex_span_delimiter');
|
|
62
|
+
const content = node.children
|
|
63
|
+
.filter((c) => c.type !== 'latex_span_delimiter')
|
|
64
|
+
.map((c) => c.text)
|
|
65
|
+
.join('');
|
|
66
|
+
if (delimiter?.text === '$$') {
|
|
67
|
+
const token = new Token('fence', 'pre', NESTING_SELF_CLOSING);
|
|
68
|
+
token.level = blockLevel;
|
|
69
|
+
token.markup = '$$';
|
|
70
|
+
token.info = 'latex';
|
|
71
|
+
token.content = content.trim();
|
|
72
|
+
retVal.push(token);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const token = new Token('math', '', NESTING_SELF_CLOSING);
|
|
76
|
+
token.map = map;
|
|
77
|
+
token.meta = 'noEscText';
|
|
78
|
+
token.content = content;
|
|
79
|
+
pushInlineNode(token, 'latex_block');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case 'whitespace':
|
|
84
|
+
if (node.text === '\n') {
|
|
85
|
+
const token = new Token('hardbreak', 'br', NESTING_SELF_CLOSING);
|
|
86
|
+
token.map = map;
|
|
87
|
+
token.content = node.text ?? '';
|
|
88
|
+
pushInlineNode(token, 'whitespace \n');
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
if (node.text && node.text.match(/^\s+$/)) {
|
|
92
|
+
const token = new Token('text', 'br', NESTING_SELF_CLOSING);
|
|
93
|
+
token.map = map;
|
|
94
|
+
token.meta = 'noEscText';
|
|
95
|
+
token.content = node.text ?? '';
|
|
96
|
+
pushInlineNode(token, 'whitespace space');
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (node.text.trim().length > 0) {
|
|
100
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
101
|
+
token.map = map;
|
|
102
|
+
token.meta = 'noEscText';
|
|
103
|
+
token.content = node.text ?? '';
|
|
104
|
+
pushInlineNode(token, 'whitespace other');
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
case 'shortcut_link':
|
|
109
|
+
{
|
|
110
|
+
const tokenName = 'link';
|
|
111
|
+
const tagName = 'a';
|
|
112
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
113
|
+
const destination = node.children
|
|
114
|
+
.find((c) => c.type === 'link_destination');
|
|
115
|
+
openToken.attrSet('href', destination?.text || '');
|
|
116
|
+
const title = node.children
|
|
117
|
+
.find((c) => c.type === 'link_title');
|
|
118
|
+
if (title) {
|
|
119
|
+
openToken.attrSet('title', title.text || '');
|
|
120
|
+
}
|
|
121
|
+
pushInlineNode(openToken, 'shortcut_link');
|
|
122
|
+
node.children
|
|
123
|
+
.filter((c) => c.type === 'link_text')
|
|
124
|
+
.forEach((c) => {
|
|
125
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
126
|
+
token.map = map;
|
|
127
|
+
token.meta = 'noEscText';
|
|
128
|
+
token.content = c.text ?? '';
|
|
129
|
+
pushInlineNode(token, 'link_text');
|
|
130
|
+
});
|
|
131
|
+
// walkInline(node.children);
|
|
132
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
133
|
+
pushInlineNode(closeToken, '/shortcut_link');
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
case 'inline_link':
|
|
137
|
+
{
|
|
138
|
+
const tokenName = 'link';
|
|
139
|
+
const tagName = 'a';
|
|
140
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
141
|
+
const destination = node.children
|
|
142
|
+
.find((c) => c.type === 'link_destination');
|
|
143
|
+
openToken.attrSet('href', destination?.text || '');
|
|
144
|
+
const title = node.children
|
|
145
|
+
.find((c) => c.type === 'link_title');
|
|
146
|
+
if (title) {
|
|
147
|
+
openToken.attrSet('title', title.text || '');
|
|
148
|
+
}
|
|
149
|
+
pushInlineNode(openToken, 'inline_link');
|
|
150
|
+
node.children
|
|
151
|
+
.filter((c) => c.type === 'link_text')
|
|
152
|
+
.forEach((c) => {
|
|
153
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
154
|
+
token.map = map;
|
|
155
|
+
token.meta = 'noEscText';
|
|
156
|
+
token.content = c.text ?? '';
|
|
157
|
+
pushInlineNode(token, 'inline_link_txt');
|
|
158
|
+
});
|
|
159
|
+
// walkInline(node.children);
|
|
160
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
161
|
+
pushInlineNode(closeToken, '/inline_link');
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
case 'image':
|
|
165
|
+
{
|
|
166
|
+
const tokenName = 'image';
|
|
167
|
+
const tagName = 'img';
|
|
168
|
+
const token = new Token(tokenName, tagName, NESTING_SELF_CLOSING);
|
|
169
|
+
const image_description = node.children
|
|
170
|
+
.find((c) => c.type === 'image_description');
|
|
171
|
+
token.attrSet('alt', image_description?.text || '');
|
|
172
|
+
const destination = node.children
|
|
173
|
+
.find((c) => c.type === 'link_destination');
|
|
174
|
+
token.attrSet('src', destination?.text || '');
|
|
175
|
+
const title = node.children
|
|
176
|
+
.find((c) => c.type === 'link_title');
|
|
177
|
+
if (title) {
|
|
178
|
+
token.attrSet('title', title.text || '');
|
|
179
|
+
}
|
|
180
|
+
pushInlineNode(token, 'image');
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
case 'strong_emphasis':
|
|
184
|
+
{
|
|
185
|
+
const delimiter = node.children
|
|
186
|
+
.find((c) => c.type === 'emphasis_delimiter');
|
|
187
|
+
const tokenName = delimiter?.text === '_' ? 'underline' : 'strong';
|
|
188
|
+
const tagName = delimiter?.text === '_' ? 'u' : 'strong';
|
|
189
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
190
|
+
if (delimiter?.text === '_') {
|
|
191
|
+
openToken.markup = '__';
|
|
192
|
+
}
|
|
193
|
+
pushInlineNode(openToken, 'strongem');
|
|
194
|
+
walkInline(node.children);
|
|
195
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
196
|
+
if (delimiter?.text === '_') {
|
|
197
|
+
closeToken.markup = '__';
|
|
198
|
+
}
|
|
199
|
+
pushInlineNode(closeToken, '/strongem');
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
case 'emphasis':
|
|
203
|
+
{
|
|
204
|
+
const delimiter = node.children
|
|
205
|
+
.find((c) => c.type === 'emphasis_delimiter');
|
|
206
|
+
const children = node.children
|
|
207
|
+
.filter((c) => c.type !== 'emphasis_delimiter');
|
|
208
|
+
const tokenName = delimiter?.text === '_' ? 'underline' : 'em';
|
|
209
|
+
const tagName = delimiter?.text === '_' ? 'u' : 'em';
|
|
210
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
211
|
+
pushInlineNode(openToken, 'em');
|
|
212
|
+
walkInline(children);
|
|
213
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
214
|
+
pushInlineNode(closeToken, '/em');
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case 'strikethrough':
|
|
218
|
+
{
|
|
219
|
+
const tokenName = 'strike';
|
|
220
|
+
const tagName = 'strike';
|
|
221
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
222
|
+
pushInlineNode(openToken, 's');
|
|
223
|
+
walkInline(node.children);
|
|
224
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
225
|
+
pushInlineNode(closeToken, '/s');
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
case 'code_span':
|
|
229
|
+
{
|
|
230
|
+
const tokenName = 'code';
|
|
231
|
+
const tagName = 'code';
|
|
232
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
233
|
+
pushInlineNode(openToken, 'code');
|
|
234
|
+
walkInline(node.children);
|
|
235
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
236
|
+
pushInlineNode(closeToken, '/code');
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case 'html_tag':
|
|
240
|
+
{
|
|
241
|
+
const tokenName = 'html_block';
|
|
242
|
+
const tagName = '';
|
|
243
|
+
const token = new Token(tokenName, tagName, NESTING_SELF_CLOSING);
|
|
244
|
+
token.content = node.text;
|
|
245
|
+
pushInlineNode(token, 'html_block');
|
|
246
|
+
}
|
|
247
|
+
break;
|
|
248
|
+
default:
|
|
249
|
+
console.debug('inline_node', node);
|
|
250
|
+
throw new Error(`Unhandled inline node type: ${node.type}`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
const walkRecursive = (node, ctx = { tableRowType: 'tbody', cellNo: 0, cellAlign: [] }) => {
|
|
255
|
+
if (!node) {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (node.type === 'inline') {
|
|
259
|
+
const inlineText = node.text;
|
|
260
|
+
if (!inlineText) {
|
|
261
|
+
throw new Error('!inlineText');
|
|
262
|
+
}
|
|
263
|
+
if (node.children.length > 0) {
|
|
264
|
+
walkInline(node.children, node.startPosition);
|
|
265
|
+
}
|
|
266
|
+
else {
|
|
267
|
+
const inlineTree = inlineParser.parse(inlineText);
|
|
268
|
+
if (!inlineTree) {
|
|
269
|
+
throw new Error('!inlineTree');
|
|
270
|
+
}
|
|
271
|
+
walkInline(inlineTree?.rootNode.children, node.startPosition);
|
|
272
|
+
}
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (!node.children || node.children.length === 0) {
|
|
276
|
+
if (node.type === 'text' || node.type.length === 1) {
|
|
277
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
278
|
+
token.map = [
|
|
279
|
+
+node.startPosition?.row,
|
|
280
|
+
+node.endPosition?.row,
|
|
281
|
+
+node.startPosition?.column,
|
|
282
|
+
+node.endPosition?.column,
|
|
283
|
+
];
|
|
284
|
+
token.meta = 'noEscText';
|
|
285
|
+
token.content = node.text ?? '';
|
|
286
|
+
pushInlineNode(token, 'txt2');
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (node.type === 'whitespace') {
|
|
290
|
+
if (blockLevel === 0) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (node.text.match(/^\n+$/)) {
|
|
294
|
+
// const token = new Token('hardbreak', 'br', NESTING_SELF_CLOSING);
|
|
295
|
+
// token.map = [ +node.startPosition?.row, +node.endPosition?.row, +node.startPosition?.column, +node.endPosition?.column ];
|
|
296
|
+
// token.content = node.text ?? '';
|
|
297
|
+
// retVal.push(token);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (node.text && node.text.match(/^\s+$/)) {
|
|
301
|
+
const token = new Token('text', 'br', NESTING_SELF_CLOSING);
|
|
302
|
+
token.map = [
|
|
303
|
+
+node.startPosition?.row,
|
|
304
|
+
+node.endPosition?.row,
|
|
305
|
+
+node.startPosition?.column,
|
|
306
|
+
+node.endPosition?.column,
|
|
307
|
+
];
|
|
308
|
+
token.meta = 'noEscText';
|
|
309
|
+
token.content = node.text ?? '';
|
|
310
|
+
pushInlineNode(token, 'txt3');
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (node.text.trim().length > 0) {
|
|
314
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
315
|
+
token.map = [
|
|
316
|
+
+node.startPosition?.row,
|
|
317
|
+
+node.endPosition?.row,
|
|
318
|
+
+node.startPosition?.column,
|
|
319
|
+
+node.endPosition?.column,
|
|
320
|
+
];
|
|
321
|
+
token.meta = 'noEscText';
|
|
322
|
+
token.content = node.text ?? '';
|
|
323
|
+
pushInlineNode(token, 'txt4');
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
switch (node.type) {
|
|
329
|
+
case 'block_continuation':
|
|
330
|
+
break;
|
|
331
|
+
case 'document':
|
|
332
|
+
case 'section':
|
|
333
|
+
node.children?.forEach((child) => walkRecursive(child, ctx));
|
|
334
|
+
break;
|
|
335
|
+
case 'thematic_break':
|
|
336
|
+
{
|
|
337
|
+
const token = new Token('hr', 'hr', NESTING_SELF_CLOSING);
|
|
338
|
+
token.level = blockLevel;
|
|
339
|
+
token.map = [
|
|
340
|
+
+node.startPosition?.row,
|
|
341
|
+
+node.endPosition?.row,
|
|
342
|
+
+node.startPosition?.column,
|
|
343
|
+
+node.endPosition?.column,
|
|
344
|
+
];
|
|
345
|
+
token.markup = node.text;
|
|
346
|
+
retVal.push(token);
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
case 'setext_heading':
|
|
350
|
+
// case 'setext_h2_underline':
|
|
351
|
+
// TODO
|
|
352
|
+
// TODO
|
|
353
|
+
{
|
|
354
|
+
const tokenName = 'heading';
|
|
355
|
+
const underline = node.children.find((child) => child.type.startsWith('setext_') &&
|
|
356
|
+
child.type.endsWith('_underline'));
|
|
357
|
+
// const marker = node.children.find((child) =>
|
|
358
|
+
// child.type.startsWith('atx_') && child.type.endsWith('_marker')
|
|
359
|
+
// );
|
|
360
|
+
const tagName = underline?.type.substring('setext_'.length, 'setext_'.length + 2) ||
|
|
361
|
+
'h1';
|
|
362
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
363
|
+
openToken.level = blockLevel;
|
|
364
|
+
openToken.markup = underline?.text || '#';
|
|
365
|
+
retVal.push(openToken);
|
|
366
|
+
const children = node.children.filter((c) => !c.type.startsWith('setext_'));
|
|
367
|
+
blockLevel++;
|
|
368
|
+
children.forEach((child) => walkRecursive(child, ctx));
|
|
369
|
+
blockLevel--;
|
|
370
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
371
|
+
closeToken.level = blockLevel;
|
|
372
|
+
closeToken.markup = underline?.text || '';
|
|
373
|
+
retVal.push(closeToken);
|
|
374
|
+
}
|
|
375
|
+
break;
|
|
376
|
+
case 'atx_heading':
|
|
377
|
+
{
|
|
378
|
+
const tokenName = 'heading';
|
|
379
|
+
const marker = node.children.find((child) => child.type.startsWith('atx_') && child.type.endsWith('_marker'));
|
|
380
|
+
const tagName = marker?.type.substring('atx_'.length, 'atx_'.length + 2) || 'h1';
|
|
381
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
382
|
+
openToken.level = blockLevel;
|
|
383
|
+
openToken.markup = marker?.text || '#';
|
|
384
|
+
retVal.push(openToken);
|
|
385
|
+
const children = [...node.children];
|
|
386
|
+
if (children.length > 0 && children[0].type.endsWith('_marker')) {
|
|
387
|
+
children.splice(0, 1);
|
|
388
|
+
}
|
|
389
|
+
if (children.length > 0 && children[0].type === 'whitespace') { // ' '
|
|
390
|
+
children.splice(0, 1);
|
|
391
|
+
}
|
|
392
|
+
if (children.length > 0 &&
|
|
393
|
+
children[children.length - 1].type === 'whitespace') { // /n
|
|
394
|
+
children.splice(children.length - 1, 1);
|
|
395
|
+
}
|
|
396
|
+
blockLevel++;
|
|
397
|
+
children.forEach((child) => walkRecursive(child, ctx));
|
|
398
|
+
blockLevel--;
|
|
399
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
400
|
+
closeToken.level = blockLevel;
|
|
401
|
+
retVal.push(closeToken);
|
|
402
|
+
}
|
|
403
|
+
break;
|
|
404
|
+
case 'minus_metadata':
|
|
405
|
+
{
|
|
406
|
+
const tokenName = 'frontmatter';
|
|
407
|
+
const tagName = 'frontmatter';
|
|
408
|
+
const token = new Token(tokenName, tagName, NESTING_SELF_CLOSING);
|
|
409
|
+
token.level = blockLevel;
|
|
410
|
+
token.content = node.text || '';
|
|
411
|
+
retVal.push(token);
|
|
412
|
+
}
|
|
413
|
+
break;
|
|
414
|
+
case 'fenced_code_block':
|
|
415
|
+
{
|
|
416
|
+
const tokenName = 'fence';
|
|
417
|
+
const tagName = 'pre';
|
|
418
|
+
const info = node.children.find((child) => child.type === 'info_string');
|
|
419
|
+
const token = new Token(tokenName, tagName, NESTING_SELF_CLOSING);
|
|
420
|
+
token.level = blockLevel;
|
|
421
|
+
token.markup = '```';
|
|
422
|
+
if (info) {
|
|
423
|
+
token.attrSet('lang', info.text);
|
|
424
|
+
}
|
|
425
|
+
const children = [...node.children
|
|
426
|
+
.filter((item) => item.type === 'code_fence_content')];
|
|
427
|
+
const content = children.map((item) => item.children
|
|
428
|
+
.map((inline) => inline.text || '')
|
|
429
|
+
.join('')).join('');
|
|
430
|
+
token.content = content;
|
|
431
|
+
retVal.push(token);
|
|
432
|
+
}
|
|
433
|
+
break;
|
|
434
|
+
case 'block_quote':
|
|
435
|
+
{
|
|
436
|
+
const tokenName = 'blockquote';
|
|
437
|
+
const tagName = 'blockquote';
|
|
438
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
439
|
+
openToken.level = blockLevel;
|
|
440
|
+
retVal.push(openToken);
|
|
441
|
+
blockLevel++;
|
|
442
|
+
node.children
|
|
443
|
+
?.filter((child) => !['block_quote_marker'].includes(child.type))
|
|
444
|
+
.forEach((child) => walkRecursive(child, ctx));
|
|
445
|
+
blockLevel--;
|
|
446
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
447
|
+
closeToken.level = blockLevel;
|
|
448
|
+
retVal.push(closeToken);
|
|
449
|
+
}
|
|
450
|
+
break;
|
|
451
|
+
case 'indented_code_block':
|
|
452
|
+
{
|
|
453
|
+
const tokenName = 'code_block';
|
|
454
|
+
const tagName = 'code_block';
|
|
455
|
+
const token = new Token(tokenName, tagName, NESTING_SELF_CLOSING);
|
|
456
|
+
token.level = blockLevel;
|
|
457
|
+
let indent = 0;
|
|
458
|
+
if ('indent' in node) {
|
|
459
|
+
indent = node.indent.length;
|
|
460
|
+
}
|
|
461
|
+
const lines = node.text.split('\n');
|
|
462
|
+
for (const line of lines) {
|
|
463
|
+
if (line.trim().length === 0) {
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
const m = line.match(/^ +/);
|
|
467
|
+
if (!m) {
|
|
468
|
+
indent = 0;
|
|
469
|
+
}
|
|
470
|
+
else {
|
|
471
|
+
if (indent > m[0].length) {
|
|
472
|
+
indent = m[0].length;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
const content = lines
|
|
477
|
+
.map((line) => line.substring(indent))
|
|
478
|
+
.join('\n');
|
|
479
|
+
token.attrSet('indent', '' + indent);
|
|
480
|
+
token.content = content;
|
|
481
|
+
retVal.push(token);
|
|
482
|
+
}
|
|
483
|
+
break;
|
|
484
|
+
case 'paragraph':
|
|
485
|
+
{
|
|
486
|
+
const tokenName = 'paragraph';
|
|
487
|
+
const tagName = 'p';
|
|
488
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
489
|
+
openToken.level = blockLevel;
|
|
490
|
+
retVal.push(openToken);
|
|
491
|
+
blockLevel++;
|
|
492
|
+
node.children?.forEach((child) => walkRecursive(child, ctx));
|
|
493
|
+
blockLevel--;
|
|
494
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
495
|
+
closeToken.level = blockLevel;
|
|
496
|
+
retVal.push(closeToken);
|
|
497
|
+
}
|
|
498
|
+
break;
|
|
499
|
+
case 'pipe_table_cell':
|
|
500
|
+
{
|
|
501
|
+
const tokenName = ctx.tableRowType === 'thead' ? 'th' : 'td';
|
|
502
|
+
const tagName = ctx.tableRowType === 'thead' ? 'th' : 'td';
|
|
503
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
504
|
+
openToken.attrSet('align', ctx.cellAlign[ctx.cellNo] || 'left');
|
|
505
|
+
openToken.level = blockLevel;
|
|
506
|
+
retVal.push(openToken);
|
|
507
|
+
blockLevel++;
|
|
508
|
+
{
|
|
509
|
+
const tokenName = 'paragraph';
|
|
510
|
+
const tagName = 'p';
|
|
511
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
512
|
+
openToken.level = blockLevel;
|
|
513
|
+
retVal.push(openToken);
|
|
514
|
+
const children = [...node.children];
|
|
515
|
+
while (children.length > 0) {
|
|
516
|
+
if (children[children.length - 1].type === 'whitespace') {
|
|
517
|
+
children.splice(children.length - 1, 1);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
break;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
if (children.length > 0) {
|
|
524
|
+
children
|
|
525
|
+
.forEach((child) => walkRecursive(child, ctx));
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
const map = [
|
|
529
|
+
node.startPosition?.row,
|
|
530
|
+
node.endPosition?.row,
|
|
531
|
+
node.startPosition?.column,
|
|
532
|
+
node.endPosition?.column,
|
|
533
|
+
];
|
|
534
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
535
|
+
token.map = map;
|
|
536
|
+
token.meta = 'noEscText';
|
|
537
|
+
token.content = node.text?.replace(/\s+$/, '') ?? '';
|
|
538
|
+
pushInlineNode(token, 'txt5');
|
|
539
|
+
}
|
|
540
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
541
|
+
closeToken.level = blockLevel;
|
|
542
|
+
retVal.push(closeToken);
|
|
543
|
+
}
|
|
544
|
+
blockLevel--;
|
|
545
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
546
|
+
closeToken.level = blockLevel;
|
|
547
|
+
retVal.push(closeToken);
|
|
548
|
+
ctx.cellNo++;
|
|
549
|
+
}
|
|
550
|
+
break;
|
|
551
|
+
case 'pipe_table_header':
|
|
552
|
+
case 'pipe_table_row':
|
|
553
|
+
{
|
|
554
|
+
const tokenName = 'tr';
|
|
555
|
+
const tagName = 'tr';
|
|
556
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
557
|
+
openToken.level = blockLevel;
|
|
558
|
+
retVal.push(openToken);
|
|
559
|
+
blockLevel++;
|
|
560
|
+
node.children
|
|
561
|
+
?.filter((c) => c.type === 'pipe_table_cell')
|
|
562
|
+
.forEach((child) => walkRecursive(child, ctx));
|
|
563
|
+
blockLevel--;
|
|
564
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
565
|
+
closeToken.level = blockLevel;
|
|
566
|
+
retVal.push(closeToken);
|
|
567
|
+
}
|
|
568
|
+
break;
|
|
569
|
+
case 'pipe_table':
|
|
570
|
+
{
|
|
571
|
+
const tokenName = 'table';
|
|
572
|
+
const tagName = 'table';
|
|
573
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
574
|
+
openToken.level = blockLevel;
|
|
575
|
+
retVal.push(openToken);
|
|
576
|
+
blockLevel++;
|
|
577
|
+
const delimiterRows = node.children
|
|
578
|
+
?.filter((c) => c.type === 'pipe_table_delimiter_row');
|
|
579
|
+
const cellAlign = [];
|
|
580
|
+
if (delimiterRows.length > 0) {
|
|
581
|
+
const delimiterRow = delimiterRows[0];
|
|
582
|
+
const delimiterCells = delimiterRow.children
|
|
583
|
+
?.filter((c) => c.type === 'pipe_table_delimiter_cell');
|
|
584
|
+
for (let cellNo = 0; cellNo < delimiterCells.length; cellNo++) {
|
|
585
|
+
const cell = delimiterCells[cellNo];
|
|
586
|
+
if (cell.children.find((c) => c.type === 'pipe_table_align_right')) {
|
|
587
|
+
cellAlign.push('right');
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
cellAlign.push('left');
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
const headRows = node.children
|
|
595
|
+
?.filter((c) => c.type === 'pipe_table_header');
|
|
596
|
+
if (headRows.length > 0) {
|
|
597
|
+
const tokenName = 'thead';
|
|
598
|
+
const tagName = 'thead';
|
|
599
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
600
|
+
openToken.level = blockLevel;
|
|
601
|
+
retVal.push(openToken);
|
|
602
|
+
blockLevel++;
|
|
603
|
+
headRows.forEach((child) => walkRecursive(child, {
|
|
604
|
+
...ctx,
|
|
605
|
+
tableRowType: 'thead',
|
|
606
|
+
cellNo: 0,
|
|
607
|
+
cellAlign: cellAlign,
|
|
608
|
+
}));
|
|
609
|
+
blockLevel--;
|
|
610
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
611
|
+
closeToken.level = blockLevel;
|
|
612
|
+
retVal.push(closeToken);
|
|
613
|
+
}
|
|
614
|
+
const bodyRows = node.children
|
|
615
|
+
?.filter((c) => c.type === 'pipe_table_row');
|
|
616
|
+
if (bodyRows.length > 0) {
|
|
617
|
+
// const tokenName = 'tbody';
|
|
618
|
+
// const tagName = 'tbody';
|
|
619
|
+
// const openToken = new Token(
|
|
620
|
+
// tokenName + '_open',
|
|
621
|
+
// tagName,
|
|
622
|
+
// NESTING_OPENING,
|
|
623
|
+
// );
|
|
624
|
+
// openToken.level = blockLevel;
|
|
625
|
+
// retVal.push(openToken);
|
|
626
|
+
blockLevel++;
|
|
627
|
+
bodyRows.forEach((child) => walkRecursive(child, {
|
|
628
|
+
...ctx,
|
|
629
|
+
tableRowType: 'tbody',
|
|
630
|
+
cellAlign: cellAlign,
|
|
631
|
+
}));
|
|
632
|
+
blockLevel--;
|
|
633
|
+
// const closeToken = new Token(
|
|
634
|
+
// tokenName + '_close',
|
|
635
|
+
// tagName,
|
|
636
|
+
// NESTING_CLOSING,
|
|
637
|
+
// );
|
|
638
|
+
// closeToken.level = blockLevel;
|
|
639
|
+
// retVal.push(closeToken);
|
|
640
|
+
}
|
|
641
|
+
blockLevel--;
|
|
642
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
643
|
+
closeToken.level = blockLevel;
|
|
644
|
+
retVal.push(closeToken);
|
|
645
|
+
}
|
|
646
|
+
break;
|
|
647
|
+
case 'list':
|
|
648
|
+
{
|
|
649
|
+
let tokenName = 'bullet_list';
|
|
650
|
+
let tagName = 'ul';
|
|
651
|
+
let start = '';
|
|
652
|
+
const firstItem = node.children.find((item) => item.type === 'list_item');
|
|
653
|
+
if (firstItem) {
|
|
654
|
+
const taskListMarker = firstItem.children.find((item) => item.type.startsWith('task_list_marker'));
|
|
655
|
+
const listMarker = firstItem.children.find((item) => item.type.startsWith('list_marker_dot'));
|
|
656
|
+
if (taskListMarker) {
|
|
657
|
+
tokenName = 'task_list';
|
|
658
|
+
tagName = 'ul';
|
|
659
|
+
}
|
|
660
|
+
else if (listMarker) {
|
|
661
|
+
tokenName = 'ordered_list';
|
|
662
|
+
tagName = 'ol';
|
|
663
|
+
start = listMarker.text.trim().replace('.', '');
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
667
|
+
openToken.level = blockLevel;
|
|
668
|
+
openToken.markup = tagName === 'ol' ? '.' : '*';
|
|
669
|
+
openToken.attrSet('symbol', tagName === 'ol' ? '1' : '*');
|
|
670
|
+
if (start) {
|
|
671
|
+
openToken.attrSet('start', start);
|
|
672
|
+
}
|
|
673
|
+
retVal.push(openToken);
|
|
674
|
+
blockLevel++;
|
|
675
|
+
node.children?.forEach((child) => walkRecursive(child, ctx));
|
|
676
|
+
blockLevel--;
|
|
677
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
678
|
+
closeToken.level = blockLevel;
|
|
679
|
+
retVal.push(closeToken);
|
|
680
|
+
}
|
|
681
|
+
break;
|
|
682
|
+
case 'list_item':
|
|
683
|
+
{
|
|
684
|
+
const taskListMarker = node.children.find((item) => item.type.startsWith('task_list_marker_'));
|
|
685
|
+
const listMarker = node.children.find((item) => item.type.startsWith('list_marker_'));
|
|
686
|
+
if (!listMarker) {
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
let tokenName = taskListMarker ? 'task_item' : 'list_item';
|
|
690
|
+
const tagName = 'li';
|
|
691
|
+
const openToken = new Token(tokenName + '_open', tagName, NESTING_OPENING);
|
|
692
|
+
openToken.level = blockLevel;
|
|
693
|
+
openToken.markup = listMarker.text.trim();
|
|
694
|
+
if (listMarker.type === 'list_marker_dot') {
|
|
695
|
+
// openToken.info = listMarker.text.trim().replace('.', '');
|
|
696
|
+
// openToken.markup = '.';
|
|
697
|
+
openToken.info = '';
|
|
698
|
+
openToken.markup = '';
|
|
699
|
+
}
|
|
700
|
+
if (taskListMarker) {
|
|
701
|
+
if (taskListMarker.type === 'task_list_marker_checked') {
|
|
702
|
+
// throw new Error('aaaa')
|
|
703
|
+
}
|
|
704
|
+
openToken.attrSet('checked', taskListMarker.type === 'task_list_marker_checked'
|
|
705
|
+
? 'checked'
|
|
706
|
+
: '');
|
|
707
|
+
}
|
|
708
|
+
retVal.push(openToken);
|
|
709
|
+
// node.children?.forEach((child) => walkRecursive(child, ctx));
|
|
710
|
+
blockLevel++;
|
|
711
|
+
walkRecursive(node.children.find((item) => item.type === 'paragraph'), ctx);
|
|
712
|
+
const lists = node.children.filter((item) => item.type === 'list');
|
|
713
|
+
for (const list of lists) {
|
|
714
|
+
walkRecursive(list, ctx);
|
|
715
|
+
}
|
|
716
|
+
blockLevel--;
|
|
717
|
+
const closeToken = new Token(tokenName + '_close', tagName, NESTING_CLOSING);
|
|
718
|
+
closeToken.level = blockLevel;
|
|
719
|
+
retVal.push(closeToken);
|
|
720
|
+
}
|
|
721
|
+
break;
|
|
722
|
+
case 'html_block':
|
|
723
|
+
{
|
|
724
|
+
const token = new Token('html_block', '', NESTING_SELF_CLOSING);
|
|
725
|
+
token.level = blockLevel;
|
|
726
|
+
token.content = node.text ?? '';
|
|
727
|
+
retVal.push(token);
|
|
728
|
+
}
|
|
729
|
+
break;
|
|
730
|
+
case 'text':
|
|
731
|
+
{
|
|
732
|
+
const token = new Token('text', '', NESTING_SELF_CLOSING);
|
|
733
|
+
token.level = blockLevel;
|
|
734
|
+
token.content = node.text ?? '';
|
|
735
|
+
retVal.push(token);
|
|
736
|
+
}
|
|
737
|
+
break;
|
|
738
|
+
default:
|
|
739
|
+
// Log unhandled node types for debugging
|
|
740
|
+
console.warn(`Unhandled node type: ${node.type}, children: ${node.children.map((c) => c.type).join(', ')}`, {
|
|
741
|
+
...node,
|
|
742
|
+
children: undefined,
|
|
743
|
+
_children: undefined,
|
|
744
|
+
tree: undefined,
|
|
745
|
+
});
|
|
746
|
+
throw new Error('aaa');
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
walkRecursive(tree.rootNode);
|
|
750
|
+
return retVal;
|
|
751
|
+
}
|
|
752
|
+
export async function sitterTokenizer(cdnUrl = '') {
|
|
753
|
+
const jsonManifest = getLangTreeSitter('markdown', cdnUrl);
|
|
754
|
+
const blockUrl = jsonManifest.files.find((url) => url.indexOf('_inline') === -1);
|
|
755
|
+
const inlineUrl = jsonManifest.files.find((url) => url.indexOf('_inline') > -1);
|
|
756
|
+
const markdownWasm = await fetchWasm(blockUrl);
|
|
757
|
+
const inlineWasm = await fetchWasm(inlineUrl);
|
|
758
|
+
const blockParser = (await createParser(markdownWasm));
|
|
759
|
+
const inlineParser = (await createParser(inlineWasm));
|
|
760
|
+
return {
|
|
761
|
+
parse: (source) => {
|
|
762
|
+
const tree = blockParser.parse(source);
|
|
763
|
+
if (!tree) {
|
|
764
|
+
throw new Error('Tree is null');
|
|
765
|
+
}
|
|
766
|
+
return treeToTokens(tree, inlineParser);
|
|
767
|
+
},
|
|
768
|
+
};
|
|
769
|
+
}
|