@blocknote/core 0.50.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +4 -4
- package/dist/{blocks-UU7EM-QM.js → blocks-D0lMxErU.js} +397 -368
- package/dist/blocks-D0lMxErU.js.map +1 -0
- package/dist/blocks-KyMOxPT3.cjs +11 -0
- package/dist/blocks-KyMOxPT3.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/extensions-Cutrafjg.cjs +52 -0
- package/dist/extensions-Cutrafjg.cjs.map +1 -0
- package/dist/{extensions-CkLT--Nc.js → extensions-D2mx2C9y.js} +622 -353
- package/dist/extensions-D2mx2C9y.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +3 -3
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +1 -1
- package/dist/locales.js.map +1 -1
- package/dist/src-D5R5YzV7.cjs +21 -0
- package/dist/src-D5R5YzV7.cjs.map +1 -0
- package/dist/{src-SVFpMAWE.js → src-Dvmif2FY.js} +1070 -564
- package/dist/src-Dvmif2FY.js.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +8 -8
- package/package.json +1 -13
- package/src/api/blockManipulation/commands/insertBlocks/__snapshots__/insertBlocks.test.ts.snap +7 -7
- package/src/api/blockManipulation/commands/insertBlocks/insertBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/mergeBlocks/__snapshots__/mergeBlocks.test.ts.snap +5 -5
- package/src/api/blockManipulation/commands/moveBlocks/__snapshots__/moveBlocks.test.ts.snap +2550 -291
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +83 -2
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +103 -42
- package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +0 -143
- package/src/api/blockManipulation/commands/replaceBlocks/__snapshots__/replaceBlocks.test.ts.snap +12 -12
- package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/splitBlock/__snapshots__/splitBlock.test.ts.snap +6 -6
- package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +22 -22
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +12 -8
- package/src/api/blockManipulation/setupTestEnv.ts +1 -1
- package/src/api/clipboard/fromClipboard/handleVSCodePaste.ts +2 -6
- package/src/api/clipboard/fromClipboard/pasteExtension.ts +7 -2
- package/src/api/clipboard/toClipboard/copyExtension.ts +12 -1
- package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +12 -4
- package/src/api/exporters/markdown/htmlToMarkdown.ts +813 -0
- package/src/api/exporters/markdown/markdownExporter.ts +2 -21
- package/src/api/nodeConversions/blockToNode.ts +3 -2
- package/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap +122 -107
- package/src/api/parsers/html/util/nestedLists.test.ts +1 -12
- package/src/api/parsers/markdown/markdownToHtml.ts +1194 -0
- package/src/api/parsers/markdown/parseMarkdown.ts +2 -101
- package/src/blocks/Audio/block.ts +1 -4
- package/src/blocks/Code/block.test.ts +258 -0
- package/src/blocks/File/block.ts +1 -4
- package/src/blocks/File/helpers/render/createFileBlockWrapper.ts +7 -2
- package/src/blocks/Image/block.ts +9 -6
- package/src/blocks/ListItem/BulletListItem/block.ts +7 -4
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +3 -4
- package/src/blocks/Video/block.ts +4 -5
- package/src/blocks/Video/parseVideoElement.ts +2 -1
- package/src/editor/Block.css +8 -0
- package/src/editor/BlockNoteEditor.test.ts +10 -4
- package/src/editor/BlockNoteEditor.ts +30 -13
- package/src/editor/managers/BlockManager.ts +10 -6
- package/src/editor/managers/ExtensionManager/index.ts +80 -25
- package/src/editor/performance.test.ts +2 -2
- package/src/editor/transformPasted.test.ts +295 -0
- package/src/editor/transformPasted.ts +77 -0
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor-forked.json +1 -12
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor.json +0 -11
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-forked.html +1 -1
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap.html +1 -1
- package/src/extensions/SideMenu/dragging.ts +7 -10
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +21 -0
- package/src/extensions/TrailingNode/TrailingNode.ts +124 -65
- package/src/i18n/locales/fr.ts +1 -1
- package/types/src/api/blockManipulation/commands/moveBlocks/moveBlocks.d.ts +13 -2
- package/types/src/api/clipboard/fromClipboard/handleVSCodePaste.d.ts +1 -1
- package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +1 -0
- package/types/src/api/exporters/markdown/htmlToMarkdown.d.ts +11 -0
- package/types/src/api/parsers/markdown/markdownToHtml.d.ts +5 -0
- package/types/src/blocks/Code/block.test.d.ts +1 -0
- package/types/src/blocks/Video/block.d.ts +2 -0
- package/types/src/blocks/Video/parseVideoElement.d.ts +1 -0
- package/types/src/editor/BlockNoteEditor.d.ts +20 -6
- package/types/src/editor/managers/BlockManager.d.ts +8 -4
- package/types/src/editor/transformPasted.test.d.ts +1 -0
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +8 -3
- package/dist/blocks-BFnTypT-.cjs +0 -11
- package/dist/blocks-BFnTypT-.cjs.map +0 -1
- package/dist/blocks-UU7EM-QM.js.map +0 -1
- package/dist/extensions-CYsFi5Is.cjs +0 -2
- package/dist/extensions-CYsFi5Is.cjs.map +0 -1
- package/dist/extensions-CkLT--Nc.js.map +0 -1
- package/dist/src-DsJ6yzs9.cjs +0 -5
- package/dist/src-DsJ6yzs9.cjs.map +0 -1
- package/dist/src-SVFpMAWE.js.map +0 -1
- package/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.ts +0 -42
- package/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.ts +0 -19
- package/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.ts +0 -39
- package/types/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.d.ts +0 -7
- package/types/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.d.ts +0 -2
- package/types/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.d.ts +0 -6
|
@@ -0,0 +1,813 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom HTML-to-Markdown serializer for BlockNote.
|
|
3
|
+
* Replaces the unified/rehype-remark pipeline with a direct DOM-based implementation.
|
|
4
|
+
*
|
|
5
|
+
* Input: HTML string from createExternalHTMLExporter
|
|
6
|
+
* Output: GFM-compatible markdown string
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Convert an HTML string (from BlockNote's external HTML exporter) to markdown.
|
|
11
|
+
*/
|
|
12
|
+
export function htmlToMarkdown(html: string): string {
|
|
13
|
+
// Use a temporary element to parse HTML. This works in both browser and
|
|
14
|
+
// server (JSDOM) environments, unlike `new DOMParser()` which may not be
|
|
15
|
+
// globally available in Node.js.
|
|
16
|
+
const container = document.createElement("div");
|
|
17
|
+
container.innerHTML = html;
|
|
18
|
+
const result = serializeChildren(container, {
|
|
19
|
+
indent: "",
|
|
20
|
+
inListItem: false,
|
|
21
|
+
});
|
|
22
|
+
return result.trim() + "\n";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface SerializeContext {
|
|
26
|
+
indent: string; // current indentation prefix for list nesting
|
|
27
|
+
// True when the current node is being serialized as continuation content
|
|
28
|
+
// of a parent list item. Used to suppress trailing blank lines that would
|
|
29
|
+
// otherwise turn the parent list into a "loose" list.
|
|
30
|
+
inListItem: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ─── Main Serializer ─────────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
function serializeChildren(node: Node, ctx: SerializeContext): string {
|
|
36
|
+
let result = "";
|
|
37
|
+
const children = Array.from(node.childNodes);
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < children.length; i++) {
|
|
40
|
+
const child = children[i];
|
|
41
|
+
result += serializeNode(child, ctx);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function serializeNode(node: Node, ctx: SerializeContext): string {
|
|
48
|
+
if (node.nodeType === 3 /* Node.TEXT_NODE */) {
|
|
49
|
+
return node.textContent || "";
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (node.nodeType !== 1 /* Node.ELEMENT_NODE */) {
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const el = node as HTMLElement;
|
|
57
|
+
const tag = el.tagName.toLowerCase();
|
|
58
|
+
|
|
59
|
+
switch (tag) {
|
|
60
|
+
case "p":
|
|
61
|
+
return serializeParagraph(el, ctx);
|
|
62
|
+
case "h1":
|
|
63
|
+
case "h2":
|
|
64
|
+
case "h3":
|
|
65
|
+
case "h4":
|
|
66
|
+
case "h5":
|
|
67
|
+
case "h6":
|
|
68
|
+
return serializeHeading(el, ctx);
|
|
69
|
+
case "blockquote":
|
|
70
|
+
return serializeBlockquote(el, ctx);
|
|
71
|
+
case "pre":
|
|
72
|
+
return serializeCodeBlock(el, ctx);
|
|
73
|
+
case "ul":
|
|
74
|
+
return serializeUnorderedList(el, ctx);
|
|
75
|
+
case "ol":
|
|
76
|
+
return serializeOrderedList(el, ctx);
|
|
77
|
+
case "table":
|
|
78
|
+
return serializeTable(el, ctx);
|
|
79
|
+
case "hr":
|
|
80
|
+
return ctx.indent + "***\n\n";
|
|
81
|
+
case "img":
|
|
82
|
+
return serializeImage(el, ctx);
|
|
83
|
+
case "video":
|
|
84
|
+
return serializeVideo(el, ctx);
|
|
85
|
+
case "audio":
|
|
86
|
+
return serializeAudio(el, ctx);
|
|
87
|
+
case "embed":
|
|
88
|
+
return serializeEmbed(el, ctx);
|
|
89
|
+
case "figure":
|
|
90
|
+
return serializeFigure(el, ctx);
|
|
91
|
+
case "a":
|
|
92
|
+
// Block-level link (file block)
|
|
93
|
+
return serializeBlockLink(el, ctx);
|
|
94
|
+
case "details":
|
|
95
|
+
return serializeDetails(el, ctx);
|
|
96
|
+
case "div":
|
|
97
|
+
// Page break or generic container — serialize children
|
|
98
|
+
return serializeChildren(el, ctx);
|
|
99
|
+
case "br":
|
|
100
|
+
return "";
|
|
101
|
+
default:
|
|
102
|
+
return serializeChildren(el, ctx);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ─── Block Serializers ───────────────────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
function serializeParagraph(el: HTMLElement, ctx: SerializeContext): string {
|
|
109
|
+
const content = serializeInlineContent(el);
|
|
110
|
+
// Trim leading/trailing hard breaks (matching remark behavior)
|
|
111
|
+
const trimmed = trimHardBreaks(content);
|
|
112
|
+
if (ctx.inListItem) {
|
|
113
|
+
return trimmed;
|
|
114
|
+
}
|
|
115
|
+
return ctx.indent + trimmed + "\n\n";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function serializeHeading(el: HTMLElement, ctx: SerializeContext): string {
|
|
119
|
+
const level = parseInt(el.tagName[1], 10);
|
|
120
|
+
const prefix = "#".repeat(level) + " ";
|
|
121
|
+
const content = serializeInlineContent(el);
|
|
122
|
+
return ctx.indent + prefix + content + "\n\n";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function serializeBlockquote(el: HTMLElement, ctx: SerializeContext): string {
|
|
126
|
+
// Check if blockquote contains block-level elements (like <p>)
|
|
127
|
+
const blockChildren = Array.from(el.children).filter((child) => {
|
|
128
|
+
const tag = child.tagName.toLowerCase();
|
|
129
|
+
return ["p", "ul", "ol", "pre", "blockquote", "table", "hr"].includes(tag);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
let content: string;
|
|
133
|
+
if (blockChildren.length > 0) {
|
|
134
|
+
// Has block-level children — serialize each
|
|
135
|
+
const parts: string[] = [];
|
|
136
|
+
for (const child of blockChildren) {
|
|
137
|
+
const tag = child.tagName.toLowerCase();
|
|
138
|
+
if (tag === "p") {
|
|
139
|
+
parts.push(serializeInlineContent(child as HTMLElement));
|
|
140
|
+
} else {
|
|
141
|
+
const innerCtx: SerializeContext = { indent: "", inListItem: false };
|
|
142
|
+
parts.push(serializeNode(child, innerCtx).trim());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
content = parts.join("\n\n");
|
|
146
|
+
} else {
|
|
147
|
+
// No block-level children — treat entire content as inline
|
|
148
|
+
content = serializeInlineContent(el);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const lines = content.split("\n");
|
|
152
|
+
return lines.map((line) => ctx.indent + "> " + line).join("\n") + "\n\n";
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function serializeCodeBlock(el: HTMLElement, ctx: SerializeContext): string {
|
|
156
|
+
const codeEl = el.querySelector("code");
|
|
157
|
+
if (!codeEl) {return "";}
|
|
158
|
+
|
|
159
|
+
const language =
|
|
160
|
+
codeEl.getAttribute("data-language") ||
|
|
161
|
+
extractLanguageFromClass(codeEl.className) ||
|
|
162
|
+
"";
|
|
163
|
+
|
|
164
|
+
// Extract code content, handling <br> elements as newlines
|
|
165
|
+
const code = extractCodeContent(codeEl);
|
|
166
|
+
|
|
167
|
+
// Use a fence longer than the longest backtick run in the code
|
|
168
|
+
const longestRun = Math.max(
|
|
169
|
+
0,
|
|
170
|
+
...((code.match(/`+/g) ?? []).map((run) => run.length))
|
|
171
|
+
);
|
|
172
|
+
const fence = "`".repeat(Math.max(3, longestRun + 1));
|
|
173
|
+
|
|
174
|
+
// For empty code blocks, don't add a newline between the fences
|
|
175
|
+
if (!code) {
|
|
176
|
+
return ctx.indent + fence + language + "\n" + fence + "\n\n";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
ctx.indent +
|
|
181
|
+
fence +
|
|
182
|
+
language +
|
|
183
|
+
"\n" +
|
|
184
|
+
code +
|
|
185
|
+
(code.endsWith("\n") ? "" : "\n") +
|
|
186
|
+
fence +
|
|
187
|
+
"\n\n"
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function extractCodeContent(el: Element): string {
|
|
192
|
+
let result = "";
|
|
193
|
+
for (const child of Array.from(el.childNodes)) {
|
|
194
|
+
if (child.nodeType === 3 /* Node.TEXT_NODE */) {
|
|
195
|
+
result += child.textContent || "";
|
|
196
|
+
} else if (child.nodeType === 1 /* Node.ELEMENT_NODE */) {
|
|
197
|
+
const tag = (child as HTMLElement).tagName.toLowerCase();
|
|
198
|
+
if (tag === "br") {
|
|
199
|
+
result += "\n";
|
|
200
|
+
} else {
|
|
201
|
+
result += extractCodeContent(child as Element);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function extractLanguageFromClass(className: string): string {
|
|
209
|
+
const match = className.match(/language-(\S+)/);
|
|
210
|
+
return match ? match[1] : "";
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function serializeUnorderedList(
|
|
214
|
+
el: HTMLElement,
|
|
215
|
+
ctx: SerializeContext
|
|
216
|
+
): string {
|
|
217
|
+
let result = "";
|
|
218
|
+
const items = Array.from(el.children).filter(
|
|
219
|
+
(child) => child.tagName.toLowerCase() === "li"
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
for (const item of items) {
|
|
223
|
+
result += serializeListItem(item as HTMLElement, "bullet", ctx);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Trailing blank line separates the list from the next block. Skip when
|
|
227
|
+
// this list is nested inside another list item — adding it would convert
|
|
228
|
+
// the parent list into a "loose" list (or break tightness).
|
|
229
|
+
if (!ctx.inListItem) {
|
|
230
|
+
result += "\n";
|
|
231
|
+
}
|
|
232
|
+
return result;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function serializeOrderedList(el: HTMLElement, ctx: SerializeContext): string {
|
|
236
|
+
let result = "";
|
|
237
|
+
const items = Array.from(el.children).filter(
|
|
238
|
+
(child) => child.tagName.toLowerCase() === "li"
|
|
239
|
+
);
|
|
240
|
+
const startNum = parseInt(el.getAttribute("start") || "1", 10);
|
|
241
|
+
|
|
242
|
+
for (let i = 0; i < items.length; i++) {
|
|
243
|
+
const num = startNum + i;
|
|
244
|
+
result += serializeListItem(items[i] as HTMLElement, "ordered", ctx, num);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (!ctx.inListItem) {
|
|
248
|
+
result += "\n";
|
|
249
|
+
}
|
|
250
|
+
return result;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function serializeListItem(
|
|
254
|
+
el: HTMLElement,
|
|
255
|
+
listType: "bullet" | "ordered",
|
|
256
|
+
ctx: SerializeContext,
|
|
257
|
+
num?: number
|
|
258
|
+
): string {
|
|
259
|
+
// Check for checkbox (task list) - direct children only
|
|
260
|
+
let checkbox: HTMLInputElement | null = null;
|
|
261
|
+
let details: HTMLElement | null = null;
|
|
262
|
+
|
|
263
|
+
for (const child of Array.from(el.children)) {
|
|
264
|
+
const tag = child.tagName.toLowerCase();
|
|
265
|
+
if (tag === "input" && (child as HTMLInputElement).type === "checkbox") {
|
|
266
|
+
checkbox = child as HTMLInputElement;
|
|
267
|
+
}
|
|
268
|
+
if (tag === "details") {
|
|
269
|
+
details = child as HTMLElement;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
let marker: string;
|
|
274
|
+
let markerWidth: number;
|
|
275
|
+
|
|
276
|
+
if (checkbox) {
|
|
277
|
+
const state = checkbox.checked ? "[x]" : "[ ]";
|
|
278
|
+
marker = `* ${state} `;
|
|
279
|
+
// For child indentation, use bullet width (2), not full checkbox marker width
|
|
280
|
+
markerWidth = 2;
|
|
281
|
+
} else if (listType === "ordered") {
|
|
282
|
+
marker = `${num}. `;
|
|
283
|
+
markerWidth = marker.length;
|
|
284
|
+
} else {
|
|
285
|
+
marker = "* ";
|
|
286
|
+
markerWidth = 2;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Collect the item's inline content
|
|
290
|
+
let inlineContent: string;
|
|
291
|
+
let firstContentEl: Element | null;
|
|
292
|
+
|
|
293
|
+
if (details) {
|
|
294
|
+
// Toggle item: get content from summary
|
|
295
|
+
const summary = details.querySelector("summary");
|
|
296
|
+
const summaryP = summary?.querySelector("p");
|
|
297
|
+
firstContentEl = details;
|
|
298
|
+
inlineContent = summaryP ? serializeInlineContent(summaryP) : "";
|
|
299
|
+
} else {
|
|
300
|
+
firstContentEl = getFirstContentElement(el, checkbox);
|
|
301
|
+
inlineContent = firstContentEl ? serializeInlineContent(firstContentEl) : "";
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// The marker line ends with a single `\n` so that consecutive list items
|
|
305
|
+
// produce a "tight" list (no blank line between markers). Continuation
|
|
306
|
+
// content within the item (nested lists, continuation paragraphs, other
|
|
307
|
+
// blocks) injects its own spacing as needed.
|
|
308
|
+
let result = ctx.indent + marker + inlineContent + "\n";
|
|
309
|
+
|
|
310
|
+
// Serialize child content (nested lists, continuation paragraphs, etc.)
|
|
311
|
+
const childIndent = ctx.indent + " ".repeat(markerWidth);
|
|
312
|
+
const childCtx: SerializeContext = { indent: childIndent, inListItem: true };
|
|
313
|
+
|
|
314
|
+
// For toggle items, also serialize children inside the details element
|
|
315
|
+
if (details) {
|
|
316
|
+
const summary = details.querySelector("summary");
|
|
317
|
+
for (const child of Array.from(details.children)) {
|
|
318
|
+
if (child === summary) {continue;}
|
|
319
|
+
const childTag = child.tagName.toLowerCase();
|
|
320
|
+
if (childTag === "p") {
|
|
321
|
+
const content = serializeInlineContent(child as HTMLElement);
|
|
322
|
+
// Continuation paragraph needs a blank line to separate it from the
|
|
323
|
+
// previous content; CommonMark would otherwise treat it as a soft
|
|
324
|
+
// wrap of that content.
|
|
325
|
+
result += "\n" + childIndent + content + "\n";
|
|
326
|
+
} else {
|
|
327
|
+
result += serializeNode(child, childCtx);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const children = Array.from(el.children);
|
|
333
|
+
for (const child of children) {
|
|
334
|
+
const childTag = child.tagName.toLowerCase();
|
|
335
|
+
|
|
336
|
+
// Skip the first content element and checkbox
|
|
337
|
+
if (child === firstContentEl || (child as HTMLElement) === checkbox) {continue;}
|
|
338
|
+
if (childTag === "input") {continue;}
|
|
339
|
+
|
|
340
|
+
// Nested lists and other block content
|
|
341
|
+
if (childTag === "ul" || childTag === "ol") {
|
|
342
|
+
// Nested list flows directly under the parent marker — no blank line.
|
|
343
|
+
result += serializeNode(child, childCtx);
|
|
344
|
+
} else if (childTag === "p") {
|
|
345
|
+
// Continuation paragraph within list item — requires blank line before
|
|
346
|
+
// so it isn't read as part of the marker line's text.
|
|
347
|
+
const content = serializeInlineContent(child as HTMLElement);
|
|
348
|
+
result += "\n" + childIndent + content + "\n";
|
|
349
|
+
} else {
|
|
350
|
+
// Other block-level children (code blocks, blockquotes, etc.) already
|
|
351
|
+
// emit their own separating newlines; prefix with a blank line so they
|
|
352
|
+
// are recognized as separate blocks.
|
|
353
|
+
result += "\n" + serializeNode(child, childCtx);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function getFirstContentElement(
|
|
361
|
+
li: HTMLElement,
|
|
362
|
+
checkbox: HTMLInputElement | null
|
|
363
|
+
): HTMLElement | null {
|
|
364
|
+
for (const child of Array.from(li.children)) {
|
|
365
|
+
if (child === checkbox) {continue;}
|
|
366
|
+
if (child.tagName.toLowerCase() === "input") {continue;}
|
|
367
|
+
const tag = child.tagName.toLowerCase();
|
|
368
|
+
if (tag === "p" || tag === "span") {return child as HTMLElement;}
|
|
369
|
+
}
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ─── Table Serializer ────────────────────────────────────────────────────────
|
|
374
|
+
|
|
375
|
+
function serializeTable(el: HTMLElement, ctx: SerializeContext): string {
|
|
376
|
+
// First, determine column count from colgroup or first row
|
|
377
|
+
const colgroup = el.querySelector("colgroup");
|
|
378
|
+
let colCount = 0;
|
|
379
|
+
|
|
380
|
+
if (colgroup) {
|
|
381
|
+
colCount = colgroup.querySelectorAll("col").length;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const rows: string[][] = [];
|
|
385
|
+
let hasHeader = false;
|
|
386
|
+
|
|
387
|
+
// Collect all rows, handling colspan/rowspan
|
|
388
|
+
const trElements = el.querySelectorAll("tr");
|
|
389
|
+
// Build a grid to handle colspan/rowspan
|
|
390
|
+
const grid: (string | null)[][] = [];
|
|
391
|
+
|
|
392
|
+
trElements.forEach((tr, rowIdx) => {
|
|
393
|
+
if (!grid[rowIdx]) {grid[rowIdx] = [];}
|
|
394
|
+
const cellElements = tr.querySelectorAll("th, td");
|
|
395
|
+
let gridCol = 0;
|
|
396
|
+
|
|
397
|
+
cellElements.forEach((cell) => {
|
|
398
|
+
// Find next empty column in this row
|
|
399
|
+
while (grid[rowIdx][gridCol] !== undefined) {gridCol++;}
|
|
400
|
+
|
|
401
|
+
if (rowIdx === 0 && cell.tagName.toLowerCase() === "th") {
|
|
402
|
+
hasHeader = true;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const content = escapeTableCell(
|
|
406
|
+
serializeInlineContent(cell as HTMLElement).trim()
|
|
407
|
+
);
|
|
408
|
+
const colspan = parseInt(cell.getAttribute("colspan") || "1", 10);
|
|
409
|
+
const rowspan = parseInt(cell.getAttribute("rowspan") || "1", 10);
|
|
410
|
+
|
|
411
|
+
// Fill the grid
|
|
412
|
+
for (let r = 0; r < rowspan; r++) {
|
|
413
|
+
for (let c = 0; c < colspan; c++) {
|
|
414
|
+
const ri = rowIdx + r;
|
|
415
|
+
if (!grid[ri]) {grid[ri] = [];}
|
|
416
|
+
grid[ri][gridCol + c] = r === 0 && c === 0 ? content : "";
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
gridCol += colspan;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Update colCount
|
|
424
|
+
if (grid[rowIdx]) {
|
|
425
|
+
colCount = Math.max(colCount, grid[rowIdx].length);
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// Convert grid to rows
|
|
430
|
+
for (const gridRow of grid) {
|
|
431
|
+
const row: string[] = [];
|
|
432
|
+
for (let c = 0; c < colCount; c++) {
|
|
433
|
+
row.push(gridRow && gridRow[c] !== undefined ? (gridRow[c] ?? "") : "");
|
|
434
|
+
}
|
|
435
|
+
rows.push(row);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (rows.length === 0) {return "";}
|
|
439
|
+
|
|
440
|
+
// Determine column widths
|
|
441
|
+
const colWidths: number[] = [];
|
|
442
|
+
for (let c = 0; c < colCount; c++) {
|
|
443
|
+
let maxWidth = 3; // minimum width for separator "---"
|
|
444
|
+
for (const row of rows) {
|
|
445
|
+
const cellWidth = c < row.length ? row[c].length : 0;
|
|
446
|
+
maxWidth = Math.max(maxWidth, cellWidth);
|
|
447
|
+
}
|
|
448
|
+
// Use minimum of 10 to match remark output
|
|
449
|
+
colWidths.push(Math.max(maxWidth, 10));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
let result = "";
|
|
453
|
+
|
|
454
|
+
if (hasHeader) {
|
|
455
|
+
result += ctx.indent + formatTableRow(rows[0], colWidths, colCount) + "\n";
|
|
456
|
+
result += ctx.indent + formatSeparatorRow(colWidths, colCount) + "\n";
|
|
457
|
+
for (let r = 1; r < rows.length; r++) {
|
|
458
|
+
result +=
|
|
459
|
+
ctx.indent + formatTableRow(rows[r], colWidths, colCount) + "\n";
|
|
460
|
+
}
|
|
461
|
+
} else {
|
|
462
|
+
// No header — emit empty header + separator
|
|
463
|
+
const emptyRow = new Array(colCount).fill("");
|
|
464
|
+
result += ctx.indent + formatTableRow(emptyRow, colWidths, colCount) + "\n";
|
|
465
|
+
result += ctx.indent + formatSeparatorRow(colWidths, colCount) + "\n";
|
|
466
|
+
for (const row of rows) {
|
|
467
|
+
result +=
|
|
468
|
+
ctx.indent + formatTableRow(row, colWidths, colCount) + "\n";
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
result += "\n";
|
|
473
|
+
return result;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function escapeTableCell(text: string): string {
|
|
477
|
+
return text.replace(/\|/g, "\\|");
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function formatTableRow(
|
|
481
|
+
cells: string[],
|
|
482
|
+
colWidths: number[],
|
|
483
|
+
colCount: number
|
|
484
|
+
): string {
|
|
485
|
+
const parts: string[] = [];
|
|
486
|
+
for (let c = 0; c < colCount; c++) {
|
|
487
|
+
const cell = c < cells.length ? cells[c] : "";
|
|
488
|
+
parts.push(" " + cell.padEnd(colWidths[c]) + " ");
|
|
489
|
+
}
|
|
490
|
+
return "|" + parts.join("|") + "|";
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function formatSeparatorRow(colWidths: number[], colCount: number): string {
|
|
494
|
+
const parts: string[] = [];
|
|
495
|
+
for (let c = 0; c < colCount; c++) {
|
|
496
|
+
parts.push(" " + "-".repeat(colWidths[c]) + " ");
|
|
497
|
+
}
|
|
498
|
+
return "|" + parts.join("|") + "|";
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// ─── Media Serializers ───────────────────────────────────────────────────────
|
|
502
|
+
|
|
503
|
+
function serializeImage(el: HTMLElement, ctx: SerializeContext): string {
|
|
504
|
+
const src = el.getAttribute("src") || "";
|
|
505
|
+
const alt = el.getAttribute("alt") || "";
|
|
506
|
+
// Empty placeholder — preserve the block-level break, matching how
|
|
507
|
+
// serializeParagraph/serializeHeading emit `\n\n` for empty content.
|
|
508
|
+
if (!src) {return "\n\n";}
|
|
509
|
+
return ctx.indent + `\n\n`;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function serializeVideo(el: HTMLElement, ctx: SerializeContext): string {
|
|
513
|
+
const src =
|
|
514
|
+
el.getAttribute("src") || el.getAttribute("data-url") || "";
|
|
515
|
+
const name = el.getAttribute("data-name") || el.getAttribute("title") || "";
|
|
516
|
+
if (!src) {return "\n\n";}
|
|
517
|
+
return ctx.indent + `\n\n`;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function serializeAudio(el: HTMLElement, ctx: SerializeContext): string {
|
|
521
|
+
const src = el.getAttribute("src") || "";
|
|
522
|
+
if (!src) {return "\n\n";}
|
|
523
|
+
// Audio has no markdown syntax, so emit raw HTML. The markdown parser
|
|
524
|
+
// passes <audio> blocks through verbatim and BlockNote's audio block parser
|
|
525
|
+
// recognizes them, giving a clean round-trip.
|
|
526
|
+
return ctx.indent + `<audio src="${escapeHtmlAttr(src)}" controls></audio>\n\n`;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function serializeEmbed(el: HTMLElement, ctx: SerializeContext): string {
|
|
530
|
+
const src = el.getAttribute("src") || "";
|
|
531
|
+
if (!src) {return "\n\n";}
|
|
532
|
+
return ctx.indent + `[](${src})\n\n`;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function serializeFigure(el: HTMLElement, ctx: SerializeContext): string {
|
|
536
|
+
const img = el.querySelector("img");
|
|
537
|
+
const video = el.querySelector("video");
|
|
538
|
+
const audio = el.querySelector("audio");
|
|
539
|
+
const link = el.querySelector("a");
|
|
540
|
+
|
|
541
|
+
const figcaption = el.querySelector("figcaption");
|
|
542
|
+
const captionText = figcaption?.textContent?.trim() || "";
|
|
543
|
+
|
|
544
|
+
if (img) {
|
|
545
|
+
return serializeMediaFigure(
|
|
546
|
+
"img",
|
|
547
|
+
img.getAttribute("src") || "",
|
|
548
|
+
img.getAttribute("alt") || "",
|
|
549
|
+
captionText,
|
|
550
|
+
ctx,
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
if (video) {
|
|
554
|
+
const src =
|
|
555
|
+
video.getAttribute("src") || video.getAttribute("data-url") || "";
|
|
556
|
+
const name =
|
|
557
|
+
video.getAttribute("data-name") || video.getAttribute("title") || "";
|
|
558
|
+
return serializeMediaFigure("video", src, name, captionText, ctx);
|
|
559
|
+
}
|
|
560
|
+
if (audio) {
|
|
561
|
+
return serializeMediaFigure(
|
|
562
|
+
"audio",
|
|
563
|
+
audio.getAttribute("src") || "",
|
|
564
|
+
"",
|
|
565
|
+
captionText,
|
|
566
|
+
ctx,
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
if (link) {
|
|
570
|
+
return serializeBlockLink(link as HTMLElement, ctx);
|
|
571
|
+
}
|
|
572
|
+
return "";
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function serializeMediaFigure(
|
|
576
|
+
kind: "img" | "video" | "audio",
|
|
577
|
+
src: string,
|
|
578
|
+
descriptor: string,
|
|
579
|
+
captionText: string,
|
|
580
|
+
ctx: SerializeContext,
|
|
581
|
+
): string {
|
|
582
|
+
if (!src) {return "";}
|
|
583
|
+
|
|
584
|
+
// No caption + has a markdown shorthand → use it.
|
|
585
|
+
if (!captionText && kind !== "audio") {
|
|
586
|
+
return ctx.indent + `\n\n`;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// The descriptor (alt / data-name) is dropped when it duplicates the
|
|
590
|
+
// caption text; otherwise on round-trip both `name` and `caption` would
|
|
591
|
+
// get set to the same string (BlockNote's HTML exporter writes alt =
|
|
592
|
+
// name || caption, so a caption-only image has alt === figcaption text).
|
|
593
|
+
const showDescriptor = descriptor && descriptor !== captionText;
|
|
594
|
+
const descAttr =
|
|
595
|
+
!showDescriptor
|
|
596
|
+
? ""
|
|
597
|
+
: kind === "img"
|
|
598
|
+
? ` alt="${escapeHtmlAttr(descriptor)}"`
|
|
599
|
+
: kind === "video"
|
|
600
|
+
? ` data-name="${escapeHtmlAttr(descriptor)}"`
|
|
601
|
+
: "";
|
|
602
|
+
|
|
603
|
+
const tag =
|
|
604
|
+
kind === "img"
|
|
605
|
+
? `<img${descAttr} src="${escapeHtmlAttr(src)}">`
|
|
606
|
+
: `<${kind} src="${escapeHtmlAttr(src)}"${descAttr} controls></${kind}>`;
|
|
607
|
+
|
|
608
|
+
const captionPart = captionText
|
|
609
|
+
? `<figcaption>${escapeHtmlText(captionText)}</figcaption>`
|
|
610
|
+
: "";
|
|
611
|
+
return ctx.indent + `<figure>${tag}${captionPart}</figure>\n\n`;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function escapeHtmlAttr(value: string): string {
|
|
615
|
+
return value
|
|
616
|
+
.replace(/&/g, "&")
|
|
617
|
+
.replace(/"/g, """)
|
|
618
|
+
.replace(/</g, "<")
|
|
619
|
+
.replace(/>/g, ">");
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
function escapeHtmlText(value: string): string {
|
|
623
|
+
return value
|
|
624
|
+
.replace(/&/g, "&")
|
|
625
|
+
.replace(/</g, "<")
|
|
626
|
+
.replace(/>/g, ">");
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function serializeBlockLink(el: HTMLElement, ctx: SerializeContext): string {
|
|
630
|
+
const href = el.getAttribute("href") || "";
|
|
631
|
+
const text = el.textContent?.trim() || "";
|
|
632
|
+
if (!href) {return ctx.indent + text + "\n\n";}
|
|
633
|
+
return ctx.indent + formatLink(text, href) + "\n\n";
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Render a link, mirroring the remark-stringify behavior from
|
|
638
|
+
* TypeCellOS/BlockNote#2661: when the link label equals the URL (or is
|
|
639
|
+
* empty), emit the bare URL so that pasting the link into another input
|
|
640
|
+
* produces a valid href instead of `<url>`-autolink brackets or redundant
|
|
641
|
+
* `[url](url)` markup. Otherwise emit `[text](url)` with the URL escaped so
|
|
642
|
+
* a `)` inside the URL does not prematurely close the destination.
|
|
643
|
+
*/
|
|
644
|
+
function formatLink(text: string, href: string): string {
|
|
645
|
+
if (!text || text === href) {
|
|
646
|
+
return href;
|
|
647
|
+
}
|
|
648
|
+
return `[${text}](${escapeLinkDestination(href)})`;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
function escapeLinkDestination(url: string): string {
|
|
652
|
+
return url.replace(/[\\()]/g, "\\$&");
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
function serializeDetails(el: HTMLElement, ctx: SerializeContext): string {
|
|
656
|
+
// Toggle heading or toggle list item
|
|
657
|
+
const summary = el.querySelector("summary");
|
|
658
|
+
if (!summary) {return serializeChildren(el, ctx);}
|
|
659
|
+
|
|
660
|
+
// Check if summary contains a heading
|
|
661
|
+
const heading = summary.querySelector("h1, h2, h3, h4, h5, h6");
|
|
662
|
+
if (heading) {
|
|
663
|
+
let result = serializeHeading(heading as HTMLElement, ctx);
|
|
664
|
+
// Also serialize non-summary children of details
|
|
665
|
+
for (const child of Array.from(el.children)) {
|
|
666
|
+
if (child !== summary) {
|
|
667
|
+
result += serializeNode(child, ctx);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
return result;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// Otherwise serialize the summary content
|
|
674
|
+
return serializeChildren(summary, ctx);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// ─── Inline Content Serializer ───────────────────────────────────────────────
|
|
678
|
+
|
|
679
|
+
function serializeInlineContent(el: Element): string {
|
|
680
|
+
let result = "";
|
|
681
|
+
|
|
682
|
+
for (const child of Array.from(el.childNodes)) {
|
|
683
|
+
if (child.nodeType === 3 /* Node.TEXT_NODE */) {
|
|
684
|
+
result += child.textContent || "";
|
|
685
|
+
} else if (child.nodeType === 1 /* Node.ELEMENT_NODE */) {
|
|
686
|
+
const childEl = child as HTMLElement;
|
|
687
|
+
const tag = childEl.tagName.toLowerCase();
|
|
688
|
+
|
|
689
|
+
switch (tag) {
|
|
690
|
+
case "strong":
|
|
691
|
+
case "b": {
|
|
692
|
+
const inner = serializeInlineContent(childEl);
|
|
693
|
+
const { content, trailing } = extractTrailingWhitespace(inner);
|
|
694
|
+
if (content) {
|
|
695
|
+
result += `**${content}**${trailing}`;
|
|
696
|
+
} else {
|
|
697
|
+
// All whitespace — just output it without emphasis
|
|
698
|
+
result += trailing;
|
|
699
|
+
}
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
case "em":
|
|
703
|
+
case "i": {
|
|
704
|
+
const inner = serializeInlineContent(childEl);
|
|
705
|
+
const { content, trailing } = extractTrailingWhitespace(inner);
|
|
706
|
+
if (content) {
|
|
707
|
+
result += `*${content}*${trailing}`;
|
|
708
|
+
} else {
|
|
709
|
+
result += trailing;
|
|
710
|
+
}
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
case "s":
|
|
714
|
+
case "del":
|
|
715
|
+
result += `~~${serializeInlineContent(childEl)}~~`;
|
|
716
|
+
break;
|
|
717
|
+
case "code": {
|
|
718
|
+
const text = childEl.textContent || "";
|
|
719
|
+
const longestRun = Math.max(
|
|
720
|
+
0,
|
|
721
|
+
...((text.match(/`+/g) ?? []).map((run) => run.length))
|
|
722
|
+
);
|
|
723
|
+
const fence = "`".repeat(longestRun + 1);
|
|
724
|
+
const needsPadding =
|
|
725
|
+
text.startsWith("`") || text.endsWith("`");
|
|
726
|
+
result += fence + (needsPadding ? ` ${text} ` : text) + fence;
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
case "u":
|
|
730
|
+
// No markdown equivalent — strip the tag, keep content
|
|
731
|
+
result += serializeInlineContent(childEl);
|
|
732
|
+
break;
|
|
733
|
+
case "a": {
|
|
734
|
+
const href = childEl.getAttribute("href") || "";
|
|
735
|
+
const text = serializeInlineContent(childEl);
|
|
736
|
+
result += formatLink(text, href);
|
|
737
|
+
break;
|
|
738
|
+
}
|
|
739
|
+
case "br":
|
|
740
|
+
result += "\\\n";
|
|
741
|
+
break;
|
|
742
|
+
case "span":
|
|
743
|
+
// Color spans, etc. — strip the tag, keep content
|
|
744
|
+
result += serializeInlineContent(childEl);
|
|
745
|
+
break;
|
|
746
|
+
case "img": {
|
|
747
|
+
const src = childEl.getAttribute("src") || "";
|
|
748
|
+
const alt = childEl.getAttribute("alt") || "";
|
|
749
|
+
result += ``;
|
|
750
|
+
break;
|
|
751
|
+
}
|
|
752
|
+
case "video": {
|
|
753
|
+
const src =
|
|
754
|
+
childEl.getAttribute("src") ||
|
|
755
|
+
childEl.getAttribute("data-url") ||
|
|
756
|
+
"";
|
|
757
|
+
const name =
|
|
758
|
+
childEl.getAttribute("data-name") ||
|
|
759
|
+
childEl.getAttribute("title") ||
|
|
760
|
+
"";
|
|
761
|
+
result += ``;
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
case "p":
|
|
765
|
+
// Paragraph inside inline context (e.g., table cell)
|
|
766
|
+
result += serializeInlineContent(childEl);
|
|
767
|
+
break;
|
|
768
|
+
case "input":
|
|
769
|
+
// Checkbox in task list — handled at block level
|
|
770
|
+
break;
|
|
771
|
+
default:
|
|
772
|
+
result += serializeInlineContent(childEl);
|
|
773
|
+
break;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
return result;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Extract trailing whitespace from emphasis content.
|
|
783
|
+
* Moves trailing spaces outside the emphasis delimiters to produce valid markdown.
|
|
784
|
+
* E.g., `<strong>Bold </strong>` → `**Bold** ` instead of `**Bold **`.
|
|
785
|
+
*/
|
|
786
|
+
function extractTrailingWhitespace(text: string): {
|
|
787
|
+
content: string;
|
|
788
|
+
trailing: string;
|
|
789
|
+
} {
|
|
790
|
+
const match = text.match(/^(.*?)(\s*)$/);
|
|
791
|
+
if (match) {
|
|
792
|
+
return { content: match[1], trailing: match[2] };
|
|
793
|
+
}
|
|
794
|
+
return { content: text, trailing: "" };
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Escape leading character after emphasis if it could break parsing.
|
|
799
|
+
* For example, "Heading" after "**Bold **" — the 'H' should be escaped
|
|
800
|
+
* if the trailing space was escaped.
|
|
801
|
+
*/
|
|
802
|
+
|
|
803
|
+
/**
|
|
804
|
+
* Trim leading/trailing hard breaks from inline content.
|
|
805
|
+
* Matches remark behavior where <br> at start/end of paragraph is dropped.
|
|
806
|
+
*/
|
|
807
|
+
function trimHardBreaks(content: string): string {
|
|
808
|
+
// Remove leading hard breaks
|
|
809
|
+
let result = content.replace(/^(\\\n)+/, "");
|
|
810
|
+
// Remove trailing hard breaks produced by `<br>`
|
|
811
|
+
result = result.replace(/(\\\n)+$/, "");
|
|
812
|
+
return result;
|
|
813
|
+
}
|