@octane-xplat/build 0.0.0-stage → 0.20.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/add-worker.js +66 -0
- package/dist/build-worker.js +276 -0
- package/dist/compatibility-check.js +153 -0
- package/dist/compatibility-data/compatibility.js +106 -0
- package/dist/compatibility-data/compatibility.json +305 -0
- package/dist/compatibility.js +229 -0
- package/dist/css-layers.js +63 -0
- package/dist/css-platform.js +230 -0
- package/dist/css-platform.types.d.ts +11 -0
- package/dist/dev-inspector.web.js +125 -0
- package/dist/dev-log-client.web.js +79 -0
- package/dist/dev-log-server.js +13 -0
- package/dist/doctor-checks.js +317 -0
- package/dist/doctor-tool.js +27 -0
- package/dist/env.js +3 -0
- package/dist/fmt-plan.js +149 -0
- package/dist/fonts-worker.js +31 -0
- package/dist/fonts.js +614 -0
- package/dist/inline-stylesheets.js +37 -0
- package/dist/linux/config.js +43 -0
- package/dist/linux/host/bridge-selftest.linux.js +255 -0
- package/dist/linux/host/gjs-host.js +1412 -0
- package/dist/linux/package.js +176 -0
- package/dist/linux-gtk/vite.d.ts +23 -0
- package/dist/linux-gtk/vite.js +194 -0
- package/dist/macos/config.js +141 -0
- package/dist/macos/doctor-checks.js +58 -0
- package/dist/macos/executables.js +39 -0
- package/dist/macos/jsc-host/runtime.js +134 -0
- package/dist/macos/native.js +287 -0
- package/dist/macos/runtime-package.js +30 -0
- package/dist/macos/tab-item.js +62 -0
- package/dist/macos/vite.d.ts +21 -0
- package/dist/macos/vite.js +111 -0
- package/dist/markdown.js +11 -0
- package/dist/mason-engine.js +20 -0
- package/dist/mcp/contracts/actions.js +133 -0
- package/dist/mcp/dev-bridge.js +363 -0
- package/dist/mcp/dev-socket.js +161 -0
- package/dist/mcp/dev-stack.js +134 -0
- package/dist/mcp/inspection/dom.web.js +46 -0
- package/dist/native-regex.js +123 -0
- package/dist/package-manager.js +106 -0
- package/dist/package-root.js +48 -0
- package/dist/patches.js +356 -0
- package/dist/route-config.js +53 -0
- package/dist/route-data.js +42 -0
- package/dist/route-generator.js +514 -0
- package/dist/route-path.js +29 -0
- package/dist/route-source.js +67 -0
- package/dist/targets.js +38 -0
- package/dist/tsconfig-envs.js +287 -0
- package/dist/tsrx-leaf.js +57 -0
- package/dist/typecheck-globals/macos.d.ts +21 -0
- package/dist/typecheck-globals/native.d.ts +26 -0
- package/dist/ui/src/markdown/markdown-ast.js +33 -0
- package/dist/ui/src/markdown/markdown-character-references.js +79 -0
- package/dist/ui/src/markdown/markdown-parse.js +1930 -0
- package/dist/ui/src/markdown/markdown-plugins.js +564 -0
- package/dist/ui/src/markdown/markdown-url.js +17 -0
- package/dist/updates-worker.js +34 -0
- package/dist/upgrade-worker.js +63 -0
- package/dist/vite.d.ts +85 -0
- package/dist/vite.js +628 -0
- package/dist/workshop-worker.js +181 -0
- package/package.json +72 -5
|
@@ -0,0 +1,1930 @@
|
|
|
1
|
+
import { decodeLiteralText, isAsciiPunctuation, matchCharacterReference } from "./markdown-character-references.js";
|
|
2
|
+
import { mdInlineText } from "./markdown-ast.js";
|
|
3
|
+
import { isSafeMdParserUrl } from "./markdown-url.js";
|
|
4
|
+
import { applyMarkdownTransforms, freezeMarkdownPluginData, isMarkdownPluginData, prepareMarkdownPlugins, reportMarkdownPluginFailure } from "./markdown-plugins.js";
|
|
5
|
+
// ../ui/src/markdown/markdown-parse.ts
|
|
6
|
+
/**
|
|
7
|
+
* The deepest lists and blockquotes may nest, as emphasis is capped
|
|
8
|
+
* (MAX_EMPHASIS_DEPTH): at most 100 levels. Content nested deeper is one
|
|
9
|
+
* paragraph of text.
|
|
10
|
+
*/
|
|
11
|
+
var MAX_BLOCK_NESTING = 100;
|
|
12
|
+
/** The key a nested parse of `input` at `depth` is remembered under. */
|
|
13
|
+
function nestedParseKey(depth, input) {
|
|
14
|
+
return `${depth}\u0000${input}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Every resolved options object is built here, so all of them share one key
|
|
18
|
+
* set in one order — and therefore one hidden class. The parser reads these
|
|
19
|
+
* fields in its hottest loops; letting an omitted-options parse and an
|
|
20
|
+
* options-bag parse produce differently-shaped objects makes those reads
|
|
21
|
+
* polymorphic in any process that does both, which is exactly what a caller
|
|
22
|
+
* comparing an empty pipeline against a configured one does.
|
|
23
|
+
*/
|
|
24
|
+
function makeResolvedOptions(fields) {
|
|
25
|
+
return {
|
|
26
|
+
sourceIds: fields.sourceIds,
|
|
27
|
+
autolink: fields.autolink,
|
|
28
|
+
math: fields.math,
|
|
29
|
+
pluginSyntaxIdentity: fields.pluginSyntaxIdentity,
|
|
30
|
+
astPositions: fields.astPositions ?? true,
|
|
31
|
+
plugins: fields.plugins,
|
|
32
|
+
isFinal: fields.isFinal,
|
|
33
|
+
allowBlockSyntax: fields.allowBlockSyntax ?? true,
|
|
34
|
+
baseOffset: fields.baseOffset,
|
|
35
|
+
linkDefs: fields.linkDefs,
|
|
36
|
+
blockDepth: fields.blockDepth ?? 0,
|
|
37
|
+
nestedParses: fields.nestedParses,
|
|
38
|
+
probing: fields.probing,
|
|
39
|
+
sourceLines: fields.sourceLines,
|
|
40
|
+
baseLine: fields.baseLine,
|
|
41
|
+
lineOrigin: fields.lineOrigin
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
var EMPTY_OPTS = makeResolvedOptions({ isFinal: true });
|
|
45
|
+
var EMPTY_INCREMENTAL_OPTS = makeResolvedOptions({ isFinal: false });
|
|
46
|
+
function resolveOptions(arg, incremental = false) {
|
|
47
|
+
if (arg == null) return incremental ? EMPTY_INCREMENTAL_OPTS : EMPTY_OPTS;
|
|
48
|
+
const plugins = arg.plugins != null && arg.plugins.length > 0 ? prepareMarkdownPlugins(arg.plugins) : void 0;
|
|
49
|
+
return makeResolvedOptions({
|
|
50
|
+
sourceIds: arg.sourceIds,
|
|
51
|
+
autolink: arg.autolink,
|
|
52
|
+
math: arg.math === true ? true : void 0,
|
|
53
|
+
pluginSyntaxIdentity: plugins?.syntaxIdentity ?? arg.pluginSyntaxIdentity,
|
|
54
|
+
plugins,
|
|
55
|
+
isFinal: incremental ? arg.isFinal === true : true,
|
|
56
|
+
sourceLines: plugins?.sourceLines === true ? true : void 0
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
var LINK_DEFINITION_RE = /^ {0,3}\[([^\]^](?:\\.|[^\]\\])*)\]:[ \t]*(?:<([^<>\n]*)>|(\S+))([ \t]+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\)))?[ \t]*$/;
|
|
60
|
+
/** The most characters a link label may hold (CommonMark 0.31 §4.7). */
|
|
61
|
+
var MAX_LINK_LABEL_LENGTH = 999;
|
|
62
|
+
var LINK_TITLE_ONLY_RE = /^ {0,3}(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\))[ \t]*$/;
|
|
63
|
+
function normalizeLinkLabel(label) {
|
|
64
|
+
return label.trim().replace(/\s+/g, " ").toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
/** Match a complete `$$…$$` display-math block without consuming partial input. */
|
|
67
|
+
function matchDisplayMathBlock(lines, lineIndex) {
|
|
68
|
+
const trimmed = lines[lineIndex].trim();
|
|
69
|
+
if (trimmed.length > 4 && trimmed.startsWith("$$") && trimmed.endsWith("$$")) {
|
|
70
|
+
const value = trimmed.slice(2, -2);
|
|
71
|
+
return value.trim() === "" ? null : {
|
|
72
|
+
value,
|
|
73
|
+
nextIndex: lineIndex + 1,
|
|
74
|
+
endLine: lineIndex
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (trimmed !== "$$") return null;
|
|
78
|
+
for (let index = lineIndex + 1; index < lines.length; index++) if (lines[index].trim() === "$$") {
|
|
79
|
+
const value = lines.slice(lineIndex + 1, index).join("\n");
|
|
80
|
+
return value.trim() === "" ? null : {
|
|
81
|
+
value,
|
|
82
|
+
nextIndex: index + 1,
|
|
83
|
+
endLine: index
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
function matchLinkDefinition(line) {
|
|
89
|
+
const match = LINK_DEFINITION_RE.exec(line);
|
|
90
|
+
if (match == null || match[1].length > MAX_LINK_LABEL_LENGTH) return null;
|
|
91
|
+
const label = normalizeLinkLabel(match[1]);
|
|
92
|
+
const destination = match[2] != null ? match[2] : match[3];
|
|
93
|
+
if (label === "" || destination == null) return null;
|
|
94
|
+
return {
|
|
95
|
+
label,
|
|
96
|
+
destination: decodeLinkDestination(destination),
|
|
97
|
+
hasTitle: match[4] != null
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A code fence line: up to three spaces of indentation, then three or more
|
|
102
|
+
* backticks or tildes (CommonMark 0.31 §4.5). Group 1 is the indentation and
|
|
103
|
+
* group 2 the fence.
|
|
104
|
+
*/
|
|
105
|
+
var FENCE_LINE = /^( {0,3})(`{3,}|~{3,})/;
|
|
106
|
+
/**
|
|
107
|
+
* Whether `line` closes a code block opened with `fence`: a fence of the same
|
|
108
|
+
* character, at least as long, after up to three spaces of indentation, with
|
|
109
|
+
* only spaces or tabs after it (CommonMark 0.31 §4.5) — so ` ```js` inside
|
|
110
|
+
* an open block is code, not its end.
|
|
111
|
+
*/
|
|
112
|
+
function closesFence(line, fence) {
|
|
113
|
+
const match = /^ {0,3}(`{3,}|~{3,})[ \t]*\r?$/.exec(line);
|
|
114
|
+
return match != null && match[1].startsWith(fence[0]) && match[1].length >= fence.length;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The list item `line` opens, read as the block parser reads one: a bullet or
|
|
118
|
+
* a number with `.` or `)` after up to nine spaces, then a space. A thematic
|
|
119
|
+
* break opens no item.
|
|
120
|
+
*/
|
|
121
|
+
function listItemScopeOf(line) {
|
|
122
|
+
if (isHorizontalRule(line)) return null;
|
|
123
|
+
const marker = /^( {0,9})([-*+]|\d+[.)]) /.exec(line);
|
|
124
|
+
if (marker == null) return null;
|
|
125
|
+
const markerEnd = marker[0].length;
|
|
126
|
+
const spacesAfter = getIndent(line.slice(markerEnd));
|
|
127
|
+
return {
|
|
128
|
+
base: marker[1].length,
|
|
129
|
+
content: spacesAfter >= 4 ? markerEnd : markerEnd + spacesAfter,
|
|
130
|
+
ordered: marker[2] !== "-" && marker[2] !== "*" && marker[2] !== "+"
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Follows the code fences that open at the top level of a document, a line at
|
|
135
|
+
* a time, as the block parser reads them. A fence indented into an open list
|
|
136
|
+
* item belongs to that item, whose own parse reads it: it neither opens nor
|
|
137
|
+
* closes a top-level fence, so a list step's fence closed at the margin leaves
|
|
138
|
+
* the margin line to open a fence of its own, as the full parse does. Scanners
|
|
139
|
+
* that run outside the block parser — streaming settlement, link definitions,
|
|
140
|
+
* display-math trimming — share it so they pair fences the same way.
|
|
141
|
+
*/
|
|
142
|
+
function topLevelFences() {
|
|
143
|
+
let fence = "";
|
|
144
|
+
let item = null;
|
|
145
|
+
let itemHadBlank = false;
|
|
146
|
+
let itemFence = "";
|
|
147
|
+
let itemEndsInParagraph = false;
|
|
148
|
+
let paragraphOpen = false;
|
|
149
|
+
const isHeadingOrBreak = (text) => /^ {0,3}#{1,6}(?:[ \t]|$)/.test(text) || isHorizontalRule(text);
|
|
150
|
+
/** Reads a line of the open item, without the item's indentation. */
|
|
151
|
+
const readItemLine = (text) => {
|
|
152
|
+
if (itemFence !== "") {
|
|
153
|
+
if (closesFence(text, itemFence)) itemFence = "";
|
|
154
|
+
itemEndsInParagraph = false;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const opening = FENCE_LINE.exec(text);
|
|
158
|
+
if (opening != null) {
|
|
159
|
+
itemFence = opening[2];
|
|
160
|
+
itemEndsInParagraph = false;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
itemEndsInParagraph = text.trim() !== "" && !isHeadingOrBreak(text);
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
get open() {
|
|
167
|
+
return fence !== "";
|
|
168
|
+
},
|
|
169
|
+
read(lines, index) {
|
|
170
|
+
const line = lines[index];
|
|
171
|
+
if (fence !== "") {
|
|
172
|
+
if (closesFence(line, fence)) {
|
|
173
|
+
fence = "";
|
|
174
|
+
return "close";
|
|
175
|
+
}
|
|
176
|
+
return "inside";
|
|
177
|
+
}
|
|
178
|
+
if (line.trim() === "") {
|
|
179
|
+
itemHadBlank = item != null;
|
|
180
|
+
paragraphOpen = false;
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const indent = getIndent(line);
|
|
184
|
+
if (item != null && (itemHadBlank ? indent >= item.content : indent > item.base)) {
|
|
185
|
+
readItemLine(line.slice(Math.min(indent, item.content)));
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const scope = listItemScopeOf(line);
|
|
189
|
+
if (scope != null && !(paragraphOpen && item == null && scope.ordered && !/^ {0,9}0*1[.)]/.test(line))) {
|
|
190
|
+
item = scope;
|
|
191
|
+
itemHadBlank = false;
|
|
192
|
+
itemFence = "";
|
|
193
|
+
paragraphOpen = false;
|
|
194
|
+
readItemLine(line.slice(scope.content));
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
const opening = FENCE_LINE.exec(line);
|
|
198
|
+
if (opening != null) {
|
|
199
|
+
item = null;
|
|
200
|
+
paragraphOpen = false;
|
|
201
|
+
fence = opening[2];
|
|
202
|
+
return "open";
|
|
203
|
+
}
|
|
204
|
+
if (item != null && !itemHadBlank && itemEndsInParagraph && canContinueParagraphLazily(lines, index)) return null;
|
|
205
|
+
item = null;
|
|
206
|
+
paragraphOpen = !isHeadingOrBreak(line);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Collect link reference definitions from the whole document and return the
|
|
213
|
+
* input with the definition lines removed. A definition is recognized at a
|
|
214
|
+
* block boundary — document start, after a blank line, after another
|
|
215
|
+
* definition, or after a self-contained block (heading / thematic break /
|
|
216
|
+
* closed fenced code) — but never inside a fenced code block or as a lazy
|
|
217
|
+
* continuation of a paragraph, honoring CommonMark's rule that a definition
|
|
218
|
+
* cannot interrupt a paragraph. First definition wins, and definitions produce
|
|
219
|
+
* no output so stripping unreferenced ones is correct.
|
|
220
|
+
*
|
|
221
|
+
* Scope limit: definitions are collected at the top level only, and a
|
|
222
|
+
* definition directly following a list, blockquote, or table (with no blank
|
|
223
|
+
* line between) is not recognized. A definition nested inside a blockquote or
|
|
224
|
+
* list item resolves within that container (via the recursive parse) but is
|
|
225
|
+
* not exposed to references elsewhere in the document, unlike full CommonMark
|
|
226
|
+
* where every definition is global. Separating a footer definition block with
|
|
227
|
+
* a blank line — the usual form — always works.
|
|
228
|
+
*/
|
|
229
|
+
function extractLinkDefinitions(input, math = false) {
|
|
230
|
+
const lines = input.split("\n");
|
|
231
|
+
const defs = /* @__PURE__ */ new Map();
|
|
232
|
+
const keep = new Array(lines.length).fill(true);
|
|
233
|
+
let atBoundary = true;
|
|
234
|
+
const fences = topLevelFences();
|
|
235
|
+
for (let index = 0; index < lines.length; index++) {
|
|
236
|
+
const line = lines[index];
|
|
237
|
+
if (fences.open) {
|
|
238
|
+
atBoundary = fences.read(lines, index) === "close";
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (math) {
|
|
242
|
+
const displayMath = matchDisplayMathBlock(lines, index);
|
|
243
|
+
if (displayMath != null) {
|
|
244
|
+
index = displayMath.endLine;
|
|
245
|
+
atBoundary = true;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (fences.read(lines, index) === "open") {
|
|
250
|
+
atBoundary = false;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (line.trim() === "") {
|
|
254
|
+
atBoundary = true;
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (atBoundary) {
|
|
258
|
+
const def = matchLinkDefinition(line);
|
|
259
|
+
if (def != null) {
|
|
260
|
+
if (!defs.has(def.label)) defs.set(def.label, def.destination);
|
|
261
|
+
keep[index] = false;
|
|
262
|
+
if (!def.hasTitle && index + 1 < lines.length && LINK_TITLE_ONLY_RE.test(lines[index + 1])) {
|
|
263
|
+
keep[index + 1] = false;
|
|
264
|
+
index++;
|
|
265
|
+
}
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
atBoundary = /^ {0,3}#{1,6}(?: |\t|$)/.test(line) || isHorizontalRule(line);
|
|
270
|
+
}
|
|
271
|
+
if (defs.size === 0) return {
|
|
272
|
+
defs,
|
|
273
|
+
cleaned: input
|
|
274
|
+
};
|
|
275
|
+
const lineMap = [];
|
|
276
|
+
for (let index = 0; index < lines.length; index++) if (keep[index]) lineMap.push(index);
|
|
277
|
+
return {
|
|
278
|
+
defs,
|
|
279
|
+
cleaned: lineMap.map((index) => lines[index]).join("\n"),
|
|
280
|
+
lineMap
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Resolve a full (`[text][label]`), collapsed (`[text][]`), or shortcut
|
|
285
|
+
* (`[text]`) reference at `start` (which points at `[`) against `linkDefs`.
|
|
286
|
+
* Returns the node plus the index just past the reference, or null when it is
|
|
287
|
+
* not a resolvable reference (caller falls through to literal handling).
|
|
288
|
+
*/
|
|
289
|
+
function matchReferenceLink(text, start, linkDefs, opts, context, inlineIndex) {
|
|
290
|
+
const textClose = inlineIndex.openerClose(start);
|
|
291
|
+
if (textClose === -1) return null;
|
|
292
|
+
const linkText = text.slice(start + 1, textClose);
|
|
293
|
+
if (text[textClose + 1] === "[") {
|
|
294
|
+
const labelClose = inlineIndex.closingBracket(textClose + 2);
|
|
295
|
+
if (labelClose !== -1) {
|
|
296
|
+
const rawLabel = text.slice(textClose + 2, labelClose);
|
|
297
|
+
const label = rawLabel === "" ? linkText : rawLabel;
|
|
298
|
+
const href = linkDefs.get(normalizeLinkLabel(label));
|
|
299
|
+
if (href != null && isSafeMdParserUrl(href)) return {
|
|
300
|
+
node: {
|
|
301
|
+
type: "link",
|
|
302
|
+
url: href,
|
|
303
|
+
children: parseInlineImpl(linkText, opts, context)
|
|
304
|
+
},
|
|
305
|
+
end: labelClose + 1
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
if (linkText.trim() === "") return null;
|
|
310
|
+
const href = linkDefs.get(normalizeLinkLabel(linkText));
|
|
311
|
+
if (href == null || !isSafeMdParserUrl(href)) return null;
|
|
312
|
+
return {
|
|
313
|
+
node: {
|
|
314
|
+
type: "link",
|
|
315
|
+
url: href,
|
|
316
|
+
children: parseInlineImpl(linkText, opts, context)
|
|
317
|
+
},
|
|
318
|
+
end: textClose + 1
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/** Reference-image equivalent of {@link matchReferenceLink} (`![alt][label]`). */
|
|
322
|
+
/** How many image descriptions enclose the one being read. */
|
|
323
|
+
var imageAltDepth = 0;
|
|
324
|
+
/**
|
|
325
|
+
* The deepest image descriptions are read as inline content. Reading one
|
|
326
|
+
* reads every image nested in it again, so the cost grows with the depth
|
|
327
|
+
* times the text; ten levels keep any input fast, and no real image nests
|
|
328
|
+
* that deep.
|
|
329
|
+
*/
|
|
330
|
+
var MAX_IMAGE_ALT_DEPTH = 10;
|
|
331
|
+
/**
|
|
332
|
+
* An image's alt text: its description read as inline content and taken as
|
|
333
|
+
* plain text (CommonMark 0.31 §6.4), so `` ![`a]b` *c*](u) `` has the alt
|
|
334
|
+
* `a]b c`. Descriptions nested deeper than MAX_IMAGE_ALT_DEPTH keep their
|
|
335
|
+
* decoded source.
|
|
336
|
+
*/
|
|
337
|
+
function imageAlt(description, opts, context) {
|
|
338
|
+
if (imageAltDepth >= MAX_IMAGE_ALT_DEPTH) return decodeLiteralText(description);
|
|
339
|
+
imageAltDepth++;
|
|
340
|
+
try {
|
|
341
|
+
return mdInlineText(breaksAsLineEndings(parseInlineImpl(description, opts, context)));
|
|
342
|
+
} finally {
|
|
343
|
+
imageAltDepth--;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* `nodes` with each hard line break as the line ending it stands for, so an
|
|
348
|
+
* alt text keeps the words on either side apart, as commonmark.js writes it.
|
|
349
|
+
*/
|
|
350
|
+
function breaksAsLineEndings(nodes) {
|
|
351
|
+
return nodes.map((node) => {
|
|
352
|
+
switch (node.type) {
|
|
353
|
+
case "break": return {
|
|
354
|
+
type: "text",
|
|
355
|
+
value: "\n"
|
|
356
|
+
};
|
|
357
|
+
case "strong":
|
|
358
|
+
case "emphasis":
|
|
359
|
+
case "delete":
|
|
360
|
+
case "link": return {
|
|
361
|
+
...node,
|
|
362
|
+
children: breaksAsLineEndings(node.children)
|
|
363
|
+
};
|
|
364
|
+
case "text":
|
|
365
|
+
case "inlineCode":
|
|
366
|
+
case "inlineMath":
|
|
367
|
+
case "image":
|
|
368
|
+
case "citation":
|
|
369
|
+
case "extension": return node;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
function matchReferenceImage(text, start, linkDefs, inlineIndex, opts, context) {
|
|
374
|
+
const altClose = inlineIndex.openerClose(start + 1);
|
|
375
|
+
if (altClose === -1) return null;
|
|
376
|
+
const alt = text.slice(start + 2, altClose);
|
|
377
|
+
if (text[altClose + 1] === "[") {
|
|
378
|
+
const labelClose = inlineIndex.closingBracket(altClose + 2);
|
|
379
|
+
if (labelClose !== -1) {
|
|
380
|
+
const rawLabel = text.slice(altClose + 2, labelClose);
|
|
381
|
+
const label = rawLabel === "" ? alt : rawLabel;
|
|
382
|
+
const src = linkDefs.get(normalizeLinkLabel(label));
|
|
383
|
+
if (src != null && isSafeMdParserUrl(src)) return {
|
|
384
|
+
node: {
|
|
385
|
+
type: "image",
|
|
386
|
+
url: src,
|
|
387
|
+
alt: imageAlt(alt, opts, context)
|
|
388
|
+
},
|
|
389
|
+
end: labelClose + 1
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
if (alt.trim() === "") return null;
|
|
394
|
+
const src = linkDefs.get(normalizeLinkLabel(alt));
|
|
395
|
+
if (src == null || !isSafeMdParserUrl(src)) return null;
|
|
396
|
+
return {
|
|
397
|
+
node: {
|
|
398
|
+
type: "image",
|
|
399
|
+
url: src,
|
|
400
|
+
alt: imageAlt(alt, opts, context)
|
|
401
|
+
},
|
|
402
|
+
end: altClose + 1
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
/** Find closing ')' that balances nested parentheses. */
|
|
406
|
+
var INLINE_DESTINATION_WITH_TITLE = /^\s*(?:<((?:[^<>\n\\]|\\.)*)>|([^\s<]\S*?))(?:\s+(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\((?:[^()\\]|\\.)*\)))?\s*$/s;
|
|
407
|
+
/**
|
|
408
|
+
* Where an inline link's or image's destination ends when it opens with `<`
|
|
409
|
+
* — the index of the `)` that closes the link — `'refused'` when it opens
|
|
410
|
+
* with `<` but is no angle-bracket destination, or null when the content
|
|
411
|
+
* after `open` (the `(`) does not open with `<`. Inside the brackets
|
|
412
|
+
* parentheses are plain characters, so `<b(c>` is the destination `b(c`. A
|
|
413
|
+
* destination that opens with `<` must be one whole angle-bracket
|
|
414
|
+
* destination — no line ending, even escaped, and no unescaped `<` inside;
|
|
415
|
+
* then only spaces, an optional title, and the `)` — or the link is text:
|
|
416
|
+
* `[a](<b>c>)` and `[a](<b)` are no links (CommonMark 0.31 §6.3).
|
|
417
|
+
*/
|
|
418
|
+
function angleDestinationClose(text, open) {
|
|
419
|
+
let index = open + 1;
|
|
420
|
+
while (text[index] === " " || text[index] === " ") index++;
|
|
421
|
+
if (text[index] !== "<") return null;
|
|
422
|
+
for (index++; index < text.length; index++) {
|
|
423
|
+
const character = text[index];
|
|
424
|
+
if (character === "\\" && text[index + 1] !== "\n" && text[index + 1] !== "\r") index++;
|
|
425
|
+
else if (character === "\n" || character === "\r" || character === "<") return "refused";
|
|
426
|
+
else if (character === ">") break;
|
|
427
|
+
}
|
|
428
|
+
if (index >= text.length) return "refused";
|
|
429
|
+
index++;
|
|
430
|
+
while (/\s/.test(text[index] ?? "")) index++;
|
|
431
|
+
const quote = text[index];
|
|
432
|
+
if (quote === "\"" || quote === "'" || quote === "(") {
|
|
433
|
+
const closer = quote === "(" ? ")" : quote;
|
|
434
|
+
for (index++; index < text.length && text[index] !== closer; index++) if (text[index] === "\\") index++;
|
|
435
|
+
else if (quote === "(" && text[index] === "(") return "refused";
|
|
436
|
+
index++;
|
|
437
|
+
while (/\s/.test(text[index] ?? "")) index++;
|
|
438
|
+
}
|
|
439
|
+
return text[index] === ")" ? index : "refused";
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* The destination of an inline link or image, without its title. Content in
|
|
443
|
+
* any other shape keeps its released meaning: all of it is the destination.
|
|
444
|
+
*/
|
|
445
|
+
function inlineDestination(content) {
|
|
446
|
+
const match = INLINE_DESTINATION_WITH_TITLE.exec(content);
|
|
447
|
+
return match == null ? content : match[1] ?? match[2] ?? content;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* A link or image destination with its backslash escapes and character
|
|
451
|
+
* references decoded, as CommonMark 0.31 §6.3 reads it: `\)` is `)`,
|
|
452
|
+
* `&` is `&`, and an escaped `\&` stays literal. Every URL safety check
|
|
453
|
+
* runs on this decoded value, so an encoded scheme such as `javascript:`
|
|
454
|
+
* is refused like the plain one.
|
|
455
|
+
*/
|
|
456
|
+
function decodeLinkDestination(raw) {
|
|
457
|
+
if (!raw.includes("\\") && !raw.includes("&")) return raw;
|
|
458
|
+
let decoded = "";
|
|
459
|
+
let index = 0;
|
|
460
|
+
while (index < raw.length) {
|
|
461
|
+
const character = raw[index];
|
|
462
|
+
if (character === "\\" && isAsciiPunctuation(raw[index + 1])) {
|
|
463
|
+
decoded += raw[index + 1];
|
|
464
|
+
index += 2;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (character === "&") {
|
|
468
|
+
const reference = matchCharacterReference(raw, index);
|
|
469
|
+
if (reference != null) {
|
|
470
|
+
decoded += reference.value;
|
|
471
|
+
index = reference.end;
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
decoded += character;
|
|
476
|
+
index++;
|
|
477
|
+
}
|
|
478
|
+
return decoded;
|
|
479
|
+
}
|
|
480
|
+
function inlineIndexOf(text, linkDefs) {
|
|
481
|
+
let backtickStarts = null;
|
|
482
|
+
const backtickCloser = (from, length) => {
|
|
483
|
+
if (backtickStarts == null) {
|
|
484
|
+
backtickStarts = /* @__PURE__ */ new Map();
|
|
485
|
+
for (let start = text.indexOf("`"); start !== -1;) {
|
|
486
|
+
let end = start;
|
|
487
|
+
while (text[end] === "`") end++;
|
|
488
|
+
const sameLength = backtickStarts.get(end - start);
|
|
489
|
+
if (sameLength == null) backtickStarts.set(end - start, [start]);
|
|
490
|
+
else sameLength.push(start);
|
|
491
|
+
start = text.indexOf("`", end);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
const sameLength = backtickStarts.get(length);
|
|
495
|
+
if (sameLength == null) return -1;
|
|
496
|
+
let low = 0;
|
|
497
|
+
let high = sameLength.length;
|
|
498
|
+
while (low < high) {
|
|
499
|
+
const middle = low + high >> 1;
|
|
500
|
+
if (sameLength[middle] < from) low = middle + 1;
|
|
501
|
+
else high = middle;
|
|
502
|
+
}
|
|
503
|
+
return low < sameLength.length ? sameLength[low] : -1;
|
|
504
|
+
};
|
|
505
|
+
let nextClosingBracket = null;
|
|
506
|
+
const closingBracket = (from) => {
|
|
507
|
+
if (nextClosingBracket == null) {
|
|
508
|
+
const closes = new Uint8Array(text.length);
|
|
509
|
+
for (let index = 0; index < text.length; index++) {
|
|
510
|
+
const character = text[index];
|
|
511
|
+
if (character === "\\") index++;
|
|
512
|
+
else if (character === "`") {
|
|
513
|
+
let runEnd = index;
|
|
514
|
+
while (text[runEnd] === "`") runEnd++;
|
|
515
|
+
const closer = backtickCloser(runEnd, runEnd - index);
|
|
516
|
+
index = (closer === -1 ? runEnd : closer + (runEnd - index)) - 1;
|
|
517
|
+
} else if (character === "]") closes[index] = 1;
|
|
518
|
+
}
|
|
519
|
+
nextClosingBracket = new Int32Array(text.length + 1).fill(-1);
|
|
520
|
+
for (let index = text.length - 1; index >= 0; index--) nextClosingBracket[index] = closes[index] === 1 ? index : nextClosingBracket[index + 1];
|
|
521
|
+
}
|
|
522
|
+
return from < text.length ? nextClosingBracket[from] : -1;
|
|
523
|
+
};
|
|
524
|
+
let parenPartners = null;
|
|
525
|
+
const closingParen = (open) => {
|
|
526
|
+
if (parenPartners == null) {
|
|
527
|
+
parenPartners = new Int32Array(text.length).fill(-1);
|
|
528
|
+
const opens = [];
|
|
529
|
+
for (let index = 0; index < text.length; index++) {
|
|
530
|
+
const character = text[index];
|
|
531
|
+
if (character === "\\") index++;
|
|
532
|
+
else if (character === "(") opens.push(index);
|
|
533
|
+
else if (character === ")") {
|
|
534
|
+
const opened = opens.pop();
|
|
535
|
+
if (opened !== void 0) parenPartners[opened] = index;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
return text[open] === "(" ? parenPartners[open] : -1;
|
|
540
|
+
};
|
|
541
|
+
/**
|
|
542
|
+
* Where the link or image whose text closes at `close` ends — past its
|
|
543
|
+
* destination or label — or -1 when nothing after the `]` makes one. The
|
|
544
|
+
* same checks the inline scan makes when it builds the node.
|
|
545
|
+
*/
|
|
546
|
+
const linkEnd = (open, close) => {
|
|
547
|
+
if (text[close + 1] === "(") {
|
|
548
|
+
const angleClose = angleDestinationClose(text, close + 1);
|
|
549
|
+
const urlClose = angleClose === "refused" ? -1 : angleClose ?? closingParen(close + 1);
|
|
550
|
+
if (urlClose !== -1) return urlClose + 1;
|
|
551
|
+
}
|
|
552
|
+
if (linkDefs == null || close - open - 1 > MAX_LINK_LABEL_LENGTH) return -1;
|
|
553
|
+
const linkText = text.slice(open + 1, close);
|
|
554
|
+
const defines = (label) => {
|
|
555
|
+
const href = linkDefs.get(normalizeLinkLabel(label));
|
|
556
|
+
return href != null && isSafeMdParserUrl(href);
|
|
557
|
+
};
|
|
558
|
+
if (text[close + 1] === "[") {
|
|
559
|
+
const labelClose = closingBracket(close + 2);
|
|
560
|
+
if (labelClose !== -1 && labelClose - close - 2 <= MAX_LINK_LABEL_LENGTH && defines(labelClose === close + 2 ? linkText : text.slice(close + 2, labelClose))) return labelClose + 1;
|
|
561
|
+
}
|
|
562
|
+
return linkText.trim() !== "" && defines(linkText) ? close + 1 : -1;
|
|
563
|
+
};
|
|
564
|
+
let openerCloses = null;
|
|
565
|
+
const openerClose = (open) => {
|
|
566
|
+
if (openerCloses == null) {
|
|
567
|
+
openerCloses = /* @__PURE__ */ new Map();
|
|
568
|
+
const openers = [];
|
|
569
|
+
let inactiveBelow = 0;
|
|
570
|
+
for (let index = 0; index < text.length; index++) {
|
|
571
|
+
const character = text[index];
|
|
572
|
+
if (character === "\\") index++;
|
|
573
|
+
else if (character === "`") {
|
|
574
|
+
let runEnd = index;
|
|
575
|
+
while (text[runEnd] === "`") runEnd++;
|
|
576
|
+
const closer = backtickCloser(runEnd, runEnd - index);
|
|
577
|
+
index = (closer === -1 ? runEnd : closer + (runEnd - index)) - 1;
|
|
578
|
+
} else if (character === "!" && text[index + 1] === "[") {
|
|
579
|
+
openers.push({
|
|
580
|
+
index: index + 1,
|
|
581
|
+
image: true
|
|
582
|
+
});
|
|
583
|
+
index++;
|
|
584
|
+
} else if (character === "[") openers.push({
|
|
585
|
+
index,
|
|
586
|
+
image: false
|
|
587
|
+
});
|
|
588
|
+
else if (character === "]" && openers.length > 0) {
|
|
589
|
+
const position = openers.length - 1;
|
|
590
|
+
const opener = openers[position];
|
|
591
|
+
openers.pop();
|
|
592
|
+
const active = opener.image || position >= inactiveBelow;
|
|
593
|
+
inactiveBelow = Math.min(inactiveBelow, position);
|
|
594
|
+
if (!active) continue;
|
|
595
|
+
const end = linkEnd(opener.index, index);
|
|
596
|
+
if (end === -1) continue;
|
|
597
|
+
openerCloses.set(opener.index, index);
|
|
598
|
+
if (!opener.image) inactiveBelow = openers.length;
|
|
599
|
+
index = end - 1;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return openerCloses.get(open) ?? -1;
|
|
604
|
+
};
|
|
605
|
+
return {
|
|
606
|
+
backtickCloser,
|
|
607
|
+
closingBracket,
|
|
608
|
+
closingParen,
|
|
609
|
+
openerClose
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
/**
|
|
613
|
+
* Adds `value` to the last text node of `nodes`, unless that node is a
|
|
614
|
+
* delimiter run, which stays its own node until emphasis is resolved.
|
|
615
|
+
*/
|
|
616
|
+
function appendInlineText(nodes, delimiterNodes, value) {
|
|
617
|
+
const last = nodes[nodes.length - 1];
|
|
618
|
+
if (last?.type === "text" && delimiterNodes?.has(last) !== true) nodes[nodes.length - 1] = {
|
|
619
|
+
...last,
|
|
620
|
+
value: last.value + value
|
|
621
|
+
};
|
|
622
|
+
else nodes.push({
|
|
623
|
+
type: "text",
|
|
624
|
+
value
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
var UNICODE_WHITESPACE = /* @__PURE__ */ new RegExp("^[\\t\\n\\f\\r\\p{Zs}]$", "u");
|
|
628
|
+
var UNICODE_PUNCTUATION = /* @__PURE__ */ new RegExp("^[\\p{P}\\p{S}]$", "u");
|
|
629
|
+
/** The character before `index` (a whole code point), if any. */
|
|
630
|
+
function characterBefore(text, index) {
|
|
631
|
+
if (index <= 0) return;
|
|
632
|
+
const low = text.charCodeAt(index - 1);
|
|
633
|
+
if (low >= 56320 && low <= 57343 && index >= 2) {
|
|
634
|
+
const high = text.charCodeAt(index - 2);
|
|
635
|
+
if (high >= 55296 && high <= 56319) return text.slice(index - 2, index);
|
|
636
|
+
}
|
|
637
|
+
return text[index - 1];
|
|
638
|
+
}
|
|
639
|
+
/** The character at `index` (a whole code point), if any. */
|
|
640
|
+
function characterAt(text, index) {
|
|
641
|
+
if (index >= text.length) return;
|
|
642
|
+
const codePoint = text.codePointAt(index);
|
|
643
|
+
return codePoint == null ? void 0 : String.fromCodePoint(codePoint);
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* The delimiter run `text.slice(start, end)`: left- or right-flanking by the
|
|
647
|
+
* characters around it, and whether it can open or close emphasis — an
|
|
648
|
+
* underscore run only where it is not inside a word.
|
|
649
|
+
*/
|
|
650
|
+
function delimiterRun(character, text, start, end) {
|
|
651
|
+
const before = characterBefore(text, start);
|
|
652
|
+
const after = characterAt(text, end);
|
|
653
|
+
const isSpaceBefore = before == null || UNICODE_WHITESPACE.test(before);
|
|
654
|
+
const isSpaceAfter = after == null || UNICODE_WHITESPACE.test(after);
|
|
655
|
+
const isPunctuationBefore = before != null && UNICODE_PUNCTUATION.test(before);
|
|
656
|
+
const isPunctuationAfter = after != null && UNICODE_PUNCTUATION.test(after);
|
|
657
|
+
const isLeftFlanking = !isSpaceAfter && (!isPunctuationAfter || isSpaceBefore || isPunctuationBefore);
|
|
658
|
+
const isRightFlanking = !isSpaceBefore && (!isPunctuationBefore || isSpaceAfter || isPunctuationAfter);
|
|
659
|
+
return {
|
|
660
|
+
node: {
|
|
661
|
+
type: "text",
|
|
662
|
+
value: text.slice(start, end)
|
|
663
|
+
},
|
|
664
|
+
character,
|
|
665
|
+
originalLength: end - start,
|
|
666
|
+
length: end - start,
|
|
667
|
+
canOpen: character === "*" ? isLeftFlanking : isLeftFlanking && (!isRightFlanking || isPunctuationBefore),
|
|
668
|
+
canClose: character === "*" ? isRightFlanking : isRightFlanking && (!isLeftFlanking || isPunctuationAfter)
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* The deepest emphasis and strong may nest. Pairs that would nest deeper stay
|
|
673
|
+
* text, as other CommonMark parsers cap nesting, so a hostile input cannot
|
|
674
|
+
* build a tree that overflows the stack of a recursive walk or render.
|
|
675
|
+
*/
|
|
676
|
+
var MAX_EMPHASIS_DEPTH = 100;
|
|
677
|
+
/**
|
|
678
|
+
* Pairs the delimiter runs of one inline text into emphasis and strong, as
|
|
679
|
+
* CommonMark's process-emphasis procedure does: each closer, in order, takes
|
|
680
|
+
* the nearest opener of its character that the rule of three allows, two
|
|
681
|
+
* delimiters for strong when both have two, else one for emphasis, and the
|
|
682
|
+
* nodes between them become its children. A run left unpaired stays text,
|
|
683
|
+
* joined to the text beside it. `***x***` keeps strong outside emphasis, as
|
|
684
|
+
* Markdown has always drawn it. Runs are taken off a linked stack as they
|
|
685
|
+
* are used or passed, so the pass is linear in the number of runs, and
|
|
686
|
+
* nesting stops at MAX_EMPHASIS_DEPTH.
|
|
687
|
+
*/
|
|
688
|
+
function resolveEmphasis(nodes, delimiters) {
|
|
689
|
+
const head = {
|
|
690
|
+
node: {
|
|
691
|
+
type: "text",
|
|
692
|
+
value: ""
|
|
693
|
+
},
|
|
694
|
+
previous: null,
|
|
695
|
+
next: null
|
|
696
|
+
};
|
|
697
|
+
let tail = head;
|
|
698
|
+
const entryOf = /* @__PURE__ */ new Map();
|
|
699
|
+
for (const node of nodes) {
|
|
700
|
+
const entry = {
|
|
701
|
+
node,
|
|
702
|
+
previous: tail,
|
|
703
|
+
next: null
|
|
704
|
+
};
|
|
705
|
+
tail.next = entry;
|
|
706
|
+
tail = entry;
|
|
707
|
+
entryOf.set(node, entry);
|
|
708
|
+
}
|
|
709
|
+
const unlink = (entry) => {
|
|
710
|
+
if (entry.previous != null) entry.previous.next = entry.next;
|
|
711
|
+
if (entry.next != null) entry.next.previous = entry.previous;
|
|
712
|
+
};
|
|
713
|
+
const stack = delimiters.map((run, index) => ({
|
|
714
|
+
run,
|
|
715
|
+
index,
|
|
716
|
+
previous: null,
|
|
717
|
+
next: null,
|
|
718
|
+
depthAfter: 0,
|
|
719
|
+
canClose: run.canClose
|
|
720
|
+
}));
|
|
721
|
+
stack.forEach((item, index) => {
|
|
722
|
+
item.previous = stack[index - 1] ?? null;
|
|
723
|
+
item.next = stack[index + 1] ?? null;
|
|
724
|
+
});
|
|
725
|
+
const leave = (item) => {
|
|
726
|
+
if (item.previous != null) {
|
|
727
|
+
item.previous.next = item.next;
|
|
728
|
+
item.previous.depthAfter = Math.max(item.previous.depthAfter, item.depthAfter);
|
|
729
|
+
}
|
|
730
|
+
if (item.next != null) item.next.previous = item.previous;
|
|
731
|
+
};
|
|
732
|
+
const openersBottom = /* @__PURE__ */ new Map();
|
|
733
|
+
let lastStrong = null;
|
|
734
|
+
let closerItem = stack[0] ?? null;
|
|
735
|
+
while (closerItem != null) {
|
|
736
|
+
const closer = closerItem.run;
|
|
737
|
+
if (!closerItem.canClose || closer.length === 0) {
|
|
738
|
+
closerItem = closerItem.next;
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
const key = `${closer.character}${closer.canOpen ? 1 : 0}${closer.originalLength % 3}`;
|
|
742
|
+
const bottom = openersBottom.get(key) ?? -1;
|
|
743
|
+
let openerItem = closerItem.previous;
|
|
744
|
+
for (; openerItem != null && openerItem.index > bottom;) {
|
|
745
|
+
const candidate = openerItem.run;
|
|
746
|
+
const isOddMatch = (closer.canOpen || candidate.canClose) && (candidate.originalLength + closer.originalLength) % 3 === 0 && !(candidate.originalLength % 3 === 0 && closer.originalLength % 3 === 0);
|
|
747
|
+
if (candidate.character === closer.character && candidate.canOpen && candidate.length > 0 && !isOddMatch) break;
|
|
748
|
+
openerItem = openerItem.previous;
|
|
749
|
+
}
|
|
750
|
+
if (openerItem == null || openerItem.index <= bottom) {
|
|
751
|
+
openersBottom.set(key, closerItem.index - 1);
|
|
752
|
+
const next = closerItem.next;
|
|
753
|
+
if (!closer.canOpen) leave(closerItem);
|
|
754
|
+
closerItem = next;
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
const opener = openerItem.run;
|
|
758
|
+
let innerDepth = openerItem.depthAfter;
|
|
759
|
+
for (let between = openerItem.next; between != null && between !== closerItem; between = between.next) innerDepth = Math.max(innerDepth, between.depthAfter);
|
|
760
|
+
openerItem.next = closerItem;
|
|
761
|
+
closerItem.previous = openerItem;
|
|
762
|
+
openerItem.depthAfter = innerDepth;
|
|
763
|
+
if (innerDepth >= MAX_EMPHASIS_DEPTH) {
|
|
764
|
+
const next = closerItem.next;
|
|
765
|
+
leave(openerItem);
|
|
766
|
+
closerItem.canClose = false;
|
|
767
|
+
if (!closer.canOpen) leave(closerItem);
|
|
768
|
+
closerItem = next;
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
const openerEntry = entryOf.get(opener.node);
|
|
772
|
+
const closerEntry = entryOf.get(closer.node);
|
|
773
|
+
if (openerEntry == null || closerEntry == null) break;
|
|
774
|
+
const use = opener.length >= 2 && closer.length >= 2 ? 2 : 1;
|
|
775
|
+
const children = [];
|
|
776
|
+
for (let entry = openerEntry.next; entry != null && entry !== closerEntry; entry = entry.next) children.push(entry.node);
|
|
777
|
+
let wrapper;
|
|
778
|
+
if (use === 2) {
|
|
779
|
+
wrapper = {
|
|
780
|
+
type: "strong",
|
|
781
|
+
children
|
|
782
|
+
};
|
|
783
|
+
lastStrong = {
|
|
784
|
+
node: wrapper,
|
|
785
|
+
opener,
|
|
786
|
+
closer
|
|
787
|
+
};
|
|
788
|
+
} else {
|
|
789
|
+
const [only] = children;
|
|
790
|
+
wrapper = children.length === 1 && only === lastStrong?.node && lastStrong.opener === opener && lastStrong.closer === closer && only.type === "strong" ? {
|
|
791
|
+
type: "strong",
|
|
792
|
+
children: [{
|
|
793
|
+
type: "emphasis",
|
|
794
|
+
children: only.children
|
|
795
|
+
}]
|
|
796
|
+
} : {
|
|
797
|
+
type: "emphasis",
|
|
798
|
+
children
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
const wrapperEntry = {
|
|
802
|
+
node: wrapper,
|
|
803
|
+
previous: openerEntry,
|
|
804
|
+
next: closerEntry
|
|
805
|
+
};
|
|
806
|
+
openerEntry.next = wrapperEntry;
|
|
807
|
+
closerEntry.previous = wrapperEntry;
|
|
808
|
+
openerItem.depthAfter = innerDepth + 1;
|
|
809
|
+
opener.length -= use;
|
|
810
|
+
closer.length -= use;
|
|
811
|
+
opener.node.value = opener.character.repeat(opener.length);
|
|
812
|
+
closer.node.value = closer.character.repeat(closer.length);
|
|
813
|
+
if (opener.length === 0) {
|
|
814
|
+
unlink(openerEntry);
|
|
815
|
+
leave(openerItem);
|
|
816
|
+
}
|
|
817
|
+
if (closer.length === 0) {
|
|
818
|
+
unlink(closerEntry);
|
|
819
|
+
const next = closerItem.next;
|
|
820
|
+
leave(closerItem);
|
|
821
|
+
closerItem = next;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
const leftover = new Set(delimiters.filter((run) => run.length > 0).map((run) => run.node));
|
|
825
|
+
const result = [];
|
|
826
|
+
let previousIsLeftover = false;
|
|
827
|
+
for (let entry = head.next; entry != null; entry = entry.next) {
|
|
828
|
+
const { node } = entry;
|
|
829
|
+
const isLeftover = leftover.has(node);
|
|
830
|
+
const last = result[result.length - 1];
|
|
831
|
+
if (node.type === "text" && last?.type === "text" && (isLeftover || previousIsLeftover)) {
|
|
832
|
+
result[result.length - 1] = {
|
|
833
|
+
...last,
|
|
834
|
+
value: last.value + node.value
|
|
835
|
+
};
|
|
836
|
+
previousIsLeftover = true;
|
|
837
|
+
continue;
|
|
838
|
+
}
|
|
839
|
+
result.push(isLeftover ? {
|
|
840
|
+
type: "text",
|
|
841
|
+
value: node.value
|
|
842
|
+
} : node);
|
|
843
|
+
previousIsLeftover = isLeftover;
|
|
844
|
+
}
|
|
845
|
+
return result;
|
|
846
|
+
}
|
|
847
|
+
/** True when the character at `index` is preceded by an odd backslash run. */
|
|
848
|
+
function isEscaped(text, index) {
|
|
849
|
+
let backslashes = 0;
|
|
850
|
+
for (let i = index - 1; i >= 0 && text[i] === "\\"; i--) backslashes++;
|
|
851
|
+
return backslashes % 2 === 1;
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Find the closing delimiter for `$…$` math on the same line.
|
|
855
|
+
*
|
|
856
|
+
* The whitespace and numeric-edge rules mirror common dollar-math parsers:
|
|
857
|
+
* whitespace cannot hug the delimiters, a digit cannot sit immediately before
|
|
858
|
+
* the opener or after the closer, and `$$` is reserved for display math. The
|
|
859
|
+
* numeric guard prevents ordinary prose such as "$20 and $30" from becoming a
|
|
860
|
+
* formula even in a math-enabled document.
|
|
861
|
+
*/
|
|
862
|
+
function isInlineMathStart(text, index) {
|
|
863
|
+
return text[index] === "$" && text[index - 1] !== "$" && text[index + 1] !== "$" && text[index + 1] != null && !/\s/.test(text[index + 1]) && !/\d/.test(text[index - 1] ?? "") && !isEscaped(text, index);
|
|
864
|
+
}
|
|
865
|
+
function findInlineMathEnd(text, start) {
|
|
866
|
+
if (!isInlineMathStart(text, start)) return -1;
|
|
867
|
+
for (let index = start + 1; index < text.length; index++) {
|
|
868
|
+
if (text[index] === "\n") return -1;
|
|
869
|
+
if (text[index] !== "$" || isEscaped(text, index)) continue;
|
|
870
|
+
if (text[index - 1] === "$" || text[index + 1] === "$" || /\s/.test(text[index - 1]) || /\d/.test(text[index + 1] ?? "")) return -1;
|
|
871
|
+
return index;
|
|
872
|
+
}
|
|
873
|
+
return -1;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Match a fullwidth bracket citation 【id】 at position `i`.
|
|
877
|
+
* Returns the sourceId and end index, or null if no match.
|
|
878
|
+
*/
|
|
879
|
+
function matchFullwidthCitation(text, i, opts) {
|
|
880
|
+
if (!opts.sourceIds || text[i] !== "【") return null;
|
|
881
|
+
const closeIndex = text.indexOf("】", i + 1);
|
|
882
|
+
if (closeIndex === -1) return null;
|
|
883
|
+
const id = text.slice(i + 1, closeIndex);
|
|
884
|
+
if (id.length === 0 || !opts.sourceIds.has(id)) return null;
|
|
885
|
+
return {
|
|
886
|
+
sourceId: id,
|
|
887
|
+
end: closeIndex + 1
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Match a bracket citation [id] at position `i`.
|
|
892
|
+
* Only matches if the id exists in sourceIds and is NOT followed by `(` (link).
|
|
893
|
+
*/
|
|
894
|
+
function matchBracketCitation(text, i, opts) {
|
|
895
|
+
if (!opts.sourceIds || text[i] !== "[") return null;
|
|
896
|
+
const closeIndex = text.indexOf("]", i + 1);
|
|
897
|
+
if (closeIndex === -1) return null;
|
|
898
|
+
if (text[closeIndex + 1] === "(") return null;
|
|
899
|
+
const id = text.slice(i + 1, closeIndex);
|
|
900
|
+
if (id.length === 0 || !opts.sourceIds.has(id)) return null;
|
|
901
|
+
return {
|
|
902
|
+
sourceId: id,
|
|
903
|
+
end: closeIndex + 1
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
function currentLineStart(source, offset) {
|
|
907
|
+
return source.lastIndexOf("\n", offset - 1) + 1;
|
|
908
|
+
}
|
|
909
|
+
function matchExtensionSyntax(source, offset, context, opts) {
|
|
910
|
+
const candidates = (context === "inline" ? opts.plugins?.inlineByFirstCharacter : opts.allowBlockSyntax === false ? void 0 : opts.plugins?.blockByFirstCharacter)?.get(source[offset]);
|
|
911
|
+
if (candidates == null) return { status: "none" };
|
|
912
|
+
const seen = /* @__PURE__ */ new Set();
|
|
913
|
+
for (const candidate of candidates) {
|
|
914
|
+
if (seen.has(candidate)) continue;
|
|
915
|
+
seen.add(candidate);
|
|
916
|
+
let fullPrefix = false;
|
|
917
|
+
for (const prefix of candidate.contribution.startsWith) {
|
|
918
|
+
if (source.startsWith(prefix, offset)) {
|
|
919
|
+
fullPrefix = true;
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
if (!opts.isFinal) {
|
|
923
|
+
const remaining = source.slice(offset);
|
|
924
|
+
if (remaining.length < prefix.length && prefix.startsWith(remaining)) return { status: "defer" };
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
if (!fullPrefix) continue;
|
|
928
|
+
const end = Math.min(source.length, offset + candidate.contribution.maxSpan);
|
|
929
|
+
let result;
|
|
930
|
+
try {
|
|
931
|
+
result = candidate.contribution.tokenize({
|
|
932
|
+
source: end === source.length ? source : source.slice(0, end),
|
|
933
|
+
offset,
|
|
934
|
+
end,
|
|
935
|
+
isFinal: opts.isFinal,
|
|
936
|
+
context,
|
|
937
|
+
lineStart: currentLineStart(source, offset),
|
|
938
|
+
column: offset - currentLineStart(source, offset)
|
|
939
|
+
});
|
|
940
|
+
} catch (error) {
|
|
941
|
+
reportMarkdownPluginFailure(candidate.pluginName, "syntax", error);
|
|
942
|
+
continue;
|
|
943
|
+
}
|
|
944
|
+
if (result == null || typeof result !== "object" || typeof result.then === "function") {
|
|
945
|
+
reportMarkdownPluginFailure(candidate.pluginName, "syntax", /* @__PURE__ */ new TypeError("Tokenizer returned an invalid result"));
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
if (result.status === "no-match") continue;
|
|
949
|
+
if (result.status === "defer") return !opts.isFinal && end === source.length && source.length - offset < candidate.contribution.maxSpan ? { status: "defer" } : { status: "none" };
|
|
950
|
+
if (result.status !== "match" || !Number.isInteger(result.end) || result.end <= offset || result.end > end || result.node.type !== "extension" || result.node.plugin !== candidate.pluginName || result.node.display !== context || result.node.name.trim() === "" || !isMarkdownPluginData(result.node.data) || !opts.plugins?.renderers.has(`${candidate.pluginName}\0${result.node.name}`)) {
|
|
951
|
+
reportMarkdownPluginFailure(candidate.pluginName, "syntax", /* @__PURE__ */ new TypeError("Tokenizer returned an invalid extension node"));
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
const { source: _ignoredSource, position: _ignoredPosition, ...safeNode } = result.node;
|
|
955
|
+
const absoluteStart = (opts.baseOffset ?? 0) + offset;
|
|
956
|
+
const absoluteEnd = (opts.baseOffset ?? 0) + result.end;
|
|
957
|
+
return {
|
|
958
|
+
status: "match",
|
|
959
|
+
end: result.end,
|
|
960
|
+
node: Object.freeze({
|
|
961
|
+
...safeNode,
|
|
962
|
+
data: freezeMarkdownPluginData(result.node.data),
|
|
963
|
+
source: source.slice(offset, result.end),
|
|
964
|
+
...opts.astPositions === true && context === "block" ? { position: Object.freeze({
|
|
965
|
+
start: Object.freeze({ offset: absoluteStart }),
|
|
966
|
+
end: Object.freeze({ offset: absoluteEnd })
|
|
967
|
+
}) } : null
|
|
968
|
+
})
|
|
969
|
+
};
|
|
970
|
+
}
|
|
971
|
+
return { status: "none" };
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* The length of the line ending at `index` in inline text: 1 for LF, 2 for
|
|
975
|
+
* CRLF (the parser keeps a CRLF line's `\r`), or 0 when none starts there.
|
|
976
|
+
*/
|
|
977
|
+
function lineEndingLengthAt(text, index) {
|
|
978
|
+
if (text[index] === "\n") return 1;
|
|
979
|
+
return text[index] === "\r" && text[index + 1] === "\n" ? 2 : 0;
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Internal block-level inline entry point: parses, then applies the GFM
|
|
983
|
+
* autolink transform when enabled. Recursive calls inside `parseInlineImpl`
|
|
984
|
+
* (link labels, bold/italic/strikethrough bodies) intentionally bypass this
|
|
985
|
+
* wrapper and call `parseInlineImpl` directly so the transform runs only on
|
|
986
|
+
* the outermost block's inline tree — letting `transformAutolinks` decide
|
|
987
|
+
* which subtrees to descend into (text, bold, italic, strikethrough) and
|
|
988
|
+
* which to skip (link, code, math, image, citation, break).
|
|
989
|
+
*/
|
|
990
|
+
function parseInlineEntry(text, opts, context = "default") {
|
|
991
|
+
const nodes = parseInlineImpl(text, opts, context);
|
|
992
|
+
return opts.autolink === "gfm" ? transformAutolinks(nodes) : nodes;
|
|
993
|
+
}
|
|
994
|
+
function parseInlineImpl(text, opts, context = "default") {
|
|
995
|
+
const nodes = [];
|
|
996
|
+
let delimiters = null;
|
|
997
|
+
let delimiterNodes = null;
|
|
998
|
+
const inlineIndex = inlineIndexOf(text, opts.linkDefs);
|
|
999
|
+
const inlineExtensionStarts = (opts.plugins?.inlineByFirstCharacter.size ?? 0) > 0 ? opts.plugins?.inlineByFirstCharacter : void 0;
|
|
1000
|
+
let i = 0;
|
|
1001
|
+
while (i < text.length) {
|
|
1002
|
+
if (text[i] === "\\" && i + 1 < text.length) {
|
|
1003
|
+
const lineEnding = lineEndingLengthAt(text, i + 1);
|
|
1004
|
+
if (lineEnding > 0) {
|
|
1005
|
+
nodes.push({ type: "break" });
|
|
1006
|
+
i += 1 + lineEnding;
|
|
1007
|
+
continue;
|
|
1008
|
+
}
|
|
1009
|
+
if (isAsciiPunctuation(text[i + 1])) {
|
|
1010
|
+
nodes.push({
|
|
1011
|
+
type: "text",
|
|
1012
|
+
value: text[i + 1]
|
|
1013
|
+
});
|
|
1014
|
+
i += 2;
|
|
1015
|
+
continue;
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
if (text[i] === "`") {
|
|
1019
|
+
let openIndex = i;
|
|
1020
|
+
while (text[openIndex] === "`") openIndex++;
|
|
1021
|
+
const tickCount = openIndex - i;
|
|
1022
|
+
const closeIndex = inlineIndex.backtickCloser(openIndex, tickCount);
|
|
1023
|
+
if (closeIndex === -1) {
|
|
1024
|
+
appendInlineText(nodes, delimiterNodes, text.slice(i, openIndex));
|
|
1025
|
+
i = openIndex;
|
|
1026
|
+
continue;
|
|
1027
|
+
}
|
|
1028
|
+
nodes.push({
|
|
1029
|
+
type: "inlineCode",
|
|
1030
|
+
value: context === "tableCell" ? text.slice(openIndex, closeIndex).replace(/\\\|/g, "|") : text.slice(openIndex, closeIndex)
|
|
1031
|
+
});
|
|
1032
|
+
i = closeIndex + tickCount;
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
1035
|
+
if (opts.math && text[i] === "$") {
|
|
1036
|
+
const closeIndex = findInlineMathEnd(text, i);
|
|
1037
|
+
if (closeIndex !== -1) {
|
|
1038
|
+
nodes.push({
|
|
1039
|
+
type: "inlineMath",
|
|
1040
|
+
value: text.slice(i + 1, closeIndex)
|
|
1041
|
+
});
|
|
1042
|
+
i = closeIndex + 1;
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
{
|
|
1047
|
+
const citation = matchFullwidthCitation(text, i, opts);
|
|
1048
|
+
if (citation) {
|
|
1049
|
+
nodes.push({
|
|
1050
|
+
type: "citation",
|
|
1051
|
+
sourceId: citation.sourceId
|
|
1052
|
+
});
|
|
1053
|
+
i = citation.end;
|
|
1054
|
+
continue;
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (text[i] === "!" && text[i + 1] === "[") {
|
|
1058
|
+
const altClose = inlineIndex.openerClose(i + 1);
|
|
1059
|
+
if (altClose !== -1 && text[altClose + 1] === "(") {
|
|
1060
|
+
const angleClose = angleDestinationClose(text, altClose + 1);
|
|
1061
|
+
const srcClose = angleClose === "refused" ? -1 : angleClose ?? inlineIndex.closingParen(altClose + 1);
|
|
1062
|
+
if (srcClose !== -1) {
|
|
1063
|
+
const src = decodeLinkDestination(inlineDestination(text.slice(altClose + 2, srcClose)));
|
|
1064
|
+
if (!isSafeMdParserUrl(src)) nodes.push({
|
|
1065
|
+
type: "text",
|
|
1066
|
+
value: text.slice(i, srcClose + 1)
|
|
1067
|
+
});
|
|
1068
|
+
else nodes.push({
|
|
1069
|
+
type: "image",
|
|
1070
|
+
url: src,
|
|
1071
|
+
alt: imageAlt(text.slice(i + 2, altClose), opts, context)
|
|
1072
|
+
});
|
|
1073
|
+
i = srcClose + 1;
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
if (opts.linkDefs != null && text[i] === "!" && text[i + 1] === "[") {
|
|
1079
|
+
const ref = matchReferenceImage(text, i, opts.linkDefs, inlineIndex, opts, context);
|
|
1080
|
+
if (ref) {
|
|
1081
|
+
nodes.push(ref.node);
|
|
1082
|
+
i = ref.end;
|
|
1083
|
+
continue;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
{
|
|
1087
|
+
const citation = matchBracketCitation(text, i, opts);
|
|
1088
|
+
if (citation) {
|
|
1089
|
+
nodes.push({
|
|
1090
|
+
type: "citation",
|
|
1091
|
+
sourceId: citation.sourceId
|
|
1092
|
+
});
|
|
1093
|
+
i = citation.end;
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
if (text[i] === "[") {
|
|
1098
|
+
const textClose = inlineIndex.openerClose(i);
|
|
1099
|
+
if (textClose !== -1 && text[textClose + 1] === "(") {
|
|
1100
|
+
const angleClose = angleDestinationClose(text, textClose + 1);
|
|
1101
|
+
const urlClose = angleClose === "refused" ? -1 : angleClose ?? inlineIndex.closingParen(textClose + 1);
|
|
1102
|
+
if (urlClose !== -1) {
|
|
1103
|
+
const href = decodeLinkDestination(inlineDestination(text.slice(textClose + 2, urlClose)));
|
|
1104
|
+
if (!isSafeMdParserUrl(href)) nodes.push({
|
|
1105
|
+
type: "text",
|
|
1106
|
+
value: text.slice(i, urlClose + 1)
|
|
1107
|
+
});
|
|
1108
|
+
else nodes.push({
|
|
1109
|
+
type: "link",
|
|
1110
|
+
url: href,
|
|
1111
|
+
children: parseInlineImpl(text.slice(i + 1, textClose), opts, context)
|
|
1112
|
+
});
|
|
1113
|
+
i = urlClose + 1;
|
|
1114
|
+
continue;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
if (opts.linkDefs != null && text[i] === "[") {
|
|
1119
|
+
const ref = matchReferenceLink(text, i, opts.linkDefs, opts, context, inlineIndex);
|
|
1120
|
+
if (ref) {
|
|
1121
|
+
nodes.push(ref.node);
|
|
1122
|
+
i = ref.end;
|
|
1123
|
+
continue;
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
if (text[i] === "*" || text[i] === "_") {
|
|
1127
|
+
const character = text[i];
|
|
1128
|
+
let runEnd = i + 1;
|
|
1129
|
+
while (text[runEnd] === character) runEnd++;
|
|
1130
|
+
const run = delimiterRun(character, text, i, runEnd);
|
|
1131
|
+
if (run.canOpen || run.canClose) {
|
|
1132
|
+
nodes.push(run.node);
|
|
1133
|
+
(delimiters ??= []).push(run);
|
|
1134
|
+
(delimiterNodes ??= /* @__PURE__ */ new Set()).add(run.node);
|
|
1135
|
+
i = runEnd;
|
|
1136
|
+
continue;
|
|
1137
|
+
}
|
|
1138
|
+
if (inlineExtensionStarts?.has(character) !== true) {
|
|
1139
|
+
appendInlineText(nodes, delimiterNodes, text.slice(i, runEnd));
|
|
1140
|
+
i = runEnd;
|
|
1141
|
+
continue;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
if (text[i] === "~" && text[i + 1] === "~") {
|
|
1145
|
+
const closeIndex = text.indexOf("~~", i + 2);
|
|
1146
|
+
if (closeIndex !== -1) {
|
|
1147
|
+
nodes.push({
|
|
1148
|
+
type: "delete",
|
|
1149
|
+
children: parseInlineImpl(text.slice(i + 2, closeIndex), opts, context)
|
|
1150
|
+
});
|
|
1151
|
+
i = closeIndex + 2;
|
|
1152
|
+
continue;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
if (inlineExtensionStarts !== void 0) {
|
|
1156
|
+
const extension = matchExtensionSyntax(text, i, "inline", opts);
|
|
1157
|
+
if (extension.status === "match") {
|
|
1158
|
+
nodes.push(extension.node);
|
|
1159
|
+
i = extension.end;
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
if (extension.status === "defer") break;
|
|
1163
|
+
}
|
|
1164
|
+
if (text[i] === "&") {
|
|
1165
|
+
const reference = matchCharacterReference(text, i);
|
|
1166
|
+
if (reference != null) {
|
|
1167
|
+
appendInlineText(nodes, delimiterNodes, reference.value);
|
|
1168
|
+
i = reference.end;
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
let end = i + 1;
|
|
1173
|
+
while (end < text.length && !"*_~`[!\\\n【&".includes(text[end]) && !(opts.math && text[end] === "$") && !(inlineExtensionStarts !== void 0 && inlineExtensionStarts.has(text[end]))) end++;
|
|
1174
|
+
const content = text.slice(i, end);
|
|
1175
|
+
if (end < text.length && text[end] === "\n") {
|
|
1176
|
+
const line = content.endsWith("\r") ? content.slice(0, -1) : content;
|
|
1177
|
+
let spaces = 0;
|
|
1178
|
+
while (line[line.length - 1 - spaces] === " ") spaces++;
|
|
1179
|
+
const trimmed = line.slice(0, line.length - spaces);
|
|
1180
|
+
if (spaces >= 2) {
|
|
1181
|
+
if (trimmed.length > 0) appendInlineText(nodes, delimiterNodes, trimmed);
|
|
1182
|
+
nodes.push({ type: "break" });
|
|
1183
|
+
i = end + 1;
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
appendInlineText(nodes, delimiterNodes, content);
|
|
1188
|
+
i = end;
|
|
1189
|
+
}
|
|
1190
|
+
return delimiters == null ? nodes : resolveEmphasis(nodes, delimiters);
|
|
1191
|
+
}
|
|
1192
|
+
var ANY_NON_WHITESPACE_OR_ANGLE = "[^\\s<]+";
|
|
1193
|
+
var SCHEME = "https?:\\/\\/";
|
|
1194
|
+
var EMAIL_LOCAL_PART = "[A-Za-z0-9._%+-]+";
|
|
1195
|
+
var DOMAIN_LABEL = "[A-Za-z0-9-]+";
|
|
1196
|
+
var DOMAIN_WITH_DOT = `${DOMAIN_LABEL}(?:\\.${DOMAIN_LABEL})+`;
|
|
1197
|
+
var ANGLE_SCHEME = "[a-zA-Z][a-zA-Z0-9+.-]*";
|
|
1198
|
+
var ANGLE_URL_BODY = "[^<>\\s]*";
|
|
1199
|
+
/** Bare http(s) URL up to whitespace or `<`. Trailing punctuation is peeled afterwards. */
|
|
1200
|
+
var URL_LITERAL_RE = new RegExp(`${SCHEME}${ANY_NON_WHITESPACE_OR_ANGLE}`, "g");
|
|
1201
|
+
/** Bare www. URL. Resulting href gets `http://` prepended. */
|
|
1202
|
+
var WWW_LITERAL_RE = new RegExp(`www\\.${ANY_NON_WHITESPACE_OR_ANGLE}`, "g");
|
|
1203
|
+
/** Bare email: local-part@domain (at least one dot in domain). */
|
|
1204
|
+
var EMAIL_LITERAL_RE = new RegExp(`${EMAIL_LOCAL_PART}@${DOMAIN_WITH_DOT}`, "g");
|
|
1205
|
+
/** Angle-bracket autolink: `<scheme:url>` (CommonMark §6.5). */
|
|
1206
|
+
var ANGLE_URL_RE = new RegExp(`<(${ANGLE_SCHEME}:${ANGLE_URL_BODY})>`, "g");
|
|
1207
|
+
/** Angle-bracket email: `<user@host.tld>`. */
|
|
1208
|
+
var ANGLE_EMAIL_RE = new RegExp(`<(${EMAIL_LOCAL_PART}@${DOMAIN_WITH_DOT})>`, "g");
|
|
1209
|
+
/** Characters treated as trailing sentence punctuation per GFM §6.9. */
|
|
1210
|
+
var TRAILING_PUNCT_CHARS = /* @__PURE__ */ new Set("?!.,:*_~");
|
|
1211
|
+
/** Characters valid in an email local part (used for boundary detection). */
|
|
1212
|
+
var EMAIL_LOCAL_CHARS = /* @__PURE__ */ new Set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._%+-@");
|
|
1213
|
+
/**
|
|
1214
|
+
* Peel trailing sentence-end punctuation and unbalanced trailing `)` off a
|
|
1215
|
+
* bare URL. Mirrors GFM §6.9: `?!.,:*_~` immediately after the URL aren't
|
|
1216
|
+
* part of it; a trailing `)` is excluded if there are more `)` than `(` in
|
|
1217
|
+
* the candidate (so `(https://example.com)` ends at the second `)` but
|
|
1218
|
+
* `https://example.com/Foo_(bar)` keeps the inner pair).
|
|
1219
|
+
*/
|
|
1220
|
+
function peelTrailingPunctAndParens(url) {
|
|
1221
|
+
let s = url;
|
|
1222
|
+
while (s.length > 0) {
|
|
1223
|
+
if (TRAILING_PUNCT_CHARS.has(s[s.length - 1])) {
|
|
1224
|
+
let end = s.length - 1;
|
|
1225
|
+
while (end > 0 && TRAILING_PUNCT_CHARS.has(s[end - 1])) end--;
|
|
1226
|
+
s = s.slice(0, end);
|
|
1227
|
+
continue;
|
|
1228
|
+
}
|
|
1229
|
+
if (s.endsWith(")")) {
|
|
1230
|
+
let open = 0;
|
|
1231
|
+
let close = 0;
|
|
1232
|
+
for (let idx = 0; idx < s.length; idx++) if (s[idx] === "(") open++;
|
|
1233
|
+
else if (s[idx] === ")") close++;
|
|
1234
|
+
if (close > open) {
|
|
1235
|
+
s = s.slice(0, -1);
|
|
1236
|
+
continue;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
break;
|
|
1240
|
+
}
|
|
1241
|
+
return s;
|
|
1242
|
+
}
|
|
1243
|
+
/** Characters that, when preceding a URL, indicate it's part of a larger token. */
|
|
1244
|
+
var URL_CONTINUATION_CHARS = /* @__PURE__ */ new Set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/=");
|
|
1245
|
+
/**
|
|
1246
|
+
* The character immediately before a bare-URL or bare-www match must not
|
|
1247
|
+
* make the URL look like the tail of a larger token (`xhttps`, `=https://`,
|
|
1248
|
+
* `/https://`). null means start-of-text — always allowed.
|
|
1249
|
+
*/
|
|
1250
|
+
function isUrlBoundaryChar(ch) {
|
|
1251
|
+
if (ch == null) return true;
|
|
1252
|
+
return !URL_CONTINUATION_CHARS.has(ch);
|
|
1253
|
+
}
|
|
1254
|
+
function scanAutolinksInText(text) {
|
|
1255
|
+
const matches = [];
|
|
1256
|
+
{
|
|
1257
|
+
const re = new RegExp(ANGLE_URL_RE.source, "g");
|
|
1258
|
+
let m;
|
|
1259
|
+
while ((m = re.exec(text)) !== null) {
|
|
1260
|
+
const url = m[1];
|
|
1261
|
+
if (!isSafeMdParserUrl(url)) continue;
|
|
1262
|
+
matches.push({
|
|
1263
|
+
start: m.index,
|
|
1264
|
+
end: m.index + m[0].length,
|
|
1265
|
+
href: url,
|
|
1266
|
+
display: url
|
|
1267
|
+
});
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
{
|
|
1271
|
+
const re = new RegExp(ANGLE_EMAIL_RE.source, "g");
|
|
1272
|
+
let m;
|
|
1273
|
+
while ((m = re.exec(text)) !== null) {
|
|
1274
|
+
const email = m[1];
|
|
1275
|
+
matches.push({
|
|
1276
|
+
start: m.index,
|
|
1277
|
+
end: m.index + m[0].length,
|
|
1278
|
+
href: `mailto:${email}`,
|
|
1279
|
+
display: email
|
|
1280
|
+
});
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
{
|
|
1284
|
+
const re = new RegExp(URL_LITERAL_RE.source, "g");
|
|
1285
|
+
let m;
|
|
1286
|
+
while ((m = re.exec(text)) !== null) {
|
|
1287
|
+
const prev = text[m.index - 1];
|
|
1288
|
+
if (!isUrlBoundaryChar(prev)) continue;
|
|
1289
|
+
if (text.slice(m.index - 2, m.index) === "](") continue;
|
|
1290
|
+
const cleaned = peelTrailingPunctAndParens(m[0]);
|
|
1291
|
+
if (cleaned.length === 0) continue;
|
|
1292
|
+
matches.push({
|
|
1293
|
+
start: m.index,
|
|
1294
|
+
end: m.index + cleaned.length,
|
|
1295
|
+
href: cleaned,
|
|
1296
|
+
display: cleaned
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
{
|
|
1301
|
+
const re = new RegExp(WWW_LITERAL_RE.source, "g");
|
|
1302
|
+
let m;
|
|
1303
|
+
while ((m = re.exec(text)) !== null) {
|
|
1304
|
+
const prev = text[m.index - 1];
|
|
1305
|
+
if (!isUrlBoundaryChar(prev)) continue;
|
|
1306
|
+
if (text.slice(m.index - 3, m.index) === "://") continue;
|
|
1307
|
+
const cleaned = peelTrailingPunctAndParens(m[0]);
|
|
1308
|
+
if (cleaned.length === 0) continue;
|
|
1309
|
+
matches.push({
|
|
1310
|
+
start: m.index,
|
|
1311
|
+
end: m.index + cleaned.length,
|
|
1312
|
+
href: `http://${cleaned}`,
|
|
1313
|
+
display: cleaned
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
{
|
|
1318
|
+
const re = new RegExp(EMAIL_LITERAL_RE.source, "g");
|
|
1319
|
+
let m;
|
|
1320
|
+
while ((m = re.exec(text)) !== null) {
|
|
1321
|
+
const prev = text[m.index - 1];
|
|
1322
|
+
if (prev != null && EMAIL_LOCAL_CHARS.has(prev)) continue;
|
|
1323
|
+
if (text.slice(Math.max(0, m.index - 7), m.index) === "mailto:") continue;
|
|
1324
|
+
matches.push({
|
|
1325
|
+
start: m.index,
|
|
1326
|
+
end: m.index + m[0].length,
|
|
1327
|
+
href: `mailto:${m[0]}`,
|
|
1328
|
+
display: m[0]
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
matches.sort((a, b) => a.start - b.start);
|
|
1333
|
+
const resolved = [];
|
|
1334
|
+
let lastEnd = 0;
|
|
1335
|
+
for (const m of matches) if (m.start >= lastEnd) {
|
|
1336
|
+
resolved.push(m);
|
|
1337
|
+
lastEnd = m.end;
|
|
1338
|
+
}
|
|
1339
|
+
return resolved;
|
|
1340
|
+
}
|
|
1341
|
+
/**
|
|
1342
|
+
* Split a text-node `content` string into a sequence of text + link nodes
|
|
1343
|
+
* based on autolink matches.
|
|
1344
|
+
*/
|
|
1345
|
+
function splitTextOnAutolinks(content) {
|
|
1346
|
+
const matches = scanAutolinksInText(content);
|
|
1347
|
+
if (matches.length === 0) return [{
|
|
1348
|
+
type: "text",
|
|
1349
|
+
value: content
|
|
1350
|
+
}];
|
|
1351
|
+
const out = [];
|
|
1352
|
+
let cursor = 0;
|
|
1353
|
+
for (const m of matches) {
|
|
1354
|
+
if (m.start > cursor) out.push({
|
|
1355
|
+
type: "text",
|
|
1356
|
+
value: content.slice(cursor, m.start)
|
|
1357
|
+
});
|
|
1358
|
+
out.push({
|
|
1359
|
+
type: "link",
|
|
1360
|
+
url: m.href,
|
|
1361
|
+
children: [{
|
|
1362
|
+
type: "text",
|
|
1363
|
+
value: m.display
|
|
1364
|
+
}]
|
|
1365
|
+
});
|
|
1366
|
+
cursor = m.end;
|
|
1367
|
+
}
|
|
1368
|
+
if (cursor < content.length) out.push({
|
|
1369
|
+
type: "text",
|
|
1370
|
+
value: content.slice(cursor)
|
|
1371
|
+
});
|
|
1372
|
+
return out;
|
|
1373
|
+
}
|
|
1374
|
+
/**
|
|
1375
|
+
* Walk an inline-node tree and replace bare URLs / emails inside `text`
|
|
1376
|
+
* nodes with `link` nodes. Recurses into emphasis containers
|
|
1377
|
+
* (`bold`/`italic`/`strikethrough`) so wrapped URLs link too, but never
|
|
1378
|
+
* descends into existing `link` children (no nested links), `code` content,
|
|
1379
|
+
* `image` alt text, `citation`, or `break`. Runs only on the outermost
|
|
1380
|
+
* block's inline tree (see `parseInlineEntry`).
|
|
1381
|
+
*/
|
|
1382
|
+
function transformAutolinks(nodes) {
|
|
1383
|
+
const out = [];
|
|
1384
|
+
for (const node of nodes) if (node.type === "text") {
|
|
1385
|
+
const split = splitTextOnAutolinks(node.value);
|
|
1386
|
+
for (const seg of split) {
|
|
1387
|
+
const last = out[out.length - 1];
|
|
1388
|
+
if (seg.type === "text" && last?.type === "text") out[out.length - 1] = {
|
|
1389
|
+
...last,
|
|
1390
|
+
value: last.value + seg.value
|
|
1391
|
+
};
|
|
1392
|
+
else out.push(seg);
|
|
1393
|
+
}
|
|
1394
|
+
} else if (node.type === "strong" || node.type === "emphasis" || node.type === "delete") out.push({
|
|
1395
|
+
...node,
|
|
1396
|
+
children: transformAutolinks(node.children)
|
|
1397
|
+
});
|
|
1398
|
+
else out.push(node);
|
|
1399
|
+
return out;
|
|
1400
|
+
}
|
|
1401
|
+
function getIndent(line) {
|
|
1402
|
+
let count = 0;
|
|
1403
|
+
while (count < line.length && line[count] === " ") count++;
|
|
1404
|
+
return count;
|
|
1405
|
+
}
|
|
1406
|
+
/** HR: 3+ identical markers (-, *, _) optionally separated by spaces. */
|
|
1407
|
+
function isHorizontalRule(line) {
|
|
1408
|
+
const trimmed = line.trim();
|
|
1409
|
+
const ch = trimmed[0];
|
|
1410
|
+
if (trimmed.length < 3 || ch !== "-" && ch !== "*" && ch !== "_" || !trimmed.endsWith(ch)) return false;
|
|
1411
|
+
let count = 0;
|
|
1412
|
+
for (let idx = 0; idx < trimmed.length; idx++) {
|
|
1413
|
+
const character = trimmed[idx];
|
|
1414
|
+
if (character === ch) count++;
|
|
1415
|
+
else if (character !== " ") return false;
|
|
1416
|
+
}
|
|
1417
|
+
return count >= 3;
|
|
1418
|
+
}
|
|
1419
|
+
/** GFM separator row: cells contain only dashes/colons. */
|
|
1420
|
+
function isTableSeparator(line) {
|
|
1421
|
+
if (!line.includes("|")) return false;
|
|
1422
|
+
const nonEmpty = line.split("|").map((cell) => cell.trim()).filter((cell) => cell.length > 0);
|
|
1423
|
+
return nonEmpty.length > 0 && nonEmpty.every((cell) => /^:?-+:?$/.test(cell));
|
|
1424
|
+
}
|
|
1425
|
+
/**
|
|
1426
|
+
* The same options for content parsed out of an enclosing block. Ranges are a
|
|
1427
|
+
* top-level contract: a list item's or a blockquote's children are parsed from
|
|
1428
|
+
* text the caller reassembled (markers and `>` prefixes stripped), so an
|
|
1429
|
+
* offset into it would not address the document.
|
|
1430
|
+
*/
|
|
1431
|
+
function nested(opts) {
|
|
1432
|
+
return {
|
|
1433
|
+
...opts,
|
|
1434
|
+
allowBlockSyntax: false,
|
|
1435
|
+
astPositions: false,
|
|
1436
|
+
blockDepth: opts.blockDepth + 1,
|
|
1437
|
+
baseLine: void 0,
|
|
1438
|
+
lineOrigin: void 0
|
|
1439
|
+
};
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* The same options for content parsed out of an enclosing block whose input
|
|
1443
|
+
* lines came from the document lines `origin` names. Without source lines,
|
|
1444
|
+
* exactly `nested(opts)`.
|
|
1445
|
+
*/
|
|
1446
|
+
function nestedAt(opts, origin) {
|
|
1447
|
+
return origin == null ? nested(opts) : {
|
|
1448
|
+
...nested(opts),
|
|
1449
|
+
lineOrigin: origin
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
function blockExtensionColumn(line) {
|
|
1453
|
+
const indentation = line.length - line.trimStart().length;
|
|
1454
|
+
return indentation <= 3 ? indentation : null;
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* A block quote marker: up to three spaces of indentation, then `>`
|
|
1458
|
+
* (CommonMark 0.31 §5.1). Text may follow the `>` directly.
|
|
1459
|
+
*/
|
|
1460
|
+
var QUOTE_MARKER = /^ {0,3}>/;
|
|
1461
|
+
/** A quoted line's content: the marker and the one space or tab after it. */
|
|
1462
|
+
function quotedContent(line) {
|
|
1463
|
+
return line.replace(/^ {0,3}>[ \t]?/, "");
|
|
1464
|
+
}
|
|
1465
|
+
/**
|
|
1466
|
+
* Returns true when a line could start a new block — used to stop paragraph
|
|
1467
|
+
* continuation. Every regex here uses bounded or single-class quantifiers
|
|
1468
|
+
* to avoid ReDoS.
|
|
1469
|
+
*/
|
|
1470
|
+
function isBlockStart(line) {
|
|
1471
|
+
if (/^ {0,3}#{1,6} /.test(line)) return true;
|
|
1472
|
+
if (FENCE_LINE.test(line)) return true;
|
|
1473
|
+
if (isHorizontalRule(line)) return true;
|
|
1474
|
+
if (QUOTE_MARKER.test(line)) return true;
|
|
1475
|
+
if (/^ {0,9}[-*+] /.test(line)) return true;
|
|
1476
|
+
if (/^ {0,9}0*1[.)] /.test(line)) return true;
|
|
1477
|
+
if (line.includes("|")) return true;
|
|
1478
|
+
return false;
|
|
1479
|
+
}
|
|
1480
|
+
/**
|
|
1481
|
+
* A lazy continuation may omit an owning quote marker or list indentation only
|
|
1482
|
+
* while the deepest open leaf is a paragraph. A blank line or an interrupting
|
|
1483
|
+
* block start closes that opportunity.
|
|
1484
|
+
*/
|
|
1485
|
+
function canContinueParagraphLazily(lines, lineIndex, insideList = false) {
|
|
1486
|
+
const line = lines[lineIndex];
|
|
1487
|
+
if (line == null || line.trim() === "") return false;
|
|
1488
|
+
if (/^ {0,3}#{1,6} /.test(line) || FENCE_LINE.test(line) || isHorizontalRule(line) || QUOTE_MARKER.test(line) || /^ {0,9}[-*+] /.test(line) || (insideList ? /^ {0,9}\d+[.)] /.test(line) : /^ {0,9}0*1[.)] /.test(line))) return false;
|
|
1489
|
+
return !(line.includes("|") && lineIndex + 1 < lines.length && isTableSeparator(lines[lineIndex + 1]));
|
|
1490
|
+
}
|
|
1491
|
+
function endsInParagraph(blocks) {
|
|
1492
|
+
const last = blocks[blocks.length - 1];
|
|
1493
|
+
if (last == null) return false;
|
|
1494
|
+
if (last.type === "paragraph") return true;
|
|
1495
|
+
if (last.type === "blockquote") return endsInParagraph(last.children);
|
|
1496
|
+
if (last.type === "list") {
|
|
1497
|
+
const item = last.children[last.children.length - 1];
|
|
1498
|
+
return item != null && endsInParagraph(item.children);
|
|
1499
|
+
}
|
|
1500
|
+
return false;
|
|
1501
|
+
}
|
|
1502
|
+
function sourceEndsInParagraph(source, opts) {
|
|
1503
|
+
const record = opts.nestedParses;
|
|
1504
|
+
if (record != null) record.map ??= /* @__PURE__ */ new Map();
|
|
1505
|
+
const known = record?.map?.get(nestedParseKey(opts.blockDepth + 1, source));
|
|
1506
|
+
return endsInParagraph(known ?? parseMarkdownImpl(source, {
|
|
1507
|
+
...nested(opts),
|
|
1508
|
+
probing: true
|
|
1509
|
+
}));
|
|
1510
|
+
}
|
|
1511
|
+
function splitTableRow(line) {
|
|
1512
|
+
let start = 0;
|
|
1513
|
+
let end = line.length;
|
|
1514
|
+
while (start < end && (line[start] === " " || line[start] === " ")) start++;
|
|
1515
|
+
if (line[start] === "|") {
|
|
1516
|
+
start++;
|
|
1517
|
+
while (start < end && line[start] === " ") start++;
|
|
1518
|
+
}
|
|
1519
|
+
while (end > start && line[end - 1] === " ") end--;
|
|
1520
|
+
if (end > start && line[end - 1] === "|") end--;
|
|
1521
|
+
const content = line.slice(start, end);
|
|
1522
|
+
const cells = [];
|
|
1523
|
+
let current = "";
|
|
1524
|
+
for (let i = 0; i < content.length; i++) if (content[i] === "\\" && i + 1 < content.length && content[i + 1] === "|") {
|
|
1525
|
+
current += "\\|";
|
|
1526
|
+
i++;
|
|
1527
|
+
} else if (content[i] === "|") {
|
|
1528
|
+
cells.push(current.trim());
|
|
1529
|
+
current = "";
|
|
1530
|
+
} else current += content[i];
|
|
1531
|
+
cells.push(current.trim());
|
|
1532
|
+
return cells;
|
|
1533
|
+
}
|
|
1534
|
+
function parseTable(lines, lineIndex, opts) {
|
|
1535
|
+
const headers = splitTableRow(lines[lineIndex]).map((cell) => ({
|
|
1536
|
+
type: "tableCell",
|
|
1537
|
+
children: parseInlineEntry(cell, opts, "tableCell")
|
|
1538
|
+
}));
|
|
1539
|
+
const alignments = splitTableRow(lines[lineIndex + 1]).map((cell) => {
|
|
1540
|
+
const trimmed = cell.trim();
|
|
1541
|
+
const leftAligned = trimmed.startsWith(":");
|
|
1542
|
+
const rightAligned = trimmed.endsWith(":");
|
|
1543
|
+
return leftAligned && rightAligned ? "center" : rightAligned ? "right" : leftAligned ? "left" : null;
|
|
1544
|
+
});
|
|
1545
|
+
const rows = [];
|
|
1546
|
+
let rowIndex = lineIndex + 2;
|
|
1547
|
+
while (rowIndex < lines.length && lines[rowIndex].includes("|") && lines[rowIndex].trim() !== "") {
|
|
1548
|
+
rows.push({
|
|
1549
|
+
type: "tableRow",
|
|
1550
|
+
children: splitTableRow(lines[rowIndex]).map((cell) => ({
|
|
1551
|
+
type: "tableCell",
|
|
1552
|
+
children: parseInlineEntry(cell, opts, "tableCell")
|
|
1553
|
+
}))
|
|
1554
|
+
});
|
|
1555
|
+
rowIndex++;
|
|
1556
|
+
}
|
|
1557
|
+
return {
|
|
1558
|
+
node: {
|
|
1559
|
+
type: "table",
|
|
1560
|
+
align: alignments,
|
|
1561
|
+
children: [{
|
|
1562
|
+
type: "tableRow",
|
|
1563
|
+
children: headers
|
|
1564
|
+
}, ...rows]
|
|
1565
|
+
},
|
|
1566
|
+
nextIndex: rowIndex
|
|
1567
|
+
};
|
|
1568
|
+
}
|
|
1569
|
+
function listItemSource(itemText, subLines, lazyLineIndexes) {
|
|
1570
|
+
if (subLines.length === 0) return itemText;
|
|
1571
|
+
const nonBlankIndents = subLines.filter((line, index) => line.trim() !== "" && !lazyLineIndexes.has(index)).map(getIndent);
|
|
1572
|
+
const minSubIndent = nonBlankIndents.length === 0 ? 0 : Math.min(...nonBlankIndents);
|
|
1573
|
+
return `${itemText}\n${subLines.map((line, index) => line.trim() === "" ? "" : line.slice(lazyLineIndexes.has(index) ? getIndent(line) : minSubIndent)).join("\n")}`;
|
|
1574
|
+
}
|
|
1575
|
+
function parseList(lines, startIndex, ordered, opts, interruptsLazyContinuation, documentLineOf) {
|
|
1576
|
+
const items = [];
|
|
1577
|
+
const baseIndent = getIndent(lines[startIndex]);
|
|
1578
|
+
const orderedStart = ordered ? lines[startIndex].match(/^ *(\d+)([.)]) /) : null;
|
|
1579
|
+
const delim = orderedStart ? orderedStart[2] : ".";
|
|
1580
|
+
const escDelim = `\\${delim}`;
|
|
1581
|
+
const bullet = ordered ? null : lines[startIndex].trimStart()[0];
|
|
1582
|
+
const itemPattern = ordered ? new RegExp(`^ {${baseIndent}}\\d+${escDelim} `) : new RegExp(`^ {${baseIndent}}\\${bullet} `);
|
|
1583
|
+
const start = orderedStart ? parseInt(orderedStart[1], 10) : void 0;
|
|
1584
|
+
let loose = false;
|
|
1585
|
+
let index = startIndex;
|
|
1586
|
+
const startsItem = (line) => itemPattern.test(line) && !isHorizontalRule(line);
|
|
1587
|
+
while (index < lines.length && startsItem(lines[index])) {
|
|
1588
|
+
const itemStart = index;
|
|
1589
|
+
const content = ordered ? lines[index].replace(new RegExp(`^ *\\d+${escDelim} `), "") : lines[index].replace(/^ *[-*+] /, "");
|
|
1590
|
+
const markerEnd = lines[index].length - content.length;
|
|
1591
|
+
const spacesAfter = getIndent(content);
|
|
1592
|
+
const contentColumn = spacesAfter >= 4 ? markerEnd : markerEnd + spacesAfter;
|
|
1593
|
+
const taskMatch = content.match(/^\[([ xX])\] (.*)/);
|
|
1594
|
+
let checked;
|
|
1595
|
+
let itemText;
|
|
1596
|
+
if (taskMatch) {
|
|
1597
|
+
checked = taskMatch[1].toLowerCase() === "x";
|
|
1598
|
+
itemText = taskMatch[2];
|
|
1599
|
+
} else itemText = content;
|
|
1600
|
+
index++;
|
|
1601
|
+
const subLines = [];
|
|
1602
|
+
const lazyLineIndexes = /* @__PURE__ */ new Set();
|
|
1603
|
+
let lazyParagraphOpen = canContinueParagraphLazily([itemText], 0, true) ? true : void 0;
|
|
1604
|
+
while (index < lines.length && lines[index].trim() !== "") {
|
|
1605
|
+
if (getIndent(lines[index]) > baseIndent) {
|
|
1606
|
+
subLines.push(lines[index]);
|
|
1607
|
+
if (lazyParagraphOpen === true && !canContinueParagraphLazily([lines[index].trimStart()], 0)) lazyParagraphOpen = void 0;
|
|
1608
|
+
index++;
|
|
1609
|
+
continue;
|
|
1610
|
+
}
|
|
1611
|
+
if (interruptsLazyContinuation(index) || !canContinueParagraphLazily(lines, index, true)) break;
|
|
1612
|
+
if (lazyParagraphOpen === void 0) lazyParagraphOpen = sourceEndsInParagraph(listItemSource(itemText, subLines, lazyLineIndexes), opts);
|
|
1613
|
+
if (!lazyParagraphOpen) break;
|
|
1614
|
+
lazyLineIndexes.add(subLines.length);
|
|
1615
|
+
subLines.push(lines[index]);
|
|
1616
|
+
index++;
|
|
1617
|
+
}
|
|
1618
|
+
for (;;) {
|
|
1619
|
+
let next = index;
|
|
1620
|
+
while (next < lines.length && lines[next].trim() === "") next++;
|
|
1621
|
+
if (next === index || next >= lines.length || getIndent(lines[next]) < contentColumn) break;
|
|
1622
|
+
for (; index < next; index++) subLines.push("");
|
|
1623
|
+
while (index < lines.length && lines[index].trim() !== "" && getIndent(lines[index]) >= contentColumn) {
|
|
1624
|
+
subLines.push(lines[index]);
|
|
1625
|
+
index++;
|
|
1626
|
+
}
|
|
1627
|
+
loose = true;
|
|
1628
|
+
}
|
|
1629
|
+
const source = listItemSource(itemText, subLines, lazyLineIndexes);
|
|
1630
|
+
let itemOrigin = null;
|
|
1631
|
+
if (documentLineOf != null) {
|
|
1632
|
+
itemOrigin = [documentLineOf(itemStart)];
|
|
1633
|
+
for (let j = 0; j < subLines.length; j++) itemOrigin.push(documentLineOf(itemStart + 1 + j));
|
|
1634
|
+
}
|
|
1635
|
+
items.push({
|
|
1636
|
+
type: "listItem",
|
|
1637
|
+
checked,
|
|
1638
|
+
children: parseMarkdownImpl(source, nestedAt(opts, itemOrigin))
|
|
1639
|
+
});
|
|
1640
|
+
let lookahead = index;
|
|
1641
|
+
while (lookahead < lines.length && lines[lookahead].trim() === "") lookahead++;
|
|
1642
|
+
if (lookahead > index && lookahead < lines.length && startsItem(lines[lookahead])) {
|
|
1643
|
+
loose = true;
|
|
1644
|
+
index = lookahead;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
const node = {
|
|
1648
|
+
type: "list",
|
|
1649
|
+
ordered,
|
|
1650
|
+
start,
|
|
1651
|
+
delimiter: ordered ? delim : void 0,
|
|
1652
|
+
spread: loose || void 0,
|
|
1653
|
+
children: items
|
|
1654
|
+
};
|
|
1655
|
+
listMarkerShapes.set(node, `${baseIndent}:${ordered ? delim : bullet}`);
|
|
1656
|
+
return {
|
|
1657
|
+
node,
|
|
1658
|
+
nextIndex: index
|
|
1659
|
+
};
|
|
1660
|
+
}
|
|
1661
|
+
/** Parse block Markdown into a versioned `MdDoc` envelope. */
|
|
1662
|
+
function parseMdDoc(input, options) {
|
|
1663
|
+
const resolved = resolveOptions(options);
|
|
1664
|
+
return applyMarkdownTransforms({
|
|
1665
|
+
kind: "octane-xplat/md",
|
|
1666
|
+
v: 2,
|
|
1667
|
+
children: parseMarkdownImpl(input, resolved)
|
|
1668
|
+
}, resolved.plugins, input, resolved.isFinal, "block");
|
|
1669
|
+
}
|
|
1670
|
+
function parseMarkdownImpl(input, callerOpts) {
|
|
1671
|
+
if (callerOpts.blockDepth >= MAX_BLOCK_NESTING) {
|
|
1672
|
+
const text = input.trim();
|
|
1673
|
+
return text === "" ? [] : [{
|
|
1674
|
+
type: "paragraph",
|
|
1675
|
+
children: parseInlineEntry(text, callerOpts)
|
|
1676
|
+
}];
|
|
1677
|
+
}
|
|
1678
|
+
const baseOpts = callerOpts.nestedParses == null ? {
|
|
1679
|
+
...callerOpts,
|
|
1680
|
+
nestedParses: { map: null }
|
|
1681
|
+
} : callerOpts;
|
|
1682
|
+
const { defs, cleaned, lineMap } = extractLinkDefinitions(input, baseOpts.math);
|
|
1683
|
+
const inherited = baseOpts.linkDefs;
|
|
1684
|
+
let linkDefs;
|
|
1685
|
+
if (defs.size === 0) linkDefs = inherited;
|
|
1686
|
+
else if (inherited == null) linkDefs = defs;
|
|
1687
|
+
else linkDefs = new Map([...defs, ...inherited]);
|
|
1688
|
+
const opts = linkDefs != null ? {
|
|
1689
|
+
...baseOpts,
|
|
1690
|
+
linkDefs
|
|
1691
|
+
} : baseOpts;
|
|
1692
|
+
const lines = cleaned.split("\n");
|
|
1693
|
+
const hasBlockExtensionSyntax = opts.allowBlockSyntax !== false && (opts.plugins?.blockByFirstCharacter.size ?? 0) > 0;
|
|
1694
|
+
const lineOffsets = [0];
|
|
1695
|
+
if (opts.astPositions === true || hasBlockExtensionSyntax) {
|
|
1696
|
+
let offset = 0;
|
|
1697
|
+
for (let index = 0; index < lines.length; index++) {
|
|
1698
|
+
offset += lines[index].length + 1;
|
|
1699
|
+
lineOffsets.push(offset);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
const blocks = [];
|
|
1703
|
+
const documentLineOf = opts.sourceLines !== true || opts.lineOrigin == null && opts.baseLine == null && opts.blockDepth > 0 ? void 0 : opts.sourceLines === true ? (loopLine) => {
|
|
1704
|
+
const inputLine = lineMap != null ? lineMap[loopLine] : loopLine;
|
|
1705
|
+
return opts.lineOrigin != null ? opts.lineOrigin[inputLine] : (opts.baseLine ?? 0) + inputLine;
|
|
1706
|
+
} : void 0;
|
|
1707
|
+
const blockExtensionMatches = /* @__PURE__ */ new Map();
|
|
1708
|
+
const joinedLines = hasBlockExtensionSyntax ? lines.join("\n") : "";
|
|
1709
|
+
const blockExtensionMatchAt = (lineIndex) => {
|
|
1710
|
+
let match = blockExtensionMatches.get(lineIndex);
|
|
1711
|
+
if (match === void 0) {
|
|
1712
|
+
match = matchExtensionSyntax(joinedLines, lineOffsets[lineIndex], "block", opts);
|
|
1713
|
+
blockExtensionMatches.set(lineIndex, match);
|
|
1714
|
+
}
|
|
1715
|
+
return match;
|
|
1716
|
+
};
|
|
1717
|
+
const interruptsWithBlockExtension = (lineIndex) => {
|
|
1718
|
+
if (!hasBlockExtensionSyntax || lineIndex >= lines.length) return false;
|
|
1719
|
+
if (blockExtensionColumn(lines[lineIndex]) == null) return false;
|
|
1720
|
+
const match = blockExtensionMatchAt(lineIndex);
|
|
1721
|
+
return match.status === "match" || match.status === "defer";
|
|
1722
|
+
};
|
|
1723
|
+
const blockStartLines = opts.astPositions === true || documentLineOf != null ? [] : null;
|
|
1724
|
+
const blockEndLines = blockStartLines != null ? [] : null;
|
|
1725
|
+
let blockStartLine = 0;
|
|
1726
|
+
const pushBlock = (node, endLine) => {
|
|
1727
|
+
blocks.push(node);
|
|
1728
|
+
blockStartLines?.push(blockStartLine);
|
|
1729
|
+
blockEndLines?.push(endLine);
|
|
1730
|
+
};
|
|
1731
|
+
let index = 0;
|
|
1732
|
+
while (index < lines.length) {
|
|
1733
|
+
blockStartLine = index;
|
|
1734
|
+
const line = lines[index];
|
|
1735
|
+
if (line.trim() === "") {
|
|
1736
|
+
index++;
|
|
1737
|
+
continue;
|
|
1738
|
+
}
|
|
1739
|
+
const fenceMatch = FENCE_LINE.exec(line);
|
|
1740
|
+
if (fenceMatch) {
|
|
1741
|
+
const [opening, indentation, fence] = fenceMatch;
|
|
1742
|
+
const info = line.slice(opening.length).trim();
|
|
1743
|
+
const language = info.match(/^(\S+)/)?.[1] ?? null;
|
|
1744
|
+
const meta = language == null ? void 0 : info.slice(language.length).trim() || void 0;
|
|
1745
|
+
const codeLines = [];
|
|
1746
|
+
const fenceIndentation = new RegExp(`^ {0,${indentation.length}}`);
|
|
1747
|
+
index++;
|
|
1748
|
+
while (index < lines.length && !closesFence(lines[index], fence)) {
|
|
1749
|
+
codeLines.push(lines[index].replace(fenceIndentation, ""));
|
|
1750
|
+
index++;
|
|
1751
|
+
}
|
|
1752
|
+
const closed = index < lines.length;
|
|
1753
|
+
if (opts.blockDepth === 0 && !closed && codeLines.length > 0 && lines[lines.length - 1] === "") codeLines.pop();
|
|
1754
|
+
index++;
|
|
1755
|
+
pushBlock({
|
|
1756
|
+
type: "code",
|
|
1757
|
+
lang: language,
|
|
1758
|
+
...meta == null ? {} : { meta },
|
|
1759
|
+
value: codeLines.join("\n")
|
|
1760
|
+
}, Math.min(index, lines.length) - 1);
|
|
1761
|
+
continue;
|
|
1762
|
+
}
|
|
1763
|
+
if (opts.math) {
|
|
1764
|
+
const displayMath = matchDisplayMathBlock(lines, index);
|
|
1765
|
+
if (displayMath != null) {
|
|
1766
|
+
pushBlock({
|
|
1767
|
+
type: "math",
|
|
1768
|
+
value: displayMath.value
|
|
1769
|
+
}, displayMath.endLine);
|
|
1770
|
+
index = displayMath.nextIndex;
|
|
1771
|
+
continue;
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
const headingMatch = line.match(/^ {0,3}(#{1,6}) +(.*)/);
|
|
1775
|
+
if (headingMatch) {
|
|
1776
|
+
pushBlock({
|
|
1777
|
+
type: "heading",
|
|
1778
|
+
depth: headingMatch[1].length,
|
|
1779
|
+
children: parseInlineEntry(headingMatch[2], opts)
|
|
1780
|
+
});
|
|
1781
|
+
index++;
|
|
1782
|
+
continue;
|
|
1783
|
+
}
|
|
1784
|
+
if (isHorizontalRule(line)) {
|
|
1785
|
+
pushBlock({ type: "thematicBreak" });
|
|
1786
|
+
index++;
|
|
1787
|
+
continue;
|
|
1788
|
+
}
|
|
1789
|
+
const imageMatch = line.match(/^!\[([^\]]*)\]\(([^)]+)\)/);
|
|
1790
|
+
const imageSrc = imageMatch ? decodeLinkDestination(inlineDestination(imageMatch[2])) : "";
|
|
1791
|
+
if (imageMatch && line.trim() === imageMatch[0] && isSafeMdParserUrl(imageSrc)) {
|
|
1792
|
+
pushBlock({
|
|
1793
|
+
type: "image",
|
|
1794
|
+
alt: imageAlt(imageMatch[1], opts, "default"),
|
|
1795
|
+
url: imageSrc
|
|
1796
|
+
});
|
|
1797
|
+
index++;
|
|
1798
|
+
continue;
|
|
1799
|
+
}
|
|
1800
|
+
if (index + 1 < lines.length && line.includes("|") && isTableSeparator(lines[index + 1])) {
|
|
1801
|
+
const tableResult = parseTable(lines, index, opts);
|
|
1802
|
+
pushBlock(tableResult.node);
|
|
1803
|
+
index = tableResult.nextIndex;
|
|
1804
|
+
continue;
|
|
1805
|
+
}
|
|
1806
|
+
if (QUOTE_MARKER.test(line)) {
|
|
1807
|
+
const quoteLines = [];
|
|
1808
|
+
const quoteOrigin = [];
|
|
1809
|
+
let lazyParagraphOpen;
|
|
1810
|
+
while (index < lines.length) {
|
|
1811
|
+
const quoteLine = lines[index];
|
|
1812
|
+
if (QUOTE_MARKER.test(quoteLine)) {
|
|
1813
|
+
const content = quotedContent(quoteLine);
|
|
1814
|
+
quoteLines.push(content);
|
|
1815
|
+
if (documentLineOf != null) quoteOrigin.push(documentLineOf(index));
|
|
1816
|
+
if (content.trim() === "") lazyParagraphOpen = false;
|
|
1817
|
+
else if (lazyParagraphOpen === false || lazyParagraphOpen === true && !canContinueParagraphLazily([content], 0)) lazyParagraphOpen = void 0;
|
|
1818
|
+
index++;
|
|
1819
|
+
continue;
|
|
1820
|
+
}
|
|
1821
|
+
if (quoteLines[quoteLines.length - 1]?.trim() === "" || interruptsWithBlockExtension(index) || !canContinueParagraphLazily(lines, index)) break;
|
|
1822
|
+
if (lazyParagraphOpen === void 0) lazyParagraphOpen = sourceEndsInParagraph(quoteLines.join("\n"), opts);
|
|
1823
|
+
if (!lazyParagraphOpen) break;
|
|
1824
|
+
quoteLines.push(quoteLine);
|
|
1825
|
+
if (documentLineOf != null) quoteOrigin.push(documentLineOf(index));
|
|
1826
|
+
index++;
|
|
1827
|
+
}
|
|
1828
|
+
pushBlock({
|
|
1829
|
+
type: "blockquote",
|
|
1830
|
+
children: parseMarkdownImpl(quoteLines.join("\n"), nestedAt(opts, documentLineOf != null ? quoteOrigin : null))
|
|
1831
|
+
});
|
|
1832
|
+
continue;
|
|
1833
|
+
}
|
|
1834
|
+
if (/^ {0,9}[-*+] /.test(line)) {
|
|
1835
|
+
const listResult = parseList(lines, index, false, opts, interruptsWithBlockExtension, documentLineOf);
|
|
1836
|
+
pushBlock(listResult.node);
|
|
1837
|
+
index = listResult.nextIndex;
|
|
1838
|
+
continue;
|
|
1839
|
+
}
|
|
1840
|
+
if (/^ {0,9}\d+[.)] /.test(line)) {
|
|
1841
|
+
const listResult = parseList(lines, index, true, opts, interruptsWithBlockExtension, documentLineOf);
|
|
1842
|
+
pushBlock(listResult.node);
|
|
1843
|
+
index = listResult.nextIndex;
|
|
1844
|
+
continue;
|
|
1845
|
+
}
|
|
1846
|
+
if (hasBlockExtensionSyntax && blockExtensionColumn(line) != null) {
|
|
1847
|
+
const extension = blockExtensionMatchAt(index);
|
|
1848
|
+
if (extension.status === "match") {
|
|
1849
|
+
const matchedLines = extension.node.source?.split("\n").length ?? 1;
|
|
1850
|
+
if (lines.slice(index, index + matchedLines).join("\n") !== extension.node.source) reportMarkdownPluginFailure(extension.node.plugin, "syntax", /* @__PURE__ */ new TypeError("Block syntax must consume complete lines"));
|
|
1851
|
+
else {
|
|
1852
|
+
pushBlock(extension.node);
|
|
1853
|
+
index += matchedLines;
|
|
1854
|
+
continue;
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
const paraLines = [line];
|
|
1859
|
+
index++;
|
|
1860
|
+
while (index < lines.length) {
|
|
1861
|
+
const nextLine = lines[index];
|
|
1862
|
+
if (isBlockStart(nextLine) || interruptsWithBlockExtension(index) || opts.math && matchDisplayMathBlock(lines, index) != null || nextLine.trim() === "") break;
|
|
1863
|
+
paraLines.push(nextLine);
|
|
1864
|
+
index++;
|
|
1865
|
+
}
|
|
1866
|
+
pushBlock({
|
|
1867
|
+
type: "paragraph",
|
|
1868
|
+
children: parseInlineEntry(paraLines.join("\n"), opts)
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
if (blockStartLines != null) stampSourceRanges(blocks, blockStartLines, blockEndLines ?? [], lines, lineMap, input, opts, documentLineOf);
|
|
1872
|
+
const nestedParses = baseOpts.nestedParses?.map;
|
|
1873
|
+
if (baseOpts.probing === true && nestedParses != null) nestedParses.set(nestedParseKey(baseOpts.blockDepth, input), blocks);
|
|
1874
|
+
return blocks;
|
|
1875
|
+
}
|
|
1876
|
+
/**
|
|
1877
|
+
* Give each block the offsets it occupies in the original input.
|
|
1878
|
+
*
|
|
1879
|
+
* Blocks are contiguous and in source order, so a block runs from its own
|
|
1880
|
+
* first line to the line before the next block starts, minus the blank lines
|
|
1881
|
+
* between them. Offsets are computed against the *input*, not the text the
|
|
1882
|
+
* block loop saw: link reference definitions are stripped before parsing, and
|
|
1883
|
+
* `lineMap` says which input line each surviving line came from.
|
|
1884
|
+
*/
|
|
1885
|
+
function stampSourceRanges(blocks, blockStartLines, blockEndLines, lines, lineMap, input, opts, documentLineOf) {
|
|
1886
|
+
const withOffsets = opts.astPositions === true;
|
|
1887
|
+
const base = opts.baseOffset ?? 0;
|
|
1888
|
+
const inputLineStarts = [0];
|
|
1889
|
+
if (withOffsets) {
|
|
1890
|
+
for (let i = 0; i < input.length; i++) if (input[i] === "\n") inputLineStarts.push(i + 1);
|
|
1891
|
+
}
|
|
1892
|
+
const lineStart = (line) => base + inputLineStarts[lineMap != null ? lineMap[line] : line];
|
|
1893
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
1894
|
+
const startLine = blockStartLines[i];
|
|
1895
|
+
let endLine = blockEndLines[i];
|
|
1896
|
+
if (endLine == null) {
|
|
1897
|
+
endLine = (i + 1 < blocks.length ? blockStartLines[i + 1] : lines.length) - 1;
|
|
1898
|
+
while (endLine > startLine && lines[endLine].trim() === "") endLine--;
|
|
1899
|
+
}
|
|
1900
|
+
const startDocLine = documentLineOf?.(startLine);
|
|
1901
|
+
const endDocLine = documentLineOf?.(endLine);
|
|
1902
|
+
const position = {
|
|
1903
|
+
start: {
|
|
1904
|
+
...withOffsets ? { offset: lineStart(startLine) } : null,
|
|
1905
|
+
...startDocLine != null ? { line: startDocLine + 1 } : null
|
|
1906
|
+
},
|
|
1907
|
+
end: {
|
|
1908
|
+
...withOffsets ? { offset: lineStart(endLine) + lines[endLine].length } : null,
|
|
1909
|
+
...endDocLine != null ? { line: endDocLine + 1 } : null
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
blocks[i] = withMarkerShapeOf(blocks[i], {
|
|
1913
|
+
...blocks[i],
|
|
1914
|
+
position
|
|
1915
|
+
});
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
/**
|
|
1919
|
+
* Each list the block parser builds, keyed to its markers: their indentation
|
|
1920
|
+
* and the bullet or delimiter. The full parse continues a list only with
|
|
1921
|
+
* items at the same indentation with the same bullet or delimiter.
|
|
1922
|
+
*/
|
|
1923
|
+
var listMarkerShapes = /* @__PURE__ */ new WeakMap();
|
|
1924
|
+
/** `merged`, carrying the marker shape of the list it continues. */
|
|
1925
|
+
function withMarkerShapeOf(list, merged) {
|
|
1926
|
+
const shape = listMarkerShapes.get(list);
|
|
1927
|
+
if (shape != null) listMarkerShapes.set(merged, shape);
|
|
1928
|
+
return merged;
|
|
1929
|
+
}
|
|
1930
|
+
export { parseMdDoc };
|