autumnnote 1.9.1 → 1.10.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.
@@ -5309,7 +5309,7 @@
5309
5309
  * @returns {boolean} `true` if any Markdown-like pattern is present, `false` otherwise.
5310
5310
  */
5311
5311
  function isMarkdown(text) {
5312
- return /^#{1,6} [^\s]|^[ \t]*[-*+] [^\s]|^[ \t]*\d+\. [^\s]|^> [^\s]|^```|^\*{2}[^*\n]+\*{2}/m.test(text) || /^.+\n=+\s*$/m.test(text) || /^.+\n-{2,}\s*$/m.test(text);
5312
+ return /^#{1,6} [^\s]|^[ \t]*[-*+] [^\s]|^[ \t]*\d+\. [^\s]|^> [^\s]|^```|^\*{2}[^*\n]+\*{2}/m.test(text) || /^.+\n=+\s*$/m.test(text) || /^.+\n-{2,}\s*$/m.test(text) || /^---\s*\n(?:[\s\S]*?\n)?(?:---|\.\.\.)\s*(?:\n|$)/.test(text) || /^\|.+\|[ \t]*\n\|[ \t:|-]+\|/m.test(text);
5313
5313
  }
5314
5314
  /**
5315
5315
  * Converts a Markdown string to an HTML string.
@@ -5317,7 +5317,12 @@
5317
5317
  * @returns {string}
5318
5318
  */
5319
5319
  function markdownToHTML(text) {
5320
- const lines = text.replaceAll("\r\n", "\n").replaceAll("\r", "\n").split("\n");
5320
+ let lines = text.replaceAll("\r\n", "\n").replaceAll("\r", "\n").split("\n");
5321
+ lines = _stripFrontmatter(lines);
5322
+ const refs = _extractReferenceDefinitions(lines);
5323
+ lines = refs.clean;
5324
+ _linkDefs = refs.linkDefs;
5325
+ _footnoteIds = refs.footnoteIds;
5321
5326
  const out = [];
5322
5327
  let i = 0;
5323
5328
  while (i < lines.length) {
@@ -5417,6 +5422,76 @@
5417
5422
  }
5418
5423
  return out.join("");
5419
5424
  }
