@bendyline/squisq-editor-react 2.2.0 → 2.3.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/NOTICE.md +30 -30
- package/dist/chunk-54UGTQBO.js +862 -0
- package/dist/chunk-5JMHFAVW.js +2408 -0
- package/dist/chunk-5Q4JN4I5.js +132 -0
- package/dist/chunk-6VDYKI3L.js +49 -0
- package/dist/chunk-GS7QWYFT.js +9 -0
- package/dist/chunk-MJJK7YQB.js +949 -0
- package/dist/chunk-NITZVAXL.js +986 -0
- package/dist/chunk-TRCKHRBS.js +35234 -0
- package/dist/chunk-V44VP242.js +3256 -0
- package/dist/image-editor/index.d.ts +223 -0
- package/dist/image-editor/index.js +15 -0
- package/dist/index.d.ts +2310 -4423
- package/dist/index.js +494 -42158
- package/dist/json-editor/index.d.ts +27 -0
- package/dist/json-editor/index.js +7 -0
- package/dist/monaco.d.ts +1 -1
- package/dist/monaco.js +1 -1
- package/dist/recorder/index.d.ts +407 -0
- package/dist/recorder/index.js +43 -0
- package/dist/shell/index.d.ts +9 -0
- package/dist/shell/index.js +22 -0
- package/dist/shell-BxSCBm4H.d.ts +1064 -0
- package/dist/styles/index.css +367 -19
- package/dist/teleprompter/index.d.ts +497 -0
- package/dist/teleprompter/index.js +57 -0
- package/package.json +29 -6
|
@@ -0,0 +1,986 @@
|
|
|
1
|
+
// src/tiptapBridge.ts
|
|
2
|
+
import { resolveIcon } from "@bendyline/squisq/icons";
|
|
3
|
+
import {
|
|
4
|
+
matchTrailingTemplateAnnotation,
|
|
5
|
+
matchTrailingPandocAttr,
|
|
6
|
+
tokenizeAttrTokens
|
|
7
|
+
} from "@bendyline/squisq/markdown";
|
|
8
|
+
var RE_BOLD_STAR = /\*\*(?![\s*])(.+?)(?<![\s*])\*\*/g;
|
|
9
|
+
var RE_BOLD_UNDER = /(?<![\p{L}\p{N}_])__(?![\s_])(.+?)(?<![\s_])__(?![\p{L}\p{N}_])/gu;
|
|
10
|
+
var RE_ITALIC_STAR = /\*(?![\s*])(.+?)(?<![\s*])\*/g;
|
|
11
|
+
var RE_ITALIC_UNDER = /(?<![\p{L}\p{N}_])_(?![\s_])(.+?)(?<![\s_])_(?![\p{L}\p{N}_])/gu;
|
|
12
|
+
var RE_STRIKETHROUGH = /~~(.+?)~~/g;
|
|
13
|
+
var ESCAPABLE_MD_CHARS = ["*", "_", "~", "\\"];
|
|
14
|
+
var RE_MD_ESCAPE = /\\([*_~\\])/g;
|
|
15
|
+
var RE_INLINE_CODE = /`(.+?)`/g;
|
|
16
|
+
var RE_IMAGE = /!\[(.*?)\]\((.+?)\)/g;
|
|
17
|
+
var RE_MENTION = /@\[([^\]]+?)\]\(([a-z][a-z0-9+.-]*)\\?:([^)\s]+)\)/gi;
|
|
18
|
+
var RE_MENTION_TAG = /<span\b[^>]*?\bdata-mention\b[^>]*?>(?:<[^>]+>)*([^<]*)<\/span>/gi;
|
|
19
|
+
var RE_ICON_MD = /\{\[([a-zA-Z0-9_:-]+)\]\}/g;
|
|
20
|
+
var RE_ICON_TAG = /<i\b[^>]*?\bdata-icon="([^"]*)"[^>]*?><\/i>/gi;
|
|
21
|
+
var RE_STRONG_TAG = /<strong>(.*?)<\/strong>/g;
|
|
22
|
+
var RE_B_TAG = /<b>(.*?)<\/b>/g;
|
|
23
|
+
var RE_EM_TAG = /<em>(.*?)<\/em>/g;
|
|
24
|
+
var RE_I_TAG = /<i>(.*?)<\/i>/g;
|
|
25
|
+
var RE_S_TAG = /<s>(.*?)<\/s>/g;
|
|
26
|
+
var RE_DEL_TAG = /<del>(.*?)<\/del>/g;
|
|
27
|
+
var RE_CODE_TAG = /<code>(.*?)<\/code>/g;
|
|
28
|
+
var RE_A_TAG = /<a\b([^>]*)>(.*?)<\/a>/g;
|
|
29
|
+
var RE_IMG_TAG = /<img\b([^>]*)>/g;
|
|
30
|
+
var RE_STRIP_TAGS = /<[^>]+>/g;
|
|
31
|
+
function markdownToTiptap(markdown) {
|
|
32
|
+
if (!markdown.trim()) return "<p></p>";
|
|
33
|
+
const html = markdown.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
34
|
+
const lines = html.split("\n");
|
|
35
|
+
const frontmatterEnd = findFrontmatterEnd(lines);
|
|
36
|
+
const outputBlocks = [];
|
|
37
|
+
let inCodeBlock = false;
|
|
38
|
+
let codeBlockLang = "";
|
|
39
|
+
let codeBlockLines = [];
|
|
40
|
+
const listStack = [];
|
|
41
|
+
let inTable = false;
|
|
42
|
+
let tableLines = [];
|
|
43
|
+
let pendingBlankLines = 0;
|
|
44
|
+
let lastBlockWasList = false;
|
|
45
|
+
const flushPendingBlankParagraphs = () => {
|
|
46
|
+
if (pendingBlankLines === 0) return;
|
|
47
|
+
const emptyParagraphs = outputBlocks.length === 0 ? pendingBlankLines : Math.max(0, pendingBlankLines - 1);
|
|
48
|
+
for (let i = 0; i < emptyParagraphs; i++) {
|
|
49
|
+
outputBlocks.push("<p></p>");
|
|
50
|
+
}
|
|
51
|
+
pendingBlankLines = 0;
|
|
52
|
+
};
|
|
53
|
+
const pushBlock = (block) => {
|
|
54
|
+
flushPendingBlankParagraphs();
|
|
55
|
+
outputBlocks.push(block);
|
|
56
|
+
lastBlockWasList = false;
|
|
57
|
+
};
|
|
58
|
+
const sealOpenItem = (level) => {
|
|
59
|
+
if (level.openAttrs === null) return;
|
|
60
|
+
level.items.push(`<li${level.openAttrs}>${level.openContent}</li>`);
|
|
61
|
+
level.openAttrs = null;
|
|
62
|
+
level.openContent = "";
|
|
63
|
+
};
|
|
64
|
+
const popListLevel = () => {
|
|
65
|
+
const level = listStack.pop();
|
|
66
|
+
if (!level) return;
|
|
67
|
+
sealOpenItem(level);
|
|
68
|
+
if (level.items.length === 0) return;
|
|
69
|
+
const tag = level.type === "ol" ? "ol" : "ul";
|
|
70
|
+
let attr = level.type === "task" ? ' data-type="taskList"' : "";
|
|
71
|
+
if (level.type === "ol" && level.start != null && level.start !== 1) {
|
|
72
|
+
attr += ` start="${level.start}"`;
|
|
73
|
+
}
|
|
74
|
+
const html2 = `<${tag}${attr}>${level.items.join("")}</${tag}>`;
|
|
75
|
+
const parent = listStack[listStack.length - 1];
|
|
76
|
+
if (!parent) {
|
|
77
|
+
pushBlock(html2);
|
|
78
|
+
lastBlockWasList = true;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (parent.openAttrs === null) {
|
|
82
|
+
parent.openAttrs = parent.type === "task" ? ' data-type="taskItem"' : "";
|
|
83
|
+
parent.openContent = "<p></p>";
|
|
84
|
+
}
|
|
85
|
+
parent.openContent += html2;
|
|
86
|
+
};
|
|
87
|
+
const flushList = () => {
|
|
88
|
+
while (listStack.length > 0) popListLevel();
|
|
89
|
+
};
|
|
90
|
+
const addListItem = (indent, type, attrs, content, start = null) => {
|
|
91
|
+
while (listStack.length > 0 && indent < listStack[listStack.length - 1].minIndent) {
|
|
92
|
+
popListLevel();
|
|
93
|
+
}
|
|
94
|
+
const top = listStack[listStack.length - 1];
|
|
95
|
+
if (!top) {
|
|
96
|
+
listStack.push({
|
|
97
|
+
indent,
|
|
98
|
+
minIndent: 0,
|
|
99
|
+
type,
|
|
100
|
+
start,
|
|
101
|
+
items: [],
|
|
102
|
+
openAttrs: attrs,
|
|
103
|
+
openContent: content
|
|
104
|
+
});
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (indent > top.indent) {
|
|
108
|
+
listStack.push({
|
|
109
|
+
indent,
|
|
110
|
+
minIndent: top.indent + 1,
|
|
111
|
+
type,
|
|
112
|
+
start,
|
|
113
|
+
items: [],
|
|
114
|
+
openAttrs: attrs,
|
|
115
|
+
openContent: content
|
|
116
|
+
});
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (indent < top.indent) top.indent = indent;
|
|
120
|
+
if (top.type !== type) {
|
|
121
|
+
const { indent: depth, minIndent } = top;
|
|
122
|
+
popListLevel();
|
|
123
|
+
listStack.push({
|
|
124
|
+
indent: depth,
|
|
125
|
+
minIndent,
|
|
126
|
+
type,
|
|
127
|
+
start,
|
|
128
|
+
items: [],
|
|
129
|
+
openAttrs: attrs,
|
|
130
|
+
openContent: content
|
|
131
|
+
});
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
sealOpenItem(top);
|
|
135
|
+
top.openAttrs = attrs;
|
|
136
|
+
top.openContent = content;
|
|
137
|
+
};
|
|
138
|
+
const flushTable = () => {
|
|
139
|
+
if (!inTable || tableLines.length === 0) {
|
|
140
|
+
inTable = false;
|
|
141
|
+
tableLines = [];
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const separatorCells = tableLines.length >= 2 ? parseTableCells(tableLines[1]) : [];
|
|
145
|
+
const isSeparator = separatorCells.length > 0 && separatorCells.every((cell) => /^:?-+:?$/.test(cell.trim()));
|
|
146
|
+
if (tableLines.length < 2 || !isSeparator) {
|
|
147
|
+
for (const tl of tableLines) {
|
|
148
|
+
pushBlock(`<p>${inlineToHtml(tl)}</p>`);
|
|
149
|
+
}
|
|
150
|
+
inTable = false;
|
|
151
|
+
tableLines = [];
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const alignments = parseAlignments(tableLines[1]);
|
|
155
|
+
const headerCells = parseTableCells(tableLines[0]);
|
|
156
|
+
const thHtml = headerCells.map((cell, i) => {
|
|
157
|
+
const align = alignments[i];
|
|
158
|
+
const style = align ? ` style="text-align: ${align}"` : "";
|
|
159
|
+
return `<th${style}>${inlineToHtml(cell)}</th>`;
|
|
160
|
+
}).join("");
|
|
161
|
+
const bodyHtml = tableLines.slice(2).map((rowLine) => {
|
|
162
|
+
const cells = parseTableCells(rowLine);
|
|
163
|
+
const tdHtml = cells.map((cell, i) => {
|
|
164
|
+
const align = alignments[i];
|
|
165
|
+
const style = align ? ` style="text-align: ${align}"` : "";
|
|
166
|
+
return `<td${style}>${inlineToHtml(cell)}</td>`;
|
|
167
|
+
}).join("");
|
|
168
|
+
return `<tr>${tdHtml}</tr>`;
|
|
169
|
+
}).join("");
|
|
170
|
+
pushBlock(`<table><thead><tr>${thHtml}</tr></thead><tbody>${bodyHtml}</tbody></table>`);
|
|
171
|
+
inTable = false;
|
|
172
|
+
tableLines = [];
|
|
173
|
+
};
|
|
174
|
+
for (let i = 0; i < lines.length; i++) {
|
|
175
|
+
const line = lines[i];
|
|
176
|
+
if (line.startsWith("```")) {
|
|
177
|
+
if (!inCodeBlock) {
|
|
178
|
+
flushList();
|
|
179
|
+
flushTable();
|
|
180
|
+
flushPendingBlankParagraphs();
|
|
181
|
+
inCodeBlock = true;
|
|
182
|
+
codeBlockLang = line.slice(3).trim();
|
|
183
|
+
codeBlockLines = [];
|
|
184
|
+
continue;
|
|
185
|
+
} else {
|
|
186
|
+
const langAttr = codeBlockLang ? ` class="language-${escapeHtml(codeBlockLang)}"` : "";
|
|
187
|
+
pushBlock(`<pre><code${langAttr}>${escapeHtml(codeBlockLines.join("\n"))}</code></pre>`);
|
|
188
|
+
inCodeBlock = false;
|
|
189
|
+
codeBlockLang = "";
|
|
190
|
+
codeBlockLines = [];
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (inCodeBlock) {
|
|
195
|
+
codeBlockLines.push(line);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (inTable && !/^\|.*\|$/.test(line.trim())) {
|
|
199
|
+
flushTable();
|
|
200
|
+
}
|
|
201
|
+
if (line.trim() === "") {
|
|
202
|
+
flushList();
|
|
203
|
+
pendingBlankLines++;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const precededByBlank = pendingBlankLines > 0 || outputBlocks.length === 0;
|
|
207
|
+
if (precededByBlank && !inTable && listStack.length === 0 && !lastBlockWasList && isIndentedCodeLine(line)) {
|
|
208
|
+
const codeLines = [];
|
|
209
|
+
let scan = i;
|
|
210
|
+
for (; scan < lines.length; scan++) {
|
|
211
|
+
const scanned = lines[scan];
|
|
212
|
+
if (scanned.trim() === "") {
|
|
213
|
+
codeLines.push("");
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (!isIndentedCodeLine(scanned)) break;
|
|
217
|
+
codeLines.push(stripCodeIndent(scanned));
|
|
218
|
+
}
|
|
219
|
+
let end = codeLines.length;
|
|
220
|
+
while (end > 0 && codeLines[end - 1] === "") end--;
|
|
221
|
+
const trailingBlanks = codeLines.length - end;
|
|
222
|
+
codeLines.length = end;
|
|
223
|
+
i = scan - trailingBlanks - 1;
|
|
224
|
+
pushBlock(`<pre><code>${escapeHtml(codeLines.join("\n"))}</code></pre>`);
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
flushPendingBlankParagraphs();
|
|
228
|
+
const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
|
|
229
|
+
if (headingMatch) {
|
|
230
|
+
flushList();
|
|
231
|
+
pushBlock(headingToHtml(headingMatch[1].length, headingMatch[2]));
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (/^(---|\*\*\*|___)(\s*)$/.test(line.trim())) {
|
|
235
|
+
flushList();
|
|
236
|
+
pushBlock("<hr>");
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (line.startsWith("> ")) {
|
|
240
|
+
flushList();
|
|
241
|
+
const quoteLines = [line.slice(2)];
|
|
242
|
+
while (i + 1 < lines.length && lines[i + 1].startsWith("> ")) {
|
|
243
|
+
i++;
|
|
244
|
+
quoteLines.push(lines[i].slice(2));
|
|
245
|
+
}
|
|
246
|
+
pushBlock(
|
|
247
|
+
`<blockquote>${quoteLines.map((quoteLine) => `<p>${inlineToHtml(quoteLine)}</p>`).join("")}</blockquote>`
|
|
248
|
+
);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
const taskMatch = line.match(/^([ \t]*)[-*+]\s+\[([xX ])\]\s*(.*)$/);
|
|
252
|
+
if (taskMatch) {
|
|
253
|
+
const checkedAttr = taskMatch[2].toLowerCase() === "x" ? ' data-checked="true"' : "";
|
|
254
|
+
addListItem(
|
|
255
|
+
indentWidth(taskMatch[1]),
|
|
256
|
+
"task",
|
|
257
|
+
` data-type="taskItem"${checkedAttr}`,
|
|
258
|
+
`<p>${inlineToHtml(taskMatch[3])}</p>`
|
|
259
|
+
);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const ulMatch = line.match(/^([ \t]*)[-*+]\s+(.+)$/);
|
|
263
|
+
if (ulMatch) {
|
|
264
|
+
addListItem(indentWidth(ulMatch[1]), "ul", "", `<p>${inlineToHtml(ulMatch[2])}</p>`);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const olMatch = line.match(/^([ \t]*)(\d+)\.\s+(.+)$/);
|
|
268
|
+
if (olMatch) {
|
|
269
|
+
addListItem(
|
|
270
|
+
indentWidth(olMatch[1]),
|
|
271
|
+
"ol",
|
|
272
|
+
"",
|
|
273
|
+
`<p>${inlineToHtml(olMatch[3])}</p>`,
|
|
274
|
+
parseInt(olMatch[2], 10)
|
|
275
|
+
);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (/^\|.*\|$/.test(line.trim())) {
|
|
279
|
+
if (!inTable) {
|
|
280
|
+
flushList();
|
|
281
|
+
inTable = true;
|
|
282
|
+
tableLines = [];
|
|
283
|
+
}
|
|
284
|
+
tableLines.push(line);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
const standaloneImageMatch = line.trim().match(/^!\[(.*?)\]\((.+?)\)$/);
|
|
288
|
+
if (standaloneImageMatch) {
|
|
289
|
+
flushList();
|
|
290
|
+
const alt = escapeHtml(standaloneImageMatch[1] ?? "");
|
|
291
|
+
const src = escapeHtml(standaloneImageMatch[2] ?? "");
|
|
292
|
+
pushBlock(`<img alt="${alt}" src="${src}">`);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const trimmed = line.trim();
|
|
296
|
+
if (/^<img\b[^>]*>$/i.test(trimmed)) {
|
|
297
|
+
flushList();
|
|
298
|
+
pushBlock(trimmed);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
if (/^<(?:video|audio)\b[^>]*>(?:[\s\S]*?<\/(?:video|audio)>)?$/i.test(trimmed)) {
|
|
302
|
+
flushList();
|
|
303
|
+
pushBlock(trimmed);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const setextLevel = i + 1 < lines.length && i + 1 > frontmatterEnd ? matchSetextUnderline(lines[i + 1]) : null;
|
|
307
|
+
if (setextLevel !== null) {
|
|
308
|
+
flushList();
|
|
309
|
+
i++;
|
|
310
|
+
pushBlock(headingToHtml(setextLevel, line.trim()));
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
flushList();
|
|
314
|
+
pushBlock(`<p>${inlineToHtml(line)}</p>`);
|
|
315
|
+
}
|
|
316
|
+
if (inCodeBlock) {
|
|
317
|
+
const langAttr = codeBlockLang ? ` class="language-${escapeHtml(codeBlockLang)}"` : "";
|
|
318
|
+
pushBlock(`<pre><code${langAttr}>${escapeHtml(codeBlockLines.join("\n"))}</code></pre>`);
|
|
319
|
+
}
|
|
320
|
+
flushList();
|
|
321
|
+
flushTable();
|
|
322
|
+
flushPendingBlankParagraphs();
|
|
323
|
+
return outputBlocks.join("") || "<p></p>";
|
|
324
|
+
}
|
|
325
|
+
function tiptapToMarkdown(html) {
|
|
326
|
+
if (!html || html === "<p></p>") return "";
|
|
327
|
+
const lines = [];
|
|
328
|
+
let remaining = html;
|
|
329
|
+
while (remaining.length > 0) {
|
|
330
|
+
const headingMatch = remaining.match(/^<h([1-6])([^>]*)>(.*?)<\/h\1>/s);
|
|
331
|
+
if (headingMatch) {
|
|
332
|
+
const level = parseInt(headingMatch[1], 10);
|
|
333
|
+
const attrs = headingMatch[2];
|
|
334
|
+
const headingHtml = headingMatch[3];
|
|
335
|
+
const chromeStart = headingHtml.search(
|
|
336
|
+
/<span\b[^>]*\bclass="[^"]*\bsquisq-(?:template|props)-badge\b[^"]*"[^>]*>/i
|
|
337
|
+
);
|
|
338
|
+
let text = htmlToInline(chromeStart >= 0 ? headingHtml.slice(0, chromeStart) : headingHtml);
|
|
339
|
+
const blockAttrsMatch = attrs.match(/data-block-attrs="([^"]*)"/);
|
|
340
|
+
const tmplMatch = attrs.match(/data-template="([^"]+)"/);
|
|
341
|
+
const paramsMatch = attrs.match(/data-template-params="([^"]+)"/);
|
|
342
|
+
const hasEmptyTemplateAnnotation = /\sdata-template-empty(?:="[^"]*")?/.test(attrs);
|
|
343
|
+
if (blockAttrsMatch) {
|
|
344
|
+
const inner = unescapeHtml(blockAttrsMatch[1]);
|
|
345
|
+
text += ` {${inner}}`;
|
|
346
|
+
}
|
|
347
|
+
if (tmplMatch || paramsMatch) {
|
|
348
|
+
let annotation = tmplMatch ? tmplMatch[1] : "";
|
|
349
|
+
if (paramsMatch) {
|
|
350
|
+
annotation += (annotation ? " " : "") + unescapeHtml(paramsMatch[1]);
|
|
351
|
+
}
|
|
352
|
+
text += ` {[${annotation}]}`;
|
|
353
|
+
} else if (hasEmptyTemplateAnnotation) {
|
|
354
|
+
text += " {[]}";
|
|
355
|
+
}
|
|
356
|
+
lines.push("#".repeat(level) + " " + text);
|
|
357
|
+
lines.push("");
|
|
358
|
+
remaining = remaining.slice(headingMatch[0].length);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const codeMatch = remaining.match(/^<pre\b[^>]*><code\b([^>]*)>(.*?)<\/code><\/pre>/s);
|
|
362
|
+
if (codeMatch) {
|
|
363
|
+
const lang = /\bclass="language-([^"]*)"/.exec(codeMatch[1] ?? "")?.[1] ?? "";
|
|
364
|
+
const code = unescapeHtml(codeMatch[2]);
|
|
365
|
+
lines.push("```" + lang);
|
|
366
|
+
lines.push(code);
|
|
367
|
+
lines.push("```");
|
|
368
|
+
lines.push("");
|
|
369
|
+
remaining = remaining.slice(codeMatch[0].length);
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const bqMatch = remaining.match(/^<blockquote>(.*?)<\/blockquote>/s);
|
|
373
|
+
if (bqMatch) {
|
|
374
|
+
const paragraphs = bqMatch[1].split(/<\/p>\s*<p[^>]*>/i).map((paragraph) => paragraph.replace(/^<p[^>]*>/i, "").replace(/<\/p>\s*$/i, ""));
|
|
375
|
+
for (const paragraph of paragraphs) {
|
|
376
|
+
for (const quoteLine of htmlToInline(paragraph).split("\n")) {
|
|
377
|
+
lines.push("> " + quoteLine);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const next = remaining.slice(bqMatch[0].length);
|
|
381
|
+
if (!/^\s*<blockquote>/.test(next)) lines.push("");
|
|
382
|
+
remaining = next;
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
if (remaining.startsWith("<hr>") || remaining.startsWith("<hr/>") || remaining.startsWith("<hr />")) {
|
|
386
|
+
const hrMatch = remaining.match(/^<hr\s*\/?>/);
|
|
387
|
+
lines.push("---");
|
|
388
|
+
lines.push("");
|
|
389
|
+
remaining = remaining.slice(hrMatch[0].length);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
const tableMatch = remaining.match(
|
|
393
|
+
/^<div[^>]*class="[^"]*tableWrapper[^"]*"[^>]*><table[^>]*>(.*?)<\/table>\s*<\/div>/s
|
|
394
|
+
) || remaining.match(/^<table[^>]*>(.*?)<\/table>/s);
|
|
395
|
+
if (tableMatch) {
|
|
396
|
+
const tableContent = tableMatch[1];
|
|
397
|
+
const rows = [];
|
|
398
|
+
const rowRegex = /<tr[^>]*>(.*?)<\/tr>/gs;
|
|
399
|
+
let rowExec;
|
|
400
|
+
while ((rowExec = rowRegex.exec(tableContent)) !== null) {
|
|
401
|
+
const rowHtml = rowExec[1];
|
|
402
|
+
const cells = [];
|
|
403
|
+
const cellRegex = /<(th|td)([^>]*)>(.*?)<\/\1>/gs;
|
|
404
|
+
let cellExec;
|
|
405
|
+
while ((cellExec = cellRegex.exec(rowHtml)) !== null) {
|
|
406
|
+
const tag = cellExec[1];
|
|
407
|
+
const attrs = cellExec[2];
|
|
408
|
+
const content = htmlToInline(cellExec[3].replace(/<\/?p>/g, ""));
|
|
409
|
+
const alignExec = attrs.match(/text-align:\s*(left|center|right)/);
|
|
410
|
+
cells.push({
|
|
411
|
+
content,
|
|
412
|
+
align: alignExec ? alignExec[1] : null,
|
|
413
|
+
isHeader: tag === "th"
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
if (cells.length > 0) {
|
|
417
|
+
rows.push(cells);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (rows.length > 0) {
|
|
421
|
+
const headerIdx = rows.findIndex((r) => r.some((c) => c.isHeader));
|
|
422
|
+
const hIdx = headerIdx >= 0 ? headerIdx : 0;
|
|
423
|
+
const headerRow = rows[hIdx];
|
|
424
|
+
const dataRows = rows.filter((_, i) => i !== hIdx);
|
|
425
|
+
const aligns = headerRow.map((c) => c.align);
|
|
426
|
+
lines.push("| " + headerRow.map((c) => c.content || " ").join(" | ") + " |");
|
|
427
|
+
lines.push(
|
|
428
|
+
"| " + aligns.map((a) => {
|
|
429
|
+
if (a === "center") return ":---:";
|
|
430
|
+
if (a === "right") return "---:";
|
|
431
|
+
return "---";
|
|
432
|
+
}).join(" | ") + " |"
|
|
433
|
+
);
|
|
434
|
+
for (const row of dataRows) {
|
|
435
|
+
lines.push("| " + row.map((c) => c.content || " ").join(" | ") + " |");
|
|
436
|
+
}
|
|
437
|
+
lines.push("");
|
|
438
|
+
}
|
|
439
|
+
remaining = remaining.slice(tableMatch[0].length);
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const listOpenMatch = remaining.match(/^<(ul|ol)\b([^>]*)>/i);
|
|
443
|
+
if (listOpenMatch) {
|
|
444
|
+
const tag = listOpenMatch[1].toLowerCase();
|
|
445
|
+
const matched = matchBalancedTag(remaining, 0, tag);
|
|
446
|
+
if (matched) {
|
|
447
|
+
const isTask = /data-type="taskList"/i.test(listOpenMatch[2] ?? "");
|
|
448
|
+
lines.push(
|
|
449
|
+
...renderList(matched.inner, tag === "ol", isTask, "", readListStart(listOpenMatch[2]))
|
|
450
|
+
);
|
|
451
|
+
lines.push("");
|
|
452
|
+
remaining = remaining.slice(matched.end);
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const pMatch = remaining.match(/^<p>(.*?)<\/p>/s);
|
|
457
|
+
if (pMatch) {
|
|
458
|
+
const text = htmlToInline(pMatch[1]);
|
|
459
|
+
if (text.trim()) {
|
|
460
|
+
lines.push(text);
|
|
461
|
+
lines.push("");
|
|
462
|
+
} else {
|
|
463
|
+
lines.push("");
|
|
464
|
+
}
|
|
465
|
+
remaining = remaining.slice(pMatch[0].length);
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
const imgMatch = remaining.match(/^<img\b([^>]*)>/);
|
|
469
|
+
if (imgMatch) {
|
|
470
|
+
const attrs = imgMatch[1] ?? "";
|
|
471
|
+
const src = /\bsrc="([^"]*)"/i.exec(attrs)?.[1];
|
|
472
|
+
if (src) {
|
|
473
|
+
const alt = /\balt="([^"]*)"/i.exec(attrs)?.[1] ?? "";
|
|
474
|
+
lines.push(serializeImage(src, alt, attrs));
|
|
475
|
+
lines.push("");
|
|
476
|
+
}
|
|
477
|
+
remaining = remaining.slice(imgMatch[0].length);
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const mediaMatch = remaining.match(/^<(video|audio)\b([^>]*)>(?:[\s\S]*?<\/\1>)?/);
|
|
481
|
+
if (mediaMatch) {
|
|
482
|
+
const tag = mediaMatch[1] === "video" ? "video" : "audio";
|
|
483
|
+
lines.push(serializeMediaTag(tag, mediaMatch[2] ?? ""));
|
|
484
|
+
lines.push("");
|
|
485
|
+
remaining = remaining.slice(mediaMatch[0].length);
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
const skipMatch = remaining.match(/^(<[^>]+>|\s+)/);
|
|
489
|
+
if (skipMatch) {
|
|
490
|
+
remaining = remaining.slice(skipMatch[0].length);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const textMatch = remaining.match(/^([^<]+)/);
|
|
494
|
+
if (textMatch) {
|
|
495
|
+
lines.push(unescapeHtml(textMatch[1]));
|
|
496
|
+
remaining = remaining.slice(textMatch[0].length);
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
remaining = remaining.slice(1);
|
|
500
|
+
}
|
|
501
|
+
return lines.join("\n");
|
|
502
|
+
}
|
|
503
|
+
function matchBalancedTag(html, start, tag) {
|
|
504
|
+
const re = new RegExp(`<${tag}\\b[^>]*>|</${tag}\\s*>`, "gi");
|
|
505
|
+
re.lastIndex = start;
|
|
506
|
+
let depth = 0;
|
|
507
|
+
let innerStart = -1;
|
|
508
|
+
let match;
|
|
509
|
+
while ((match = re.exec(html)) !== null) {
|
|
510
|
+
if (match[0][1] === "/") {
|
|
511
|
+
depth--;
|
|
512
|
+
if (depth === 0)
|
|
513
|
+
return { inner: html.slice(innerStart, match.index), end: match.index + match[0].length };
|
|
514
|
+
if (depth < 0) return null;
|
|
515
|
+
} else {
|
|
516
|
+
if (depth === 0) {
|
|
517
|
+
if (match.index !== start) return null;
|
|
518
|
+
innerStart = match.index + match[0].length;
|
|
519
|
+
}
|
|
520
|
+
depth++;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
function splitTopLevelListItems(inner) {
|
|
526
|
+
const items = [];
|
|
527
|
+
const re = /<li\b([^>]*)>|<\/li\s*>/gi;
|
|
528
|
+
let depth = 0;
|
|
529
|
+
let itemStart = -1;
|
|
530
|
+
let itemAttrs = "";
|
|
531
|
+
let match;
|
|
532
|
+
while ((match = re.exec(inner)) !== null) {
|
|
533
|
+
if (match[0][1] === "/") {
|
|
534
|
+
depth--;
|
|
535
|
+
if (depth === 0 && itemStart >= 0) {
|
|
536
|
+
items.push({ attrs: itemAttrs, inner: inner.slice(itemStart, match.index) });
|
|
537
|
+
}
|
|
538
|
+
if (depth < 0) depth = 0;
|
|
539
|
+
} else {
|
|
540
|
+
if (depth === 0) {
|
|
541
|
+
itemStart = match.index + match[0].length;
|
|
542
|
+
itemAttrs = match[1] ?? "";
|
|
543
|
+
}
|
|
544
|
+
depth++;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return items;
|
|
548
|
+
}
|
|
549
|
+
function readListStart(attrs) {
|
|
550
|
+
const parsed = parseInt(/\bstart="(\d+)"/i.exec(attrs ?? "")?.[1] ?? "", 10);
|
|
551
|
+
return Number.isFinite(parsed) ? parsed : 1;
|
|
552
|
+
}
|
|
553
|
+
function splitItemContent(itemHtml) {
|
|
554
|
+
const nested = [];
|
|
555
|
+
let content = "";
|
|
556
|
+
let cursor = 0;
|
|
557
|
+
const re = /<(ul|ol)\b([^>]*)>/gi;
|
|
558
|
+
let match;
|
|
559
|
+
while ((match = re.exec(itemHtml)) !== null) {
|
|
560
|
+
const tag = match[1].toLowerCase();
|
|
561
|
+
const matched = matchBalancedTag(itemHtml, match.index, tag);
|
|
562
|
+
if (!matched) break;
|
|
563
|
+
content += itemHtml.slice(cursor, match.index);
|
|
564
|
+
nested.push({
|
|
565
|
+
ordered: tag === "ol",
|
|
566
|
+
task: /data-type="taskList"/i.test(match[2] ?? ""),
|
|
567
|
+
inner: matched.inner,
|
|
568
|
+
start: readListStart(match[2])
|
|
569
|
+
});
|
|
570
|
+
cursor = matched.end;
|
|
571
|
+
re.lastIndex = matched.end;
|
|
572
|
+
}
|
|
573
|
+
return { content: content + itemHtml.slice(cursor), nested };
|
|
574
|
+
}
|
|
575
|
+
function renderList(inner, ordered, task, indent, start = 1) {
|
|
576
|
+
const lines = [];
|
|
577
|
+
const items = splitTopLevelListItems(inner);
|
|
578
|
+
items.forEach((item, idx) => {
|
|
579
|
+
const { content, nested } = splitItemContent(item.inner);
|
|
580
|
+
const marker = ordered ? `${start + idx}. ` : "- ";
|
|
581
|
+
if (task) {
|
|
582
|
+
const checked = /data-checked="true"/.test(item.attrs);
|
|
583
|
+
const body = content.replace(/<label\b[^>]*>.*?<\/label>/s, "");
|
|
584
|
+
const text = htmlToInline(body.replace(/<[^>]+>/g, "").trim());
|
|
585
|
+
lines.push(`${indent}- [${checked ? "x" : " "}] ${text}`.trimEnd());
|
|
586
|
+
} else {
|
|
587
|
+
lines.push(...renderListItem(marker, content, indent));
|
|
588
|
+
}
|
|
589
|
+
const childIndent = indent + " ".repeat(task ? 2 : marker.length);
|
|
590
|
+
for (const child of nested) {
|
|
591
|
+
lines.push(...renderList(child.inner, child.ordered, child.task, childIndent, child.start));
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
return lines;
|
|
595
|
+
}
|
|
596
|
+
function renderListItem(prefix, html, outerIndent = "") {
|
|
597
|
+
const indent = outerIndent + " ".repeat(prefix.length);
|
|
598
|
+
const media = [];
|
|
599
|
+
const htmlWithoutMedia = html.replace(
|
|
600
|
+
/<(video|audio)\b([^>]*)>(?:[\s\S]*?<\/\1>)?/gi,
|
|
601
|
+
(_full, tag, attrs) => {
|
|
602
|
+
media.push(serializeMediaTag(tag.toLowerCase() === "video" ? "video" : "audio", attrs ?? ""));
|
|
603
|
+
return "";
|
|
604
|
+
}
|
|
605
|
+
);
|
|
606
|
+
const paragraphs = htmlWithoutMedia.split(/<\/p>\s*<p[^>]*>/i).map((p) => p.replace(/^<p[^>]*>/i, "").replace(/<\/p>\s*$/i, ""));
|
|
607
|
+
const textLines = [];
|
|
608
|
+
paragraphs.forEach((paragraph, pIdx) => {
|
|
609
|
+
const inline = htmlToInline(paragraph).trim();
|
|
610
|
+
if (!inline) return;
|
|
611
|
+
const subLines = inline.split("\n");
|
|
612
|
+
subLines.forEach((sub, sIdx) => {
|
|
613
|
+
if (pIdx === 0 && sIdx === 0) {
|
|
614
|
+
textLines.push(outerIndent + prefix + sub);
|
|
615
|
+
} else {
|
|
616
|
+
if (sIdx === 0) textLines.push("");
|
|
617
|
+
textLines.push(indent + sub);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
if (textLines.length === 0 && media.length === 0) return [outerIndent + prefix];
|
|
622
|
+
const result = [];
|
|
623
|
+
if (textLines.length > 0) {
|
|
624
|
+
result.push(...textLines);
|
|
625
|
+
for (const tag of media) {
|
|
626
|
+
result.push("");
|
|
627
|
+
result.push(indent + tag);
|
|
628
|
+
}
|
|
629
|
+
} else {
|
|
630
|
+
result.push(outerIndent + prefix + media[0]);
|
|
631
|
+
for (const tag of media.slice(1)) {
|
|
632
|
+
result.push("");
|
|
633
|
+
result.push(indent + tag);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return result;
|
|
637
|
+
}
|
|
638
|
+
function findFrontmatterEnd(lines) {
|
|
639
|
+
if (lines[0]?.trim() !== "---") return -1;
|
|
640
|
+
for (let i = 1; i < lines.length; i++) {
|
|
641
|
+
if (lines[i].trim() === "---") return i;
|
|
642
|
+
}
|
|
643
|
+
return -1;
|
|
644
|
+
}
|
|
645
|
+
function matchSetextUnderline(line) {
|
|
646
|
+
const match = /^ {0,3}(=+|-+)[ \t]*$/.exec(line);
|
|
647
|
+
if (!match) return null;
|
|
648
|
+
return match[1][0] === "=" ? 1 : 2;
|
|
649
|
+
}
|
|
650
|
+
function isIndentedCodeLine(line) {
|
|
651
|
+
return /^(?: {4}|\t)/.test(line) && line.trim() !== "";
|
|
652
|
+
}
|
|
653
|
+
function stripCodeIndent(line) {
|
|
654
|
+
return line.startsWith(" ") ? line.slice(1) : line.slice(4);
|
|
655
|
+
}
|
|
656
|
+
function headingToHtml(level, rawText) {
|
|
657
|
+
let text = rawText;
|
|
658
|
+
let attrs = "";
|
|
659
|
+
let templateInner = null;
|
|
660
|
+
let pandocInner = null;
|
|
661
|
+
for (let pass = 0; pass < 4; pass++) {
|
|
662
|
+
let matched = false;
|
|
663
|
+
if (templateInner == null) {
|
|
664
|
+
const m = matchTrailingTemplateAnnotation(text);
|
|
665
|
+
if (m) {
|
|
666
|
+
templateInner = m.inner.trim();
|
|
667
|
+
text = text.slice(0, m.index).trimEnd();
|
|
668
|
+
matched = true;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (pandocInner == null) {
|
|
672
|
+
const m = matchTrailingPandocAttr(text);
|
|
673
|
+
if (m) {
|
|
674
|
+
pandocInner = m.inner.trim();
|
|
675
|
+
text = text.slice(0, m.index).trimEnd();
|
|
676
|
+
matched = true;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
if (!matched) break;
|
|
680
|
+
}
|
|
681
|
+
if (templateInner != null) {
|
|
682
|
+
const tokens = tokenizeAttrTokens(templateInner);
|
|
683
|
+
if (tokens.length === 0) {
|
|
684
|
+
attrs += ' data-template-empty="true"';
|
|
685
|
+
}
|
|
686
|
+
const firstIsParam = tokens.length > 0 && tokens[0].indexOf("=") > 0;
|
|
687
|
+
if (!firstIsParam && tokens[0]) {
|
|
688
|
+
attrs += ` data-template="${escapeHtml(tokens[0])}"`;
|
|
689
|
+
}
|
|
690
|
+
const params = tokens.slice(firstIsParam ? 0 : 1).filter((t) => t.includes("="));
|
|
691
|
+
if (params.length > 0) {
|
|
692
|
+
attrs += ` data-template-params="${escapeHtml(params.join(" "))}"`;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
if (pandocInner != null) {
|
|
696
|
+
attrs += ` data-block-attrs="${escapeHtml(pandocInner)}"`;
|
|
697
|
+
}
|
|
698
|
+
return `<h${level}${attrs}>${inlineToHtml(text)}</h${level}>`;
|
|
699
|
+
}
|
|
700
|
+
function indentWidth(raw) {
|
|
701
|
+
let width = 0;
|
|
702
|
+
for (const ch of raw) width += ch === " " ? 4 : 1;
|
|
703
|
+
return width;
|
|
704
|
+
}
|
|
705
|
+
function parseTableCells(line) {
|
|
706
|
+
let inner = line.trim();
|
|
707
|
+
if (inner.startsWith("|")) inner = inner.slice(1);
|
|
708
|
+
if (inner.endsWith("|")) inner = inner.slice(0, -1);
|
|
709
|
+
return inner.split("|").map((cell) => cell.trim());
|
|
710
|
+
}
|
|
711
|
+
function parseAlignments(separatorLine) {
|
|
712
|
+
return parseTableCells(separatorLine).map((cell) => {
|
|
713
|
+
const s = cell.replace(/\s/g, "");
|
|
714
|
+
if (s.startsWith(":") && s.endsWith(":")) return "center";
|
|
715
|
+
if (s.endsWith(":")) return "right";
|
|
716
|
+
if (s.startsWith(":")) return "left";
|
|
717
|
+
return null;
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
function serializeMediaTag(tag, attrs) {
|
|
721
|
+
const src = /\bsrc="([^"]*)"/i.exec(attrs)?.[1] ?? "";
|
|
722
|
+
const controls = /\bcontrols\b/i.test(attrs);
|
|
723
|
+
const width = /\bwidth="([^"]*)"/i.exec(attrs)?.[1];
|
|
724
|
+
const height = /\bheight="([^"]*)"/i.exec(attrs)?.[1];
|
|
725
|
+
const poster = tag === "video" ? /\bposter="([^"]*)"/i.exec(attrs)?.[1] : void 0;
|
|
726
|
+
const parts = [`<${tag} src="${src}"`];
|
|
727
|
+
if (controls) parts.push(" controls");
|
|
728
|
+
if (width) parts.push(` width="${width}"`);
|
|
729
|
+
if (height) parts.push(` height="${height}"`);
|
|
730
|
+
if (poster) parts.push(` poster="${poster}"`);
|
|
731
|
+
parts.push(`></${tag}>`);
|
|
732
|
+
return parts.join("");
|
|
733
|
+
}
|
|
734
|
+
function serializeImage(src, alt, attrs) {
|
|
735
|
+
const width = /\bwidth="([^"]*)"/i.exec(attrs)?.[1];
|
|
736
|
+
const height = /\bheight="([^"]*)"/i.exec(attrs)?.[1];
|
|
737
|
+
const title = /\btitle="([^"]*)"/i.exec(attrs)?.[1];
|
|
738
|
+
if (!width && !height) {
|
|
739
|
+
return ``;
|
|
740
|
+
}
|
|
741
|
+
const parts = [`<img alt="${alt}" src="${src}"`];
|
|
742
|
+
if (width) parts.push(` width="${width}"`);
|
|
743
|
+
if (height) parts.push(` height="${height}"`);
|
|
744
|
+
if (title) parts.push(` title="${title}"`);
|
|
745
|
+
parts.push(">");
|
|
746
|
+
return parts.join("");
|
|
747
|
+
}
|
|
748
|
+
function escapeHtml(text) {
|
|
749
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
750
|
+
}
|
|
751
|
+
function unescapeHtml(text) {
|
|
752
|
+
return text.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, '"').replace(/ | | /gi, "\xA0").replace(/&/g, "&");
|
|
753
|
+
}
|
|
754
|
+
function inlineToHtml(text) {
|
|
755
|
+
const placeholders = [];
|
|
756
|
+
const stash = (html) => {
|
|
757
|
+
const token = `\0PH${placeholders.length}\0`;
|
|
758
|
+
placeholders.push(html);
|
|
759
|
+
return token;
|
|
760
|
+
};
|
|
761
|
+
let staged = text.replace(
|
|
762
|
+
RE_INLINE_CODE,
|
|
763
|
+
(_m, code) => stash(`<code>${escapeHtml(code)}</code>`)
|
|
764
|
+
);
|
|
765
|
+
staged = staged.replace(
|
|
766
|
+
RE_IMAGE,
|
|
767
|
+
(_m, alt, src) => stash(`<img alt="${escapeHtml(alt)}" src="${escapeHtml(src)}">`)
|
|
768
|
+
);
|
|
769
|
+
staged = staged.replace(
|
|
770
|
+
RE_MENTION,
|
|
771
|
+
(_m, label, kind, id) => stash(
|
|
772
|
+
`<span data-mention="true" data-kind="${escapeHtml(kind)}" data-id="${escapeHtml(id)}" data-label="${escapeHtml(label)}" class="mention">@${escapeHtml(label)}</span>`
|
|
773
|
+
)
|
|
774
|
+
);
|
|
775
|
+
staged = staged.replace(RE_ICON_MD, (full, token) => {
|
|
776
|
+
const icon = resolveIcon(token);
|
|
777
|
+
if (!icon) return full;
|
|
778
|
+
return stash(
|
|
779
|
+
`<i class="fa-${icon.family} fa-${icon.name}" data-icon="${escapeHtml(token)}" data-family="${icon.family}" data-name="${icon.name}" contenteditable="false"></i>`
|
|
780
|
+
);
|
|
781
|
+
});
|
|
782
|
+
staged = replaceMarkdownLinks(staged, (linkText, destination) => {
|
|
783
|
+
const { url, title } = splitLinkDestination(destination);
|
|
784
|
+
const titleAttr = title === null ? "" : ` title="${escapeHtml(title)}"`;
|
|
785
|
+
return stash(`<a href="${escapeHtml(url)}"${titleAttr}>${inlineToHtml(linkText)}</a>`);
|
|
786
|
+
});
|
|
787
|
+
let result = escapeHtml(staged);
|
|
788
|
+
result = result.replace(
|
|
789
|
+
RE_MD_ESCAPE,
|
|
790
|
+
(_m, ch) => `\0ESC${ESCAPABLE_MD_CHARS.indexOf(ch)}\0`
|
|
791
|
+
);
|
|
792
|
+
result = result.replace(RE_BOLD_STAR, "<strong>$1</strong>");
|
|
793
|
+
result = result.replace(RE_BOLD_UNDER, "<strong>$1</strong>");
|
|
794
|
+
result = result.replace(RE_ITALIC_STAR, "<em>$1</em>");
|
|
795
|
+
result = result.replace(RE_ITALIC_UNDER, "<em>$1</em>");
|
|
796
|
+
result = result.replace(RE_STRIKETHROUGH, "<s>$1</s>");
|
|
797
|
+
for (let index = 0; index < ESCAPABLE_MD_CHARS.length; index++) {
|
|
798
|
+
result = result.split(`\0ESC${index}\0`).join(ESCAPABLE_MD_CHARS[index]);
|
|
799
|
+
}
|
|
800
|
+
for (let index = placeholders.length - 1; index >= 0; index--) {
|
|
801
|
+
result = result.split(`\0PH${index}\0`).join(placeholders[index] ?? "");
|
|
802
|
+
}
|
|
803
|
+
return preserveLeadingSpaces(result);
|
|
804
|
+
}
|
|
805
|
+
function replaceMarkdownLinks(text, replace) {
|
|
806
|
+
let output = "";
|
|
807
|
+
let consumedThrough = 0;
|
|
808
|
+
let searchFrom = 0;
|
|
809
|
+
while (searchFrom < text.length) {
|
|
810
|
+
const labelOpen = text.indexOf("[", searchFrom);
|
|
811
|
+
if (labelOpen < 0) break;
|
|
812
|
+
if (isEscapedMarkdownCharacter(text, labelOpen)) {
|
|
813
|
+
searchFrom = labelOpen + 1;
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
const labelClose = findMatchingMarkdownDelimiter(text, labelOpen, "[", "]");
|
|
817
|
+
const destinationOpen = labelClose + 1;
|
|
818
|
+
if (labelClose < 0 || text[destinationOpen] !== "(") {
|
|
819
|
+
searchFrom = labelOpen + 1;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
const destinationClose = findMatchingMarkdownDelimiter(text, destinationOpen, "(", ")");
|
|
823
|
+
if (destinationClose < 0) {
|
|
824
|
+
searchFrom = labelOpen + 1;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
const label = text.slice(labelOpen + 1, labelClose);
|
|
828
|
+
const href = text.slice(destinationOpen + 1, destinationClose);
|
|
829
|
+
if (!label || !href) {
|
|
830
|
+
searchFrom = labelOpen + 1;
|
|
831
|
+
continue;
|
|
832
|
+
}
|
|
833
|
+
output += text.slice(consumedThrough, labelOpen) + replace(label, href);
|
|
834
|
+
consumedThrough = destinationClose + 1;
|
|
835
|
+
searchFrom = consumedThrough;
|
|
836
|
+
}
|
|
837
|
+
return output + text.slice(consumedThrough);
|
|
838
|
+
}
|
|
839
|
+
function splitLinkDestination(destination) {
|
|
840
|
+
const OPENER_FOR = { '"': '"', "'": "'", ")": "(" };
|
|
841
|
+
const trimmed = destination.trimEnd();
|
|
842
|
+
const opener = OPENER_FOR[trimmed[trimmed.length - 1] ?? ""];
|
|
843
|
+
if (!opener) return { url: destination.trim(), title: null };
|
|
844
|
+
for (let index = trimmed.length - 2; index > 0; index--) {
|
|
845
|
+
if (trimmed[index] !== opener) continue;
|
|
846
|
+
if (isEscapedMarkdownCharacter(trimmed, index)) continue;
|
|
847
|
+
if (!/\s/.test(trimmed[index - 1])) continue;
|
|
848
|
+
const url = trimmed.slice(0, index).trim();
|
|
849
|
+
if (!url) continue;
|
|
850
|
+
return {
|
|
851
|
+
url,
|
|
852
|
+
// Backslash escapes inside a title are real escapes (`\"` is a literal
|
|
853
|
+
// quote), matching core's parser.
|
|
854
|
+
title: trimmed.slice(index + 1, -1).replace(/\\([\\"'()])/g, "$1")
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
return { url: destination.trim(), title: null };
|
|
858
|
+
}
|
|
859
|
+
function escapeLinkTitleAttr(attrValue) {
|
|
860
|
+
return attrValue.replace(/\\/g, "\\\\").replace(/"/g, "\\"");
|
|
861
|
+
}
|
|
862
|
+
function findMatchingMarkdownDelimiter(text, start, open, close) {
|
|
863
|
+
let depth = 0;
|
|
864
|
+
for (let index = start; index < text.length; index++) {
|
|
865
|
+
if (isEscapedMarkdownCharacter(text, index)) continue;
|
|
866
|
+
if (text[index] === open) depth++;
|
|
867
|
+
if (text[index] !== close) continue;
|
|
868
|
+
depth--;
|
|
869
|
+
if (depth === 0) return index;
|
|
870
|
+
}
|
|
871
|
+
return -1;
|
|
872
|
+
}
|
|
873
|
+
function isEscapedMarkdownCharacter(text, index) {
|
|
874
|
+
let backslashes = 0;
|
|
875
|
+
for (let cursor = index - 1; cursor >= 0 && text[cursor] === "\\"; cursor--) {
|
|
876
|
+
backslashes++;
|
|
877
|
+
}
|
|
878
|
+
return backslashes % 2 === 1;
|
|
879
|
+
}
|
|
880
|
+
function isMarkdownWordChar(ch) {
|
|
881
|
+
return ch !== void 0 && /[\p{L}\p{N}_]/u.test(ch);
|
|
882
|
+
}
|
|
883
|
+
function isMarkdownSpace(ch) {
|
|
884
|
+
return ch === void 0 || /\s/.test(ch);
|
|
885
|
+
}
|
|
886
|
+
var RE_BARE_URL = /(?:https?:\/\/|ftp:\/\/|www\.)(?:\([^\s<>()]*\)|[^\s<>[\]()])+/gi;
|
|
887
|
+
function escapeMarkdownText(text) {
|
|
888
|
+
let out = "";
|
|
889
|
+
let cursor = 0;
|
|
890
|
+
RE_BARE_URL.lastIndex = 0;
|
|
891
|
+
let url;
|
|
892
|
+
while ((url = RE_BARE_URL.exec(text)) !== null) {
|
|
893
|
+
out += escapeMarkdownRun(text.slice(cursor, url.index));
|
|
894
|
+
out += url[0];
|
|
895
|
+
cursor = url.index + url[0].length;
|
|
896
|
+
}
|
|
897
|
+
return out + escapeMarkdownRun(text.slice(cursor));
|
|
898
|
+
}
|
|
899
|
+
function escapeMarkdownRun(text) {
|
|
900
|
+
let out = "";
|
|
901
|
+
for (let i = 0; i < text.length; i++) {
|
|
902
|
+
const ch = text[i];
|
|
903
|
+
const prev = text[i - 1];
|
|
904
|
+
const next = text[i + 1];
|
|
905
|
+
if (ch === "\\") {
|
|
906
|
+
out += ESCAPABLE_MD_CHARS.includes(next ?? "") ? "\\\\" : "\\";
|
|
907
|
+
continue;
|
|
908
|
+
}
|
|
909
|
+
if (ch === "~") {
|
|
910
|
+
out += prev === "~" || next === "~" ? "\\~" : "~";
|
|
911
|
+
continue;
|
|
912
|
+
}
|
|
913
|
+
if (ch === "*") {
|
|
914
|
+
out += !isMarkdownSpace(next) || !isMarkdownSpace(prev) ? "\\*" : "*";
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
if (ch === "_") {
|
|
918
|
+
const canOpen = !isMarkdownWordChar(prev) && !isMarkdownSpace(next);
|
|
919
|
+
const canClose = !isMarkdownWordChar(next) && !isMarkdownSpace(prev);
|
|
920
|
+
out += canOpen || canClose ? "\\_" : "_";
|
|
921
|
+
continue;
|
|
922
|
+
}
|
|
923
|
+
out += ch;
|
|
924
|
+
}
|
|
925
|
+
return out;
|
|
926
|
+
}
|
|
927
|
+
function escapeMarkdownTextNodes(html) {
|
|
928
|
+
let out = "";
|
|
929
|
+
let cursor = 0;
|
|
930
|
+
const re = /<code\b[^>]*>[\s\S]*?<\/code>|<[^>]+>/gi;
|
|
931
|
+
let match;
|
|
932
|
+
while ((match = re.exec(html)) !== null) {
|
|
933
|
+
out += escapeMarkdownText(html.slice(cursor, match.index));
|
|
934
|
+
out += match[0];
|
|
935
|
+
cursor = match.index + match[0].length;
|
|
936
|
+
}
|
|
937
|
+
return out + escapeMarkdownText(html.slice(cursor));
|
|
938
|
+
}
|
|
939
|
+
function htmlToInline(html) {
|
|
940
|
+
let result = html;
|
|
941
|
+
result = result.replace(/<br\s*\/?>/gi, " \n");
|
|
942
|
+
result = escapeMarkdownTextNodes(result);
|
|
943
|
+
result = result.replace(RE_ICON_TAG, (_m, token) => `{[${token}]}`);
|
|
944
|
+
result = result.replace(RE_STRONG_TAG, "**$1**");
|
|
945
|
+
result = result.replace(RE_B_TAG, "**$1**");
|
|
946
|
+
result = result.replace(RE_EM_TAG, "*$1*");
|
|
947
|
+
result = result.replace(RE_I_TAG, "*$1*");
|
|
948
|
+
result = result.replace(RE_S_TAG, "~~$1~~");
|
|
949
|
+
result = result.replace(RE_DEL_TAG, "~~$1~~");
|
|
950
|
+
result = result.replace(RE_CODE_TAG, "`$1`");
|
|
951
|
+
result = result.replace(RE_MENTION_TAG, (match, _inner) => {
|
|
952
|
+
const kind = /data-kind="([^"]*)"/i.exec(match)?.[1] ?? "";
|
|
953
|
+
const id = /data-id="([^"]*)"/i.exec(match)?.[1] ?? "";
|
|
954
|
+
const label = /data-label="([^"]*)"/i.exec(match)?.[1] ?? "";
|
|
955
|
+
if (!kind || !id || !label) return match;
|
|
956
|
+
return `@[${label}](${kind}:${id})`;
|
|
957
|
+
});
|
|
958
|
+
result = result.replace(RE_A_TAG, (match, attrs, text) => {
|
|
959
|
+
const href = /\bhref="([^"]*)"/i.exec(attrs)?.[1];
|
|
960
|
+
if (href === void 0) return match;
|
|
961
|
+
const title = /\btitle="([^"]*)"/i.exec(attrs)?.[1];
|
|
962
|
+
const titlePart = title ? ` "${escapeLinkTitleAttr(title)}"` : "";
|
|
963
|
+
return `[${text}](${href}${titlePart})`;
|
|
964
|
+
});
|
|
965
|
+
result = result.replace(RE_IMG_TAG, (match, attrs) => {
|
|
966
|
+
const src = /\bsrc="([^"]*)"/i.exec(attrs)?.[1];
|
|
967
|
+
if (!src) return match;
|
|
968
|
+
const alt = /\balt="([^"]*)"/i.exec(attrs)?.[1] ?? "";
|
|
969
|
+
return serializeImage(src, alt, attrs);
|
|
970
|
+
});
|
|
971
|
+
result = result.replace(RE_STRIP_TAGS, "");
|
|
972
|
+
return restoreLeadingSpaces(unescapeHtml(result));
|
|
973
|
+
}
|
|
974
|
+
function preserveLeadingSpaces(html) {
|
|
975
|
+
return html.replace(/^ +/, (spaces) => " ".repeat(spaces.length));
|
|
976
|
+
}
|
|
977
|
+
function restoreLeadingSpaces(text) {
|
|
978
|
+
return text.replace(/(^|\n)(\u00a0+)/g, (_match, prefix, spaces) => {
|
|
979
|
+
return prefix + " ".repeat(spaces.length);
|
|
980
|
+
});
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
export {
|
|
984
|
+
markdownToTiptap,
|
|
985
|
+
tiptapToMarkdown
|
|
986
|
+
};
|