@threadlabs/looma 0.45.1 → 0.46.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.
@@ -1,562 +0,0 @@
1
- import { LOOMA_CONTAINER_NAMES, LOOMA_MENTION_HANDLE, applyLoomaCodeNotation, assignLoomaHeadingIds, normalizeLoomaCalloutTone, normalizeLoomaChipTone, parseLoomaAttributes, parseLoomaFenceInfo, readLoomaAttributes, splitLoomaFrontmatter } from "./index.js";
2
- import MarkdownIt from "markdown-it";
3
- //#region src/markdown/markdown-it.ts
4
- var CONTAINER_OPEN = /^(:{3,})[ \t]*([A-Za-z][\w-]*)(?:[ \t]+(.*?))?[ \t]*$/;
5
- var CONTAINER_CLOSE = /^:{3,}[ \t]*$/;
6
- var FENCE_OPEN = /^(`{3,}|~{3,})/;
7
- var TAB_LABEL = /^==[ \t]+(\S.*?)[ \t]*$/;
8
- var OPEN_FLAG = /[ \t]*\{open\}$/;
9
- var ALERT = /^\[!(NOTE|TIP|IMPORTANT|WARNING|CAUTION)\][ \t]*(?:\n|$)/i;
10
- var LEAF = /^::([A-Za-z][\w-]*)(\{.*\})?[ \t]*$/;
11
- var TASK = /^\[([ xX])\](?:[ \t]+|$)/;
12
- var TOC_FORMATS = /* @__PURE__ */ new Set([
13
- "plain",
14
- "bulleted",
15
- "numbered"
16
- ]);
17
- var PLACEMENTS = /* @__PURE__ */ new Set([
18
- "block",
19
- "center",
20
- "wrap-left",
21
- "wrap-right"
22
- ]);
23
- var opensContainer = (text) => {
24
- const match = CONTAINER_OPEN.exec(text);
25
- return match && LOOMA_CONTAINER_NAMES.has(match[2].toLowerCase()) ? match : null;
26
- };
27
- function lineText(state, line) {
28
- return state.src.slice(state.bMarks[line] + state.tShift[line], state.eMarks[line]);
29
- }
30
- function frontmatter(state, startLine, _endLine, silent) {
31
- if (startLine !== 0 || state.blkIndent !== 0) return false;
32
- const split = splitLoomaFrontmatter(state.src);
33
- if (!split) return false;
34
- if (silent) return true;
35
- const lines = split.source.split("\n").length;
36
- const token = state.push("looma_frontmatter", "", 0);
37
- Object.assign(token, {
38
- content: split.yaml,
39
- block: true,
40
- hidden: true,
41
- map: [0, lines]
42
- });
43
- state.env.frontmatter = split.yaml;
44
- state.line = lines;
45
- return true;
46
- }
47
- function container(state, startLine, endLine, silent) {
48
- if (state.sCount[startLine] - state.blkIndent >= 4) return false;
49
- const match = opensContainer(lineText(state, startLine));
50
- if (!match) return false;
51
- if (silent) return true;
52
- const name = match[2].toLowerCase();
53
- const grouped = name === "tabs" || name === "code-group";
54
- const labels = [];
55
- let depth = 1;
56
- let fence = null;
57
- let next = startLine + 1;
58
- for (; next < endLine; next++) {
59
- const text = lineText(state, next);
60
- if (text && state.sCount[next] < state.blkIndent) break;
61
- if (fence) {
62
- const close = FENCE_OPEN.exec(text);
63
- if (close && close[1][0] === fence.char && close[1].length >= fence.length && !text.slice(close[1].length).trim()) fence = null;
64
- continue;
65
- }
66
- const opener = FENCE_OPEN.exec(text);
67
- if (opener) {
68
- fence = {
69
- char: opener[1][0],
70
- length: opener[1].length
71
- };
72
- continue;
73
- }
74
- if (CONTAINER_CLOSE.test(text)) {
75
- if (--depth === 0) break;
76
- } else if (opensContainer(text)) depth++;
77
- else if (grouped && depth === 1) {
78
- const label = TAB_LABEL.exec(text);
79
- if (label) labels.push({
80
- line: next,
81
- label: label[1]
82
- });
83
- }
84
- }
85
- let title = (match[3] ?? "").trim();
86
- const open = name === "details" && OPEN_FLAG.test(title);
87
- if (name === "details") title = title.replace(OPEN_FLAG, "");
88
- const parentType = state.parentType;
89
- const lineMax = state.lineMax;
90
- state.parentType = "container";
91
- state.lineMax = next;
92
- const opening = state.push("looma_container_open", "div", 1);
93
- Object.assign(opening, {
94
- info: name,
95
- meta: {
96
- title,
97
- open
98
- },
99
- block: true,
100
- map: [startLine, next],
101
- markup: match[1]
102
- });
103
- const panel = (label, from, to) => {
104
- state.push("looma_tab_open", "section", 1).meta = { label };
105
- state.md.block.tokenize(state, from, to);
106
- state.push("looma_tab_close", "section", -1);
107
- };
108
- if (labels.length) {
109
- state.md.block.tokenize(state, startLine + 1, labels[0].line);
110
- labels.forEach(({ line, label }, index) => panel(label, line + 1, labels[index + 1]?.line ?? next));
111
- } else {
112
- const first = state.tokens.length;
113
- state.md.block.tokenize(state, startLine + 1, next);
114
- if (name === "code-group") for (let index = state.tokens.length - 1; index >= first; index--) {
115
- const token = state.tokens[index];
116
- if (token.type !== "fence" || token.level !== opening.level + 1) continue;
117
- const { title: fenceTitle, language } = parseLoomaFenceInfo(token.info);
118
- const tabOpen = new state.Token("looma_tab_open", "section", 1);
119
- tabOpen.meta = { label: fenceTitle ?? language ?? "Code" };
120
- state.tokens.splice(index, 1, tabOpen, token, new state.Token("looma_tab_close", "section", -1));
121
- }
122
- }
123
- const closing = state.push("looma_container_close", "div", -1);
124
- Object.assign(closing, {
125
- info: name,
126
- meta: { title },
127
- block: true,
128
- markup: match[1]
129
- });
130
- state.parentType = parentType;
131
- state.lineMax = lineMax;
132
- state.line = Math.min(next + 1, endLine);
133
- return true;
134
- }
135
- function leaf(state, startLine, _endLine, silent) {
136
- if (state.sCount[startLine] - state.blkIndent >= 4) return false;
137
- const match = LEAF.exec(lineText(state, startLine));
138
- if (!match || match[1].toLowerCase() !== "toc") return false;
139
- const attributes = match[2] ? parseLoomaAttributes(match[2]) : {};
140
- if (!attributes) return false;
141
- if (silent) return true;
142
- const format = typeof attributes.format === "string" && TOC_FORMATS.has(attributes.format) ? attributes.format : "plain";
143
- const depth = attributes.depth === "1" || attributes.depth === "2" ? Number(attributes.depth) : 3;
144
- const token = state.push("looma_toc", "nav", 0);
145
- Object.assign(token, {
146
- block: true,
147
- map: [startLine, startLine + 1],
148
- meta: {
149
- format,
150
- depth
151
- }
152
- });
153
- state.line = startLine + 1;
154
- return true;
155
- }
156
- function alerts(state) {
157
- const tokens = state.tokens;
158
- for (let index = 0; index < tokens.length; index++) {
159
- if (tokens[index].type !== "blockquote_open" || tokens[index + 1]?.type !== "paragraph_open") continue;
160
- const inline = tokens[index + 2];
161
- const match = inline?.type === "inline" ? ALERT.exec(inline.content) : null;
162
- if (!match) continue;
163
- inline.content = inline.content.slice(match[0].length);
164
- if (!inline.content.trim()) tokens.splice(index + 1, 3);
165
- tokens[index].tag = "ui-callout";
166
- tokens[index].attrSet("tone", normalizeLoomaCalloutTone(match[1]));
167
- for (let depth = 0, end = index; end < tokens.length; end++) if (tokens[end].type === "blockquote_open") depth++;
168
- else if (tokens[end].type === "blockquote_close" && --depth === 0) {
169
- tokens[end].tag = "ui-callout";
170
- break;
171
- }
172
- }
173
- }
174
- function taskLists(state) {
175
- const tokens = state.tokens;
176
- for (let index = 0; index < tokens.length; index++) {
177
- if (tokens[index].type !== "bullet_list_open") continue;
178
- const level = tokens[index].level;
179
- const items = [];
180
- let isTaskList = true;
181
- for (let cursor = index + 1; cursor < tokens.length; cursor++) {
182
- const token = tokens[cursor];
183
- if (token.type === "bullet_list_close" && token.level === level) break;
184
- if (token.type !== "list_item_open" || token.level !== level + 1) continue;
185
- const inline = tokens[cursor + 2];
186
- const match = tokens[cursor + 1]?.type === "paragraph_open" && inline?.type === "inline" ? TASK.exec(inline.content) : null;
187
- if (!match) {
188
- isTaskList = false;
189
- break;
190
- }
191
- items.push({
192
- item: token,
193
- inline,
194
- checked: match[1] !== " "
195
- });
196
- }
197
- if (!isTaskList || !items.length) continue;
198
- tokens[index].attrSet("data-type", "taskList");
199
- for (const { item, inline, checked } of items) {
200
- inline.content = inline.content.replace(TASK, "");
201
- item.attrSet("data-type", "taskItem");
202
- item.attrSet("data-checked", String(checked));
203
- inline.meta = {
204
- ...inline.meta,
205
- task: checked
206
- };
207
- }
208
- }
209
- }
210
- function directive(state, silent) {
211
- const { src, pos } = state;
212
- if (src.charCodeAt(pos) !== 58) return false;
213
- const name = /^:(chip)\[/.exec(src.slice(pos, pos + 10))?.[1];
214
- if (!name) return false;
215
- let label = "";
216
- let index = pos + name.length + 2;
217
- for (; index < state.posMax && src[index] !== "]"; index++) {
218
- if (src[index] === "\\" && index + 1 < state.posMax) index++;
219
- label += src[index];
220
- }
221
- if (src[index] !== "]") return false;
222
- const read = src[index + 1] === "{" ? readLoomaAttributes(src, index + 1) : {
223
- attributes: {},
224
- end: index + 1
225
- };
226
- if (!read || read.end > state.posMax) return false;
227
- if (!silent) {
228
- const token = state.push(`looma_${name}`, "span", 0);
229
- token.meta = {
230
- label,
231
- attributes: read.attributes
232
- };
233
- }
234
- state.pos = read.end;
235
- return true;
236
- }
237
- function mention(state, silent) {
238
- const { src, pos } = state;
239
- if (src.charCodeAt(pos) !== 64 || pos > 0 && !/\s/u.test(src[pos - 1])) return false;
240
- let index = pos + 1;
241
- let label = "";
242
- if (src[index] === "[") {
243
- for (index++; index < state.posMax && src[index] !== "]"; index++) {
244
- if (src[index] === "\\" && index + 1 < state.posMax) index++;
245
- label += src[index];
246
- }
247
- if (src[index] !== "]" || !label) return false;
248
- index++;
249
- } else {
250
- label = LOOMA_MENTION_HANDLE.exec(src.slice(index, state.posMax))?.[0] ?? "";
251
- if (!label) return false;
252
- index += label.length;
253
- }
254
- let ref = src[index] === "#" ? LOOMA_MENTION_HANDLE.exec(src.slice(index + 1, state.posMax))?.[0] : void 0;
255
- if (ref) index += ref.length + 1;
256
- const read = src[index] === "{" ? readLoomaAttributes(src, index) : null;
257
- if (read && read.end <= state.posMax) {
258
- if (typeof read.attributes.ref === "string") ref ??= read.attributes.ref;
259
- index = read.end;
260
- }
261
- if (!silent) state.push("looma_mention", "span", 0).meta = {
262
- label,
263
- ref
264
- };
265
- state.pos = index;
266
- return true;
267
- }
268
- function inlineText(token) {
269
- return (token?.children ?? []).map((child) => child.type === "image" ? inlineText(child) : child.content).join("");
270
- }
271
- function attributes(state) {
272
- const headings = [];
273
- const tokens = state.tokens;
274
- for (let index = 0; index < tokens.length; index++) {
275
- const token = tokens[index];
276
- if (token.type !== "inline" || !token.children) continue;
277
- const children = token.children;
278
- if (typeof token.meta?.task === "boolean") {
279
- const html = (content) => Object.assign(new state.Token("html_inline", "", 0), { content });
280
- children.unshift(html(`<label><input type="checkbox" disabled=""${token.meta.task ? " checked=\"\"" : ""}> `));
281
- children.push(html("</label>"));
282
- }
283
- for (let child = children.length - 3; child >= 0; child--) {
284
- const text = children[child + 1];
285
- if (children[child].type !== "link_open" || children[child].markup !== "linkify" || /^(?:[a-z][\w+.-]*:|www\.)/i.test(text.content)) continue;
286
- children.splice(child, 3, Object.assign(new state.Token("text", "", 0), { content: text.content }));
287
- }
288
- if (tokens[index - 1]?.type === "heading_open") {
289
- const last = children.at(-1);
290
- const marker = last?.type === "text" ? /[ \t]+(\{[^{}]*\})$/.exec(last.content) : null;
291
- const parsed = marker && parseLoomaAttributes(marker[1]);
292
- let id;
293
- if (last && marker && parsed && typeof parsed.id === "string") {
294
- id = parsed.id;
295
- last.content = last.content.slice(0, marker.index);
296
- }
297
- headings.push({
298
- open: tokens[index - 1],
299
- text: inlineText(token).trim(),
300
- id
301
- });
302
- }
303
- for (let child = 0; child < children.length; child++) {
304
- const next = children[child + 1];
305
- if (children[child].type !== "image" || next?.type !== "text" || !next.content.startsWith("{")) continue;
306
- const read = readLoomaAttributes(next.content);
307
- if (!read) continue;
308
- applyImageAttributes(children[child], read.attributes);
309
- next.content = next.content.slice(read.end);
310
- if (!next.content) children.splice(child + 1, 1);
311
- }
312
- if (tokens[index - 1]?.type === "paragraph_open" && children.length === 1 && children[0].type === "image") {
313
- tokens[index - 1].hidden = true;
314
- tokens[index + 1].hidden = true;
315
- }
316
- }
317
- const ids = assignLoomaHeadingIds(headings);
318
- headings.forEach(({ open }, index) => open.attrSet("id", ids[index]));
319
- }
320
- function applyImageAttributes(image, attributes) {
321
- for (const key of ["width", "height"]) {
322
- const value = Number(attributes[key]);
323
- if (Number.isSafeInteger(value) && value > 0) image.attrSet(key, String(value));
324
- }
325
- const placement = attributes.placement;
326
- if (typeof placement === "string" && PLACEMENTS.has(placement) && placement !== "block") image.attrSet("data-placement", placement);
327
- if (attributes.responsive === true) image.attrSet("data-looma-responsive", "");
328
- }
329
- var FOOTNOTE_DEFINITION = /^\[\^([^\s\]]+)\]:/;
330
- var FOOTNOTE_REFERENCE = /^\[\^([^\s\]]+)\]/;
331
- function footnoteDefinition(state, startLine, endLine, silent) {
332
- if (state.sCount[startLine] - state.blkIndent >= 4) return false;
333
- const start = state.bMarks[startLine] + state.tShift[startLine];
334
- const match = FOOTNOTE_DEFINITION.exec(state.src.slice(start, state.eMarks[startLine]));
335
- if (!match) return false;
336
- if (silent) return true;
337
- (state.env.footnotes ??= /* @__PURE__ */ new Set()).add(match[1]);
338
- const open = state.push("looma_footnote_open", "li", 1);
339
- Object.assign(open, {
340
- meta: { label: match[1] },
341
- block: true,
342
- map: [startLine, startLine]
343
- });
344
- const saved = {
345
- bMark: state.bMarks[startLine],
346
- tShift: state.tShift[startLine],
347
- sCount: state.sCount[startLine],
348
- parentType: state.parentType
349
- };
350
- let position = start + match[0].length;
351
- const contentStart = position;
352
- let offset = state.sCount[startLine] + match[0].length;
353
- const initial = offset;
354
- while (position < state.eMarks[startLine] && (state.src[position] === " " || state.src[position] === " ")) {
355
- offset += state.src[position] === " " ? 4 - (offset + state.bsCount[startLine]) % 4 : 1;
356
- position++;
357
- }
358
- state.tShift[startLine] = position - contentStart;
359
- state.sCount[startLine] = offset - initial;
360
- state.bMarks[startLine] = contentStart;
361
- state.blkIndent += 4;
362
- state.parentType = "footnote";
363
- if (state.sCount[startLine] < state.blkIndent) state.sCount[startLine] += state.blkIndent;
364
- state.md.block.tokenize(state, startLine, endLine);
365
- state.parentType = saved.parentType;
366
- state.blkIndent -= 4;
367
- state.tShift[startLine] = saved.tShift;
368
- state.sCount[startLine] = saved.sCount;
369
- state.bMarks[startLine] = saved.bMark;
370
- open.map[1] = state.line;
371
- state.push("looma_footnote_close", "li", -1).block = true;
372
- return true;
373
- }
374
- function footnoteReference(state, silent) {
375
- if (state.src.charCodeAt(state.pos) !== 91 || state.src.charCodeAt(state.pos + 1) !== 94) return false;
376
- const match = FOOTNOTE_REFERENCE.exec(state.src.slice(state.pos, state.posMax));
377
- if (!match || !state.env.footnotes?.has(match[1])) return false;
378
- if (!silent) state.push("looma_footnote_ref", "sup", 0).meta = { label: match[1] };
379
- state.pos += match[0].length;
380
- return true;
381
- }
382
- function footnotes(state) {
383
- const definitions = /* @__PURE__ */ new Map();
384
- const body = [];
385
- for (let index = 0; index < state.tokens.length; index++) {
386
- const token = state.tokens[index];
387
- if (token.type !== "looma_footnote_open") {
388
- body.push(token);
389
- continue;
390
- }
391
- let depth = 0, end = index;
392
- for (; end < state.tokens.length; end++) if (state.tokens[end].type === "looma_footnote_open") depth++;
393
- else if (state.tokens[end].type === "looma_footnote_close" && --depth === 0) break;
394
- const label = token.meta.label;
395
- if (!definitions.has(label)) definitions.set(label, state.tokens.slice(index, end + 1));
396
- index = end;
397
- }
398
- if (!definitions.size) return;
399
- const order = [];
400
- const counts = /* @__PURE__ */ new Map();
401
- const number = (tokens) => {
402
- for (const token of tokens) for (const child of token.children ?? []) {
403
- if (child.type !== "looma_footnote_ref") continue;
404
- const label = child.meta.label;
405
- if (!counts.has(label)) order.push(label);
406
- const occurrence = (counts.get(label) ?? 0) + 1;
407
- counts.set(label, occurrence);
408
- child.meta = {
409
- label,
410
- number: order.indexOf(label) + 1,
411
- occurrence
412
- };
413
- }
414
- };
415
- number(body);
416
- const notes = [];
417
- for (let index = 0; index < order.length; index++) {
418
- const label = order[index];
419
- const tokens = definitions.get(label);
420
- number(tokens);
421
- const backrefs = Object.assign(new state.Token("looma_footnote_backrefs", "", 0), { meta: {
422
- label,
423
- number: index + 1
424
- } });
425
- const last = tokens.length - 2;
426
- if (tokens[last].type === "paragraph_close") tokens.splice(last, 0, backrefs);
427
- else tokens.splice(last + 1, 0, new state.Token("paragraph_open", "p", 1), backrefs, new state.Token("paragraph_close", "p", -1));
428
- notes.push(...tokens);
429
- }
430
- if (!notes.length) {
431
- state.tokens = body;
432
- return;
433
- }
434
- for (const note of notes) if (note.type === "looma_footnote_open") note.meta.count = counts.get(note.meta.label);
435
- state.tokens = [
436
- ...body,
437
- Object.assign(new state.Token("looma_footnotes_open", "section", 1), { block: true }),
438
- ...notes,
439
- Object.assign(new state.Token("looma_footnotes_close", "section", -1), { block: true })
440
- ];
441
- }
442
- function tableOfContents(tokens, format, depth, escape) {
443
- const roots = [];
444
- const ancestors = [];
445
- tokens.forEach((token, index) => {
446
- if (token.type !== "heading_open") return;
447
- const level = Number(token.tag.slice(1));
448
- const text = inlineText(tokens[index + 1]).trim();
449
- if (level > depth || !text) return;
450
- while (ancestors.length && ancestors[ancestors.length - 1].level >= level) ancestors.pop();
451
- const entry = {
452
- id: token.attrGet("id") ?? "",
453
- text,
454
- children: []
455
- };
456
- (ancestors.at(-1)?.entry.children ?? roots).push(entry);
457
- ancestors.push({
458
- level,
459
- entry
460
- });
461
- });
462
- const [list, item] = format === "plain" ? ["div", "div"] : [format === "numbered" ? "ol" : "ul", "li"];
463
- const render = (entries) => `<${list} role="list">${entries.map(({ id, text, children }) => `<${item} role="listitem"><a href="#${escape(id)}">${escape(text)}</a>${children.length ? render(children) : ""}</${item}>`).join("")}</${list}>`;
464
- return `<nav data-looma-toc="" data-format="${format}" data-depth="${depth}" aria-label="Table of contents"><strong>Table of contents</strong>${roots.length ? render(roots) : ""}</nav>\n`;
465
- }
466
- /**
467
- * Adds Looma Markdown to a markdown-it instance: containers, alerts, task lists, footnotes, the table of
468
- * contents, chips, mentions, brace attributes, heading anchors, code meta and front matter.
469
- * @contract Renders the canonical HTML, which is the HTML LoomaEditor saves. It is not sanitized.
470
- * Linkify and raw HTML are instance options; `createLoomaMarkdownIt` turns both on.
471
- */
472
- function loomaMarkdown(md, { codeNotation = "apply" } = {}) {
473
- const escape = md.utils.escapeHtml;
474
- md.block.ruler.before("table", "looma_frontmatter", frontmatter);
475
- md.block.ruler.before("reference", "looma_footnote", footnoteDefinition, { alt: ["paragraph", "reference"] });
476
- md.block.ruler.before("fence", "looma_container", container, { alt: [
477
- "paragraph",
478
- "reference",
479
- "blockquote",
480
- "list"
481
- ] });
482
- md.block.ruler.before("paragraph", "looma_leaf", leaf, { alt: [
483
- "paragraph",
484
- "reference",
485
- "blockquote",
486
- "list"
487
- ] });
488
- md.inline.ruler.before("link", "looma_directive", directive);
489
- md.inline.ruler.before("link", "looma_mention", mention);
490
- md.inline.ruler.before("link", "looma_footnote_ref", footnoteReference);
491
- md.core.ruler.after("block", "looma_task_lists", taskLists);
492
- md.core.ruler.before("inline", "looma_alerts", alerts);
493
- md.core.ruler.before("text_join", "looma_attributes", attributes);
494
- md.core.ruler.after("inline", "looma_footnotes", footnotes);
495
- const rules = md.renderer.rules;
496
- rules.looma_frontmatter = () => "";
497
- rules.looma_container_open = (tokens, index) => {
498
- const { info, meta } = tokens[index];
499
- if (info === "details") return `<details${meta.open ? " open" : ""}><summary>${escape(meta.title || "Details")}</summary>\n`;
500
- if (info === "figure") return "<figure>\n";
501
- if (info === "tabs" || info === "code-group") return `<ui-tabs${info === "code-group" ? " class=\"code-group\"" : ""} group="${escape(meta.title || info)}">\n`;
502
- return `<ui-callout tone="${normalizeLoomaCalloutTone(info)}"${meta.title ? ` heading="${escape(meta.title)}"` : ""}>\n`;
503
- };
504
- rules.looma_container_close = (tokens, index, _options, env) => {
505
- const { info } = tokens[index];
506
- if (info === "details") return "</details>\n";
507
- if (info === "figure") {
508
- const title = tokens[index].meta.title;
509
- return `${title ? `<figcaption>${md.renderInline(title, env)}</figcaption>\n` : ""}</figure>\n`;
510
- }
511
- if (info === "tabs" || info === "code-group") return "</ui-tabs>\n";
512
- return "</ui-callout>\n";
513
- };
514
- rules.looma_tab_open = (tokens, index) => `<section aria-label="${escape(tokens[index].meta.label)}">\n`;
515
- rules.looma_tab_close = () => "</section>\n";
516
- rules.looma_footnote_ref = (tokens, index) => {
517
- const { label, number, occurrence } = tokens[index].meta;
518
- return `<sup><a href="#fn:${escape(label)}" id="fnref:${escape(label)}${occurrence > 1 ? `:${occurrence}` : ""}" data-footnote-ref="" aria-describedby="footnote:label">${number}</a></sup>`;
519
- };
520
- rules.looma_footnote_backrefs = (tokens, index) => {
521
- const { label, number } = tokens[index].meta;
522
- const count = tokens.find((token) => token.type === "looma_footnote_open" && token.meta.label === label)?.meta.count ?? 1;
523
- return Array.from({ length: count }, (_, occurrence) => {
524
- const suffix = occurrence ? `:${occurrence + 1}` : "";
525
- return ` <a href="#fnref:${escape(label)}${suffix}" data-footnote-backref="" aria-label="Back to reference ${number}${occurrence ? `-${occurrence + 1}` : ""}">↩${occurrence ? `<sup>${occurrence + 1}</sup>` : ""}</a>`;
526
- }).join("");
527
- };
528
- rules.looma_footnotes_open = () => "<section data-footnotes=\"\" class=\"footnotes\">\n<h2 id=\"footnote:label\" class=\"sr-only\">Footnotes</h2>\n<ol>\n";
529
- rules.looma_footnotes_close = () => "</ol>\n</section>\n";
530
- rules.looma_footnote_open = (tokens, index) => `<li id="fn:${escape(tokens[index].meta.label)}">\n`;
531
- rules.looma_footnote_close = () => "</li>\n";
532
- rules.looma_toc = (tokens, index) => tableOfContents(tokens, tokens[index].meta.format, tokens[index].meta.depth, escape);
533
- rules.looma_chip = (tokens, index) => {
534
- const { label, attributes } = tokens[index].meta;
535
- const tone = normalizeLoomaChipTone(attributes.tone);
536
- return `<span data-looma-chip="" data-label="${escape(label)}" data-color="${tone}"${label ? "" : " data-placeholder=\"\""}>${escape(label)}</span>`;
537
- };
538
- rules.looma_mention = (tokens, index) => {
539
- const { label, ref } = tokens[index].meta;
540
- return `<span data-type="mention" class="looma-mention"${ref === void 0 ? "" : ` data-id="${escape(ref)}"`} data-label="${escape(label)}">@${escape(label)}</span>`;
541
- };
542
- const code = (content, info) => {
543
- const { language, meta } = parseLoomaFenceInfo(info);
544
- const source = content.replace(/\n$/, "");
545
- return `<pre><code${language ? ` class="language-${escape(language)}"` : ""}${meta ? ` data-meta="${escape(meta)}"` : ""}>${escape(codeNotation === "keep" ? source : applyLoomaCodeNotation(source).code)}</code></pre>\n`;
546
- };
547
- rules.fence = (tokens, index) => code(tokens[index].content, tokens[index].info);
548
- rules.code_block = (tokens, index) => code(tokens[index].content, "");
549
- }
550
- /**
551
- * A markdown-it instance for Looma Markdown: raw HTML and GFM autolinks on, with `loomaMarkdown`.
552
- * @contract Each call returns a new instance that callers may extend.
553
- */
554
- function createLoomaMarkdownIt(options = {}) {
555
- return new MarkdownIt({
556
- html: true,
557
- linkify: true,
558
- typographer: false
559
- }).use(loomaMarkdown, options);
560
- }
561
- //#endregion
562
- export { createLoomaMarkdownIt, loomaMarkdown };