5425
+ /** Reference-link and footnote definitions collected per markdownToHTML() call. */
5426
+ var _linkDefs = /* @__PURE__ */ new Map();
5427
+ var _footnoteIds = /* @__PURE__ */ new Set();
5428
+ /**
5429
+ * Strips a leading YAML frontmatter block (--- ... --- or --- ... ...) from
5430
+ * the line array, only when it is the very first line and the enclosed body
5431
+ * looks like YAML (key: value / list items / indented continuations) — this
5432
+ * disambiguates real frontmatter from a horizontal rule followed by prose.
5433
+ * @param {string[]} lines
5434
+ * @returns {string[]}
5435
+ */
5436
+ function _stripFrontmatter(lines) {
5437
+ if ((lines[0] || "").trim() !== "---") return lines;
5438
+ let closeIdx = -1;
5439
+ for (let j = 1; j < lines.length; j++) {
5440
+ const t = lines[j].trim();
5441
+ if (t === "---" || t === "...") {
5442
+ closeIdx = j;
5443
+ break;
5444
+ }
5445
+ }
5446
+ if (closeIdx === -1) return lines;
5447
+ if (!lines.slice(1, closeIdx).every((l) => l.trim() === "" || /^[ \t]*[\w$.-]+\s*:(\s|$)/.test(l) || /^[ \t]*-\s+\S/.test(l) || /^[ \t]+\S/.test(l))) return lines;
5448
+ let start = closeIdx + 1;
5449
+ if (lines[start] !== void 0 && lines[start].trim() === "") start++;
5450
+ return lines.slice(start);
5451
+ }
5452
+ /**
5453
+ * Extracts GFM reference-link definitions (`[ref]: url "title"`) and footnote
5454
+ * definitions (`[^id]: text`) from the line array, skipping fenced code
5455
+ * regions. Returns the definition-free line array plus lookup maps.
5456
+ * @param {string[]} lines
5457
+ * @returns {{ clean: string[], linkDefs: Map<string, {href: string, title?: string}>, footnoteIds: Set<string> }}
5458
+ */
5459
+ function _extractReferenceDefinitions(lines) {
5460
+ const linkDefs = /* @__PURE__ */ new Map();
5461
+ const footnoteIds = /* @__PURE__ */ new Set();
5462
+ const clean = [];
5463
+ let inFence = false;
5464
+ const linkDefRe = /^\[([^\]]+)\]:\s*(\S+)(?:\s+"([^"]*)")?\s*$/;
5465
+ const footnoteDefRe = /^\[\^([^\]]+)\]:\s*(.+)$/;
5466
+ for (const line of lines) {
5467
+ if (/^```/.test(line)) {
5468
+ inFence = !inFence;
5469
+ clean.push(line);
5470
+ continue;
5471
+ }
5472
+ if (!inFence) {
5473
+ const fm = footnoteDefRe.exec(line);
5474
+ if (fm) {
5475
+ footnoteIds.add(fm[1]);
5476
+ continue;
5477
+ }
5478
+ const lm = linkDefRe.exec(line);
5479
+ if (lm) {
5480
+ linkDefs.set(lm[1].trim().toLowerCase(), {
5481
+ href: lm[2],
5482
+ title: lm[3]
5483
+ });
5484
+ continue;
5485
+ }
5486
+ }
5487
+ clean.push(line);
5488
+ }
5489
+ return {
5490
+ clean,
5491
+ linkDefs,
5492
+ footnoteIds
5493
+ };
5494
+ }
5420
5495
  /**
5421
5496
  * Splits a GFM table row string into trimmed cell strings.
5422
5497
  * '| a | b | c |' → ['a', 'b', 'c']
@@ -5480,6 +5555,19 @@
5480
5555
  function _inline(text) {
5481
5556
  text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_, alt, src) => `<img src="${_escAttr(src)}" alt="${_escAttr(alt)}" class="an-image">`);
5482
5557
  text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_, label, href) => `<a href="${_escAttr(href)}">${_esc(label)}</a>`);
5558
+ text = text.replace(/\[([^\]]+)\]\[([^\]]*)\]/g, (m, label, ref) => {
5559
+ const def = _linkDefs.get((ref || label).trim().toLowerCase());
5560
+ if (!def) return m;
5561
+ const titleAttr = def.title ? ` title="${_escAttr(def.title)}"` : "";
5562
+ return `<a href="${_escAttr(def.href)}"${titleAttr}>${_esc(label)}</a>`;
5563
+ });
5564
+ text = text.replace(/\[([^\]]+)\]/g, (m, label) => {
5565
+ const def = _linkDefs.get(label.trim().toLowerCase());
5566
+ if (!def) return m;
5567
+ const titleAttr = def.title ? ` title="${_escAttr(def.title)}"` : "";
5568
+ return `<a href="${_escAttr(def.href)}"${titleAttr}>${_esc(label)}</a>`;
5569
+ });
5570
+ text = text.replace(/\[\^([^\]]+)\]/g, (m, id) => _footnoteIds.has(id) ? `<sup>[${_esc(id)}]</sup>` : m);
5483
5571
  text = text.replace(/\*{3}([^*\n]+?)\*{3}/g, (_, c) => `<strong><em>${_esc(c)}</em></strong>`);
5484
5572
  text = text.replace(/_{3}([^_\n]+?)_{3}/g, (_, c) => `<strong><em>${_esc(c)}</em></strong>`);
5485
5573
  text = text.replace(/\*{2}([^*\n]+?)\*{2}/g, (_, c) => `<strong>${_esc(c)}</strong>`);
@@ -17168,7 +17256,7 @@
17168
17256
  /** All pre-built button definitions — accessible in every module format including UMD/CJS. */
17169
17257
  buttons,
17170
17258
  /** Library version */
17171
- version: "1.9.1"
17259
+ version: "1.10.0"
17172
17260
  };
17173
17261
  /**
17174
17262
  * @param {string|Element|NodeList|Element[]} selector