@seed-hypermedia/client 0.0.81 → 0.0.82
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/block-diff.mjs +1 -1
- package/dist/blocks-to-markdown.d.ts +16 -11
- package/dist/blocks-to-markdown.mjs +1 -1
- package/dist/{chunk-OZKQOI2X.mjs → chunk-3KLY2B27.mjs} +51 -4
- package/dist/{chunk-YH52VFVR.mjs → chunk-TET43O3D.mjs} +296 -178
- package/dist/chunk-XB6E7EVC.mjs +872 -0
- package/dist/index.mjs +3 -3
- package/dist/markdown-to-blocks.d.ts +27 -32
- package/dist/markdown-to-blocks.mjs +3 -1
- package/package.json +1 -1
- package/dist/chunk-H7L4TI6X.mjs +0 -705
|
@@ -3,166 +3,208 @@ import {
|
|
|
3
3
|
} from "./chunk-MZUGCZJ6.mjs";
|
|
4
4
|
|
|
5
5
|
// src/blocks-to-markdown.ts
|
|
6
|
-
|
|
6
|
+
import { stringify as stringifyYaml } from "yaml";
|
|
7
|
+
var FM_KEY_ORDER = [
|
|
7
8
|
"name",
|
|
8
9
|
"summary",
|
|
9
|
-
"displayAuthor",
|
|
10
|
-
"displayPublishTime",
|
|
11
10
|
"icon",
|
|
12
11
|
"cover",
|
|
12
|
+
"displayAuthor",
|
|
13
|
+
"displayPublishTime",
|
|
14
|
+
"schema",
|
|
15
|
+
"childrenSchema",
|
|
16
|
+
"schemaDefinition",
|
|
13
17
|
"siteUrl",
|
|
14
18
|
"layout",
|
|
15
|
-
"
|
|
16
|
-
"seedExperimentalHomeOrder",
|
|
19
|
+
"theme",
|
|
17
20
|
"contentWidth",
|
|
18
21
|
"childrenType",
|
|
22
|
+
"showOutline",
|
|
23
|
+
"showActivity",
|
|
24
|
+
"seedExperimentalLogo",
|
|
25
|
+
"seedExperimentalHomeOrder",
|
|
19
26
|
"importCategories",
|
|
20
27
|
"importTags"
|
|
21
28
|
];
|
|
22
|
-
|
|
29
|
+
function frontmatterKeyRank(key) {
|
|
30
|
+
const idx = FM_KEY_ORDER.indexOf(key);
|
|
31
|
+
return idx === -1 ? FM_KEY_ORDER.length : idx;
|
|
32
|
+
}
|
|
23
33
|
function emitFrontmatter(metadata) {
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const val = metadata[key];
|
|
33
|
-
if (val !== void 0) {
|
|
34
|
-
lines.push(`${key}: ${val}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (metadata.theme && typeof metadata.theme === "object") {
|
|
38
|
-
const entries = [];
|
|
39
|
-
if (metadata.theme.headerLayout !== void 0) {
|
|
40
|
-
entries.push(` headerLayout: ${JSON.stringify(metadata.theme.headerLayout)}`);
|
|
34
|
+
const entries = Object.entries(metadata || {}).filter(([, v]) => v !== void 0);
|
|
35
|
+
if (entries.length === 0) return "---\n---\n";
|
|
36
|
+
const body = stringifyYaml(Object.fromEntries(entries), {
|
|
37
|
+
lineWidth: 0,
|
|
38
|
+
sortMapEntries: (a, b) => {
|
|
39
|
+
const ka = String(a.key);
|
|
40
|
+
const kb = String(b.key);
|
|
41
|
+
return frontmatterKeyRank(ka) - frontmatterKeyRank(kb) || ka.localeCompare(kb);
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
});
|
|
44
|
+
return "---\n" + body.replace(/\n$/, "") + "\n---\n";
|
|
45
|
+
}
|
|
46
|
+
function commentJson(value) {
|
|
47
|
+
return JSON.stringify(sortKeysDeep(value)).replace(/--/g, "-\\u002d");
|
|
48
|
+
}
|
|
49
|
+
function sortKeysDeep(value) {
|
|
50
|
+
if (Array.isArray(value)) return value.map(sortKeysDeep);
|
|
51
|
+
if (value && typeof value === "object") {
|
|
52
|
+
const out = {};
|
|
53
|
+
for (const k of Object.keys(value).sort()) {
|
|
54
|
+
const v = value[k];
|
|
55
|
+
if (v !== void 0) out[k] = sortKeysDeep(v);
|
|
45
56
|
}
|
|
57
|
+
return out;
|
|
46
58
|
}
|
|
47
|
-
|
|
48
|
-
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
function blockComment(id, type, attrs) {
|
|
62
|
+
let s = `<!-- id:${id}`;
|
|
63
|
+
if (type) s += ` type:${type}`;
|
|
64
|
+
if (attrs && Object.keys(attrs).length) s += ` attrs:${commentJson(attrs)}`;
|
|
65
|
+
return s + " -->";
|
|
49
66
|
}
|
|
50
67
|
function idComment(id) {
|
|
51
|
-
return
|
|
68
|
+
return blockComment(id);
|
|
52
69
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
|
|
70
|
+
var NATIVE_TYPES = /* @__PURE__ */ new Set(["Paragraph", "Heading", "Code", "Math", "Image", "Table"]);
|
|
71
|
+
function isNativeAttribute(type, key, value, hasChildren) {
|
|
72
|
+
if (key === "childrenType") {
|
|
73
|
+
if (value === "Unordered" || value === "Ordered" || value === "Blockquote") return true;
|
|
74
|
+
if (value === "Group") return hasChildren;
|
|
75
|
+
return false;
|
|
57
76
|
}
|
|
58
|
-
|
|
77
|
+
if (type === "Code" && key === "language") return true;
|
|
78
|
+
return false;
|
|
59
79
|
}
|
|
60
80
|
function blocksToMarkdown(doc, options) {
|
|
61
81
|
const opts = { ipfsGateway: true, ...options };
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
const out = [emitFrontmatter(doc.metadata || {}).replace(/\n$/, "")];
|
|
83
|
+
renderNodes(doc.content || [], "", void 0, 0, opts, out);
|
|
84
|
+
return out.join("\n") + "\n";
|
|
85
|
+
}
|
|
86
|
+
var LIST_TYPES = /* @__PURE__ */ new Set(["Unordered", "Ordered", "Blockquote"]);
|
|
87
|
+
function markerFor(childrenType, index) {
|
|
88
|
+
if (childrenType === "Unordered") return "- ";
|
|
89
|
+
if (childrenType === "Ordered") return `${index + 1}. `;
|
|
90
|
+
if (childrenType === "Blockquote") return "> ";
|
|
91
|
+
return "";
|
|
92
|
+
}
|
|
93
|
+
function renderNodes(nodes, ind, childrenType, headingDepth, opts, out) {
|
|
94
|
+
const listy = LIST_TYPES.has(childrenType || "");
|
|
95
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
96
|
+
const node = nodes[i];
|
|
97
|
+
if (i > 0 && !listy) out.push("");
|
|
98
|
+
const marker = markerFor(childrenType, i);
|
|
99
|
+
const wroteHeading = renderNode(node, ind, marker, headingDepth, opts, out);
|
|
100
|
+
const next = nodes[i + 1];
|
|
101
|
+
if (wroteHeading && next && next.block.type !== "Heading") {
|
|
102
|
+
out.push(ind + `<!-- end:${node.block.id} -->`);
|
|
68
103
|
}
|
|
69
104
|
}
|
|
70
|
-
return lines.join("\n");
|
|
71
105
|
}
|
|
72
|
-
function
|
|
73
|
-
var _a;
|
|
106
|
+
function renderNode(node, ind, marker, headingDepth, opts, out) {
|
|
74
107
|
const block = node.block;
|
|
75
108
|
const children = node.children || [];
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
109
|
+
const type = block.type;
|
|
110
|
+
const text = block.text || "";
|
|
111
|
+
const link = block.link || "";
|
|
112
|
+
const attributes = block.attributes || {};
|
|
113
|
+
const childrenType = attributes.childrenType;
|
|
114
|
+
if (type === "Table") {
|
|
115
|
+
const extra2 = extraAttributes(type, attributes, false);
|
|
116
|
+
const contentInd2 = ind + " ".repeat(marker.length);
|
|
117
|
+
const table = tableToMarkdown(node, contentInd2, (t, a) => renderInline(t, a, { atLineStart: false }));
|
|
118
|
+
out.push(ind + marker + blockComment(block.id, table ? void 0 : "Table", extra2));
|
|
119
|
+
if (table) out.push(...table.split("\n"));
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
const extra = extraAttributes(type, attributes, children.length > 0);
|
|
123
|
+
const comment = blockComment(block.id, NATIVE_TYPES.has(type) ? void 0 : type, extra);
|
|
124
|
+
const contentInd = ind + " ".repeat(marker.length);
|
|
125
|
+
let wroteHeading = false;
|
|
126
|
+
let childInd = marker ? contentInd : ind + " ";
|
|
127
|
+
let childHeadingDepth = 0;
|
|
128
|
+
let shorthand = false;
|
|
129
|
+
switch (type) {
|
|
130
|
+
case "Paragraph": {
|
|
131
|
+
if (!text && children.length && LIST_TYPES.has(childrenType || "") && !marker) {
|
|
132
|
+
out.push(ind + comment);
|
|
133
|
+
shorthand = true;
|
|
134
|
+
childInd = ind;
|
|
135
|
+
} else if (!text) {
|
|
136
|
+
out.push(ind + marker + comment);
|
|
96
137
|
} else {
|
|
97
|
-
|
|
138
|
+
out.push(ind + marker + renderInline(text, block.annotations, { atLineStart: true }) + " " + comment);
|
|
98
139
|
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
function blockToMarkdown(block, depth, opts) {
|
|
104
|
-
var _a, _b;
|
|
105
|
-
const ind = indent(depth);
|
|
106
|
-
const b = block;
|
|
107
|
-
const text = b.text || "";
|
|
108
|
-
const link = b.link || "";
|
|
109
|
-
const annotations = b.annotations;
|
|
110
|
-
const id = b.id;
|
|
111
|
-
switch (block.type) {
|
|
112
|
-
case "Paragraph": {
|
|
113
|
-
const rendered = ind + applyAnnotations(text, annotations);
|
|
114
|
-
return appendIdToFirstLine(rendered, id);
|
|
140
|
+
break;
|
|
115
141
|
}
|
|
116
142
|
case "Heading": {
|
|
117
|
-
const level = Math.min(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
143
|
+
const level = Math.min(headingDepth + 1, 6);
|
|
144
|
+
out.push(
|
|
145
|
+
ind + marker + "#".repeat(level) + " " + renderInline(text, block.annotations, { atLineStart: false }) + " " + comment
|
|
146
|
+
);
|
|
147
|
+
if (level < 6) {
|
|
148
|
+
wroteHeading = !marker;
|
|
149
|
+
childInd = contentInd;
|
|
150
|
+
childHeadingDepth = headingDepth + 1;
|
|
151
|
+
} else {
|
|
152
|
+
childInd = contentInd + " ";
|
|
153
|
+
}
|
|
154
|
+
if (children.length) out.push("");
|
|
155
|
+
break;
|
|
121
156
|
}
|
|
122
157
|
case "Code": {
|
|
123
|
-
const lang =
|
|
124
|
-
|
|
158
|
+
const lang = attributes.language || "";
|
|
159
|
+
let fenceLen = 3;
|
|
160
|
+
for (const m of Array.from(text.matchAll(/`+/g))) fenceLen = Math.max(fenceLen, m[0].length + 1);
|
|
161
|
+
const fence = "`".repeat(fenceLen);
|
|
162
|
+
out.push(ind + marker + fence + lang + " " + comment);
|
|
163
|
+
for (const line of text.split("\n")) out.push(line ? contentInd + line : "");
|
|
164
|
+
out.push(contentInd + fence);
|
|
165
|
+
break;
|
|
125
166
|
}
|
|
126
167
|
case "Math": {
|
|
127
|
-
|
|
168
|
+
out.push(ind + marker + "$$ " + comment);
|
|
169
|
+
for (const line of text.split("\n")) out.push(line ? contentInd + line : "");
|
|
170
|
+
out.push(contentInd + "$$");
|
|
171
|
+
break;
|
|
128
172
|
}
|
|
129
173
|
case "Image": {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
case "Video": {
|
|
135
|
-
const videoUrl = formatMediaUrl(link, opts.ipfsGateway);
|
|
136
|
-
return ind + `[Video](${videoUrl}) ${idComment(id)}`;
|
|
137
|
-
}
|
|
138
|
-
case "File": {
|
|
139
|
-
const fileName = ((_b = b.attributes) == null ? void 0 : _b.name) || "file";
|
|
140
|
-
const fileUrl = formatMediaUrl(link, opts.ipfsGateway);
|
|
141
|
-
return ind + `[${fileName}](${fileUrl}) ${idComment(id)}`;
|
|
142
|
-
}
|
|
143
|
-
case "Embed": {
|
|
144
|
-
return ind + `> [Embed: ${link}](${link}) ${idComment(id)}`;
|
|
145
|
-
}
|
|
146
|
-
case "WebEmbed": {
|
|
147
|
-
return ind + `[Web Embed](${link}) ${idComment(id)}`;
|
|
148
|
-
}
|
|
149
|
-
case "Button": {
|
|
150
|
-
const buttonText = text || "Button";
|
|
151
|
-
return ind + `[${buttonText}](${link}) ${idComment(id)}`;
|
|
152
|
-
}
|
|
153
|
-
case "Query": {
|
|
154
|
-
return ind + `<!-- Query block --> ${idComment(id)}`;
|
|
155
|
-
}
|
|
156
|
-
case "Nostr": {
|
|
157
|
-
return ind + `[Nostr: ${link}](${link}) ${idComment(id)}`;
|
|
174
|
+
const alt = renderInline(text, block.annotations, { atLineStart: false });
|
|
175
|
+
out.push(ind + marker + `)}) ` + comment);
|
|
176
|
+
break;
|
|
158
177
|
}
|
|
159
178
|
default: {
|
|
160
|
-
|
|
161
|
-
|
|
179
|
+
const url = renderUrl(formatMediaUrl(link, opts.ipfsGateway));
|
|
180
|
+
let visible = "";
|
|
181
|
+
if (text && link) visible = `[${renderInline(text, block.annotations, { atLineStart: false })}](${url})`;
|
|
182
|
+
else if (link) visible = `<${formatMediaUrl(link, opts.ipfsGateway)}>`;
|
|
183
|
+
else if (text) visible = renderInline(text, block.annotations, { atLineStart: true });
|
|
184
|
+
out.push(ind + marker + (visible ? visible + " " : "") + comment);
|
|
185
|
+
if (!visible && children.length && LIST_TYPES.has(childrenType || "") && !marker) {
|
|
186
|
+
shorthand = true;
|
|
187
|
+
childInd = ind;
|
|
162
188
|
}
|
|
163
|
-
return "";
|
|
164
189
|
}
|
|
165
190
|
}
|
|
191
|
+
if (children.length) {
|
|
192
|
+
if (!shorthand && type !== "Heading" && !LIST_TYPES.has(childrenType || "")) out.push("");
|
|
193
|
+
renderNodes(children, childInd, childrenType, childHeadingDepth, opts, out);
|
|
194
|
+
}
|
|
195
|
+
return wroteHeading;
|
|
196
|
+
}
|
|
197
|
+
function extraAttributes(type, attributes, hasChildren) {
|
|
198
|
+
const extra = {};
|
|
199
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
200
|
+
if (value === void 0 || value === null) continue;
|
|
201
|
+
if (isNativeAttribute(type, key, value, hasChildren)) continue;
|
|
202
|
+
extra[key] = value;
|
|
203
|
+
}
|
|
204
|
+
return extra;
|
|
205
|
+
}
|
|
206
|
+
function renderUrl(url) {
|
|
207
|
+
return /[\s()<>]/.test(url) ? `<${url}>` : url;
|
|
166
208
|
}
|
|
167
209
|
function normalizeTableNode(node) {
|
|
168
210
|
var _a, _b;
|
|
@@ -171,7 +213,8 @@ function normalizeTableNode(node) {
|
|
|
171
213
|
for (const child of node.children || []) {
|
|
172
214
|
const block = child.block;
|
|
173
215
|
if ((block == null ? void 0 : block.type) === "TableColumn") {
|
|
174
|
-
|
|
216
|
+
const attributes = extraAttributes("TableColumn", block.attributes || {}, false);
|
|
217
|
+
columns.push(Object.keys(attributes).length ? { id: block.id, attributes } : { id: block.id });
|
|
175
218
|
} else if ((block == null ? void 0 : block.type) === "TableRow") {
|
|
176
219
|
const cells = /* @__PURE__ */ new Map();
|
|
177
220
|
for (const cellNode of child.children || []) {
|
|
@@ -194,12 +237,12 @@ function normalizeTableNode(node) {
|
|
|
194
237
|
function escapeCellText(s) {
|
|
195
238
|
return s.replace(/\|/g, "\\|").replace(/\n/g, "<br>");
|
|
196
239
|
}
|
|
197
|
-
function assembleTableMarkdown(
|
|
240
|
+
function assembleTableMarkdown(columns, header, bodyRows, ind) {
|
|
198
241
|
const lines = [];
|
|
199
|
-
lines.push(ind + idComment(tableId));
|
|
200
242
|
const headerCells = columns.map((col, idx) => {
|
|
201
243
|
const text = header.cellTexts[idx] || "";
|
|
202
|
-
|
|
244
|
+
const attrs = col.attributes && Object.keys(col.attributes).length ? ` attrs:${commentJson(col.attributes)}` : "";
|
|
245
|
+
return (text ? text + " " : "") + `<!-- col:${col.id}${attrs} -->`;
|
|
203
246
|
});
|
|
204
247
|
if (header.rowId) {
|
|
205
248
|
headerCells[headerCells.length - 1] += " " + idComment(header.rowId);
|
|
@@ -214,7 +257,7 @@ function assembleTableMarkdown(tableId, columns, header, bodyRows, ind) {
|
|
|
214
257
|
}
|
|
215
258
|
return lines.join("\n");
|
|
216
259
|
}
|
|
217
|
-
function tableToMarkdown(node,
|
|
260
|
+
function tableToMarkdown(node, ind, renderCell) {
|
|
218
261
|
var _a;
|
|
219
262
|
const { columns, rows } = normalizeTableNode(node);
|
|
220
263
|
if (columns.length === 0) return "";
|
|
@@ -227,13 +270,7 @@ function tableToMarkdown(node, depth, renderCell) {
|
|
|
227
270
|
rowId: row.id,
|
|
228
271
|
cellTexts: renderRowCells(row)
|
|
229
272
|
}));
|
|
230
|
-
return assembleTableMarkdown(
|
|
231
|
-
node.block.id,
|
|
232
|
-
columns,
|
|
233
|
-
{ rowId: headerRow == null ? void 0 : headerRow.id, cellTexts: renderRowCells(headerRow) },
|
|
234
|
-
bodyRows,
|
|
235
|
-
indent(depth)
|
|
236
|
-
);
|
|
273
|
+
return assembleTableMarkdown(columns, { rowId: headerRow == null ? void 0 : headerRow.id, cellTexts: renderRowCells(headerRow) }, bodyRows, ind);
|
|
237
274
|
}
|
|
238
275
|
async function tableToResolvedMarkdown(node, depth, renderCell) {
|
|
239
276
|
var _a;
|
|
@@ -252,68 +289,152 @@ async function tableToResolvedMarkdown(node, depth, renderCell) {
|
|
|
252
289
|
cellTexts: await renderRowCells(row)
|
|
253
290
|
}))
|
|
254
291
|
);
|
|
255
|
-
return assembleTableMarkdown(
|
|
256
|
-
node.block.id,
|
|
292
|
+
return resolvedIndent(depth) + resolvedIdComment(node.block.id) + "\n" + assembleTableMarkdown(
|
|
257
293
|
columns,
|
|
258
294
|
{ rowId: headerRow == null ? void 0 : headerRow.id, cellTexts: await renderRowCells(headerRow) },
|
|
259
295
|
bodyRows,
|
|
260
|
-
|
|
296
|
+
resolvedIndent(depth)
|
|
261
297
|
);
|
|
262
298
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
299
|
+
var SPAN_STYLE = {
|
|
300
|
+
TextColor: "color",
|
|
301
|
+
BackgroundColor: "background-color",
|
|
302
|
+
TextSize: "font-size",
|
|
303
|
+
TextFamily: "font-family"
|
|
304
|
+
};
|
|
305
|
+
function encodeHtmlAttr(s) {
|
|
306
|
+
return s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");
|
|
307
|
+
}
|
|
308
|
+
function escapeInline(s, atLineStart) {
|
|
309
|
+
let out = s.replace(/[\\`*_~\[\]<]/g, (c) => "\\" + c).replace(/\n/g, "<br>");
|
|
310
|
+
if (atLineStart) {
|
|
311
|
+
out = out.replace(/^(#|[-+>|$]|\d+[.)])/, (m) => m.length === 1 ? "\\" + m : m.slice(0, -1) + "\\" + m.slice(-1));
|
|
312
|
+
if (/^\s/.test(out)) out = "\\" + out;
|
|
313
|
+
}
|
|
314
|
+
return out;
|
|
315
|
+
}
|
|
316
|
+
function escapeTrailingWhitespace(s) {
|
|
317
|
+
return /\s$/.test(s) ? s.slice(0, -1) + "\\" + s.slice(-1) : s;
|
|
318
|
+
}
|
|
319
|
+
function spansFromAnnotations(text, annotations) {
|
|
320
|
+
const spans = [];
|
|
321
|
+
for (const ann of annotations || []) {
|
|
322
|
+
const a = ann;
|
|
323
|
+
const starts = a.starts || [];
|
|
324
|
+
const ends = a.ends || [];
|
|
325
|
+
const link = a.link || void 0;
|
|
326
|
+
const attributes = a.attributes && Object.keys(a.attributes).length ? a.attributes : void 0;
|
|
327
|
+
const key = a.type + " " + (link ?? "") + " " + (attributes ? commentJson(attributes) : "");
|
|
271
328
|
for (let i = 0; i < starts.length; i++) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
329
|
+
const start = Math.max(0, starts[i]);
|
|
330
|
+
const end = Math.min(text.length, ends[i] ?? -1);
|
|
331
|
+
if (end <= start) continue;
|
|
332
|
+
spans.push({ start, end, type: a.type, link, attributes, key });
|
|
276
333
|
}
|
|
277
334
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
335
|
+
spans.sort((x, y) => x.key.localeCompare(y.key) || x.start - y.start || x.end - y.end);
|
|
336
|
+
const merged = [];
|
|
337
|
+
for (const s of spans) {
|
|
338
|
+
const last = merged[merged.length - 1];
|
|
339
|
+
if (last && last.key === s.key && s.start <= last.end) last.end = Math.max(last.end, s.end);
|
|
340
|
+
else merged.push({ ...s });
|
|
341
|
+
}
|
|
342
|
+
return merged;
|
|
343
|
+
}
|
|
344
|
+
function spanPriority(s) {
|
|
345
|
+
if (s.type === "Embed") return 2;
|
|
346
|
+
if (s.type === "Code") return 1;
|
|
347
|
+
return 0;
|
|
348
|
+
}
|
|
349
|
+
function renderInline(text, annotations, options) {
|
|
350
|
+
const spans = spansFromAnnotations(text, annotations);
|
|
351
|
+
if (spans.length === 0) return escapeTrailingWhitespace(escapeInline(text, options.atLineStart));
|
|
352
|
+
const boundaries = /* @__PURE__ */ new Set([0, text.length]);
|
|
353
|
+
for (const s of spans) {
|
|
354
|
+
boundaries.add(s.start);
|
|
355
|
+
boundaries.add(s.end);
|
|
356
|
+
}
|
|
357
|
+
const points = Array.from(boundaries).sort((a, b) => a - b);
|
|
358
|
+
const stack = [];
|
|
359
|
+
let out = "";
|
|
360
|
+
for (let p = 0; p < points.length - 1; p++) {
|
|
361
|
+
const pos = points[p];
|
|
362
|
+
const next = points[p + 1];
|
|
363
|
+
const active = spans.filter((s) => s.start <= pos && s.end >= next);
|
|
364
|
+
const survivors = [];
|
|
365
|
+
let firstInactive = stack.findIndex((o) => !active.includes(o.span));
|
|
366
|
+
if (firstInactive === -1) firstInactive = stack.length;
|
|
367
|
+
while (stack.length > firstInactive) {
|
|
368
|
+
const top2 = stack.pop();
|
|
369
|
+
out += top2.close;
|
|
370
|
+
if (active.includes(top2.span)) survivors.unshift(top2.span);
|
|
371
|
+
}
|
|
372
|
+
const opening = [...survivors, ...active.filter((s) => !stack.some((o) => o.span === s) && !survivors.includes(s))];
|
|
373
|
+
opening.sort(
|
|
374
|
+
(a, b) => spanPriority(a) - spanPriority(b) || b.end - a.end || a.start - b.start || a.type.localeCompare(b.type)
|
|
375
|
+
);
|
|
376
|
+
const inner = stack.filter((o) => spanPriority(o.span) > 0);
|
|
377
|
+
if (opening.length && inner.length) {
|
|
378
|
+
for (const o of inner.reverse()) {
|
|
379
|
+
out += o.close;
|
|
380
|
+
stack.splice(stack.indexOf(o), 1);
|
|
381
|
+
opening.push(o.span);
|
|
382
|
+
}
|
|
383
|
+
opening.sort(
|
|
384
|
+
(a, b) => spanPriority(a) - spanPriority(b) || b.end - a.end || a.start - b.start || a.type.localeCompare(b.type)
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
for (const s of opening) {
|
|
388
|
+
const chunk2 = text.slice(pos, next);
|
|
389
|
+
const [open, close] = markersFor(s, chunk2, text);
|
|
390
|
+
out += open;
|
|
391
|
+
stack.push({ span: s, close });
|
|
392
|
+
}
|
|
393
|
+
const top = stack[stack.length - 1];
|
|
394
|
+
const chunk = text.slice(pos, next);
|
|
395
|
+
if ((top == null ? void 0 : top.span.type) === "Embed") {
|
|
396
|
+
} else if ((top == null ? void 0 : top.span.type) === "Code") {
|
|
397
|
+
out += chunk.replace(/\n/g, "<br>");
|
|
398
|
+
} else {
|
|
399
|
+
out += escapeInline(chunk, options.atLineStart && out === "");
|
|
400
|
+
}
|
|
288
401
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return result;
|
|
402
|
+
while (stack.length) out += stack.pop().close;
|
|
403
|
+
return escapeTrailingWhitespace(out);
|
|
292
404
|
}
|
|
293
|
-
function
|
|
294
|
-
|
|
405
|
+
function markersFor(s, chunk, fullText) {
|
|
406
|
+
var _a;
|
|
407
|
+
switch (s.type) {
|
|
295
408
|
case "Bold":
|
|
296
|
-
return "**";
|
|
409
|
+
return ["**", "**"];
|
|
297
410
|
case "Italic":
|
|
298
|
-
return "_";
|
|
411
|
+
return ["_", "_"];
|
|
299
412
|
case "Strike":
|
|
300
|
-
return "~~";
|
|
301
|
-
case "Code":
|
|
302
|
-
return "`";
|
|
413
|
+
return ["~~", "~~"];
|
|
303
414
|
case "Underline":
|
|
304
|
-
return
|
|
415
|
+
return ["<u>", "</u>"];
|
|
416
|
+
case "Range":
|
|
417
|
+
return ["<mark>", "</mark>"];
|
|
305
418
|
case "Link":
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
309
|
-
|
|
419
|
+
return ["[", `](${renderUrl(s.link || "")})`];
|
|
420
|
+
case "Embed":
|
|
421
|
+
return [`<${s.link || ""}>`, ""];
|
|
422
|
+
case "Code": {
|
|
423
|
+
let fenceLen = 1;
|
|
424
|
+
for (const m of Array.from(fullText.slice(s.start, s.end).matchAll(/`+/g)))
|
|
425
|
+
fenceLen = Math.max(fenceLen, m[0].length + 1);
|
|
426
|
+
const fence = "`".repeat(fenceLen);
|
|
427
|
+
const pad = /^[` ]|[` ]$/.test(chunk) ? " " : "";
|
|
428
|
+
return [fence + pad, pad + fence];
|
|
429
|
+
}
|
|
430
|
+
default: {
|
|
431
|
+
const prop = SPAN_STYLE[s.type];
|
|
432
|
+
if (prop) {
|
|
433
|
+
const value = String(((_a = s.attributes) == null ? void 0 : _a.value) ?? "");
|
|
434
|
+
return [`<span style="${prop}:${encodeHtmlAttr(value)}">`, "</span>"];
|
|
310
435
|
}
|
|
311
|
-
|
|
312
|
-
const link = "link" in ann ? ann.link || "" : "";
|
|
313
|
-
return type === "open" ? `<${link}` : ">";
|
|
436
|
+
return ["", ""];
|
|
314
437
|
}
|
|
315
|
-
default:
|
|
316
|
-
return "";
|
|
317
438
|
}
|
|
318
439
|
}
|
|
319
440
|
function formatMediaUrl(url, useGateway) {
|
|
@@ -323,9 +444,6 @@ function formatMediaUrl(url, useGateway) {
|
|
|
323
444
|
}
|
|
324
445
|
return url;
|
|
325
446
|
}
|
|
326
|
-
function indent(depth) {
|
|
327
|
-
return " ".repeat(depth);
|
|
328
|
-
}
|
|
329
447
|
function slugify(title) {
|
|
330
448
|
return title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 60);
|
|
331
449
|
}
|