@quartz-community/remark-obsidian 2.2.0 → 2.2.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/index.js CHANGED
@@ -3075,9 +3075,95 @@ function spelledTarget(path, heading, resolved) {
3075
3075
  if (heading !== resolved.heading) spelled.spelledHeading = heading;
3076
3076
  return Object.keys(spelled).length > 0 ? spelled : void 0;
3077
3077
  }
3078
+ function dropClosingBackslash(part) {
3079
+ return part.slice(0, -1).trimEnd();
3080
+ }
3078
3081
  function writtenPart(part, spelled, strippedBackslash) {
3079
- const resolved = strippedBackslash ? `${part}\\` : part;
3080
- return spelled !== void 0 && resolveTarget(spelled) === resolved ? spelled : resolved;
3082
+ if (spelled !== void 0) {
3083
+ const resolved = resolveTarget(spelled);
3084
+ if (strippedBackslash ? resolved.endsWith("\\") && dropClosingBackslash(resolved) === part : resolved === part) {
3085
+ return spelled;
3086
+ }
3087
+ }
3088
+ return strippedBackslash ? `${part}\\` : part;
3089
+ }
3090
+
3091
+ // src/lib/tag-char.ts
3092
+ var TAG_STOP = /[\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~[\]\\\s]/u;
3093
+ function isTagCharacter(char) {
3094
+ return char !== "" && !TAG_STOP.test(char);
3095
+ }
3096
+
3097
+ // src/lib/colour-marker.ts
3098
+ var COLOUR_MARKERS = /* @__PURE__ */ new Set([
3099
+ 128308,
3100
+ 128997,
3101
+ 128992,
3102
+ 128999,
3103
+ 128993,
3104
+ 129e3,
3105
+ 128994,
3106
+ 129001,
3107
+ 128309,
3108
+ 128998,
3109
+ 128995,
3110
+ 129002
3111
+ ]);
3112
+ function colourMarkerLength(content) {
3113
+ const first = content.codePointAt(0);
3114
+ return first !== void 0 && COLOUR_MARKERS.has(first) ? 2 : 0;
3115
+ }
3116
+ function tagLength(value2) {
3117
+ if (value2.charAt(0) !== "#") return 0;
3118
+ let end = 1;
3119
+ let nonDigit = false;
3120
+ for (const character of value2.slice(1)) {
3121
+ if (!isTagCharacter(character)) break;
3122
+ if (!/[0-9]/u.test(character)) nonDigit = true;
3123
+ end += character.length;
3124
+ }
3125
+ return end > 1 && nonDigit ? end : 0;
3126
+ }
3127
+ function colourMarkerTagTransform(tree, source) {
3128
+ visit(tree, "highlight", (node2) => {
3129
+ const children = node2.children;
3130
+ const first = children[0];
3131
+ if (first?.type !== "text") return;
3132
+ const marker = colourMarkerLength(first.value);
3133
+ if (marker === 0) return;
3134
+ const length = tagLength(first.value.slice(marker));
3135
+ if (length === 0) return;
3136
+ const start = first.position?.start;
3137
+ if (!start || source.charAt((start.offset ?? 0) + marker) !== "#") return;
3138
+ const at3 = (columns2) => start && {
3139
+ line: start.line,
3140
+ column: start.column + columns2,
3141
+ offset: (start.offset ?? 0) + columns2
3142
+ };
3143
+ const place = (from, to) => start ? { position: { start: at3(from), end: at3(to) } } : {};
3144
+ const end = marker + length;
3145
+ const rest = first.value.slice(end);
3146
+ const split = [
3147
+ {
3148
+ type: "text",
3149
+ value: first.value.slice(0, marker),
3150
+ ...place(0, marker)
3151
+ },
3152
+ {
3153
+ type: "tag",
3154
+ value: first.value.slice(marker + 1, end),
3155
+ ...place(marker, end)
3156
+ }
3157
+ ];
3158
+ if (rest !== "") {
3159
+ split.push({
3160
+ type: "text",
3161
+ value: rest,
3162
+ ...first.position ? { position: { start: at3(end), end: first.position.end } } : {}
3163
+ });
3164
+ }
3165
+ children.splice(0, 1, ...split);
3166
+ });
3081
3167
  }
3082
3168
 
3083
3169
  // src/lib/syntax/legacy-scan.ts
@@ -8901,12 +8987,6 @@ function keys(value2) {
8901
8987
  return result;
8902
8988
  }
8903
8989
 
8904
- // src/lib/tag-char.ts
8905
- var TAG_STOP = /[\u2000-\u206F\u2E00-\u2E7F'!"#$%&()*+,.:;<=>?@^`{|}~[\]\\\s]/u;
8906
- function isTagCharacter(char) {
8907
- return char !== "" && !TAG_STOP.test(char);
8908
- }
8909
-
8910
8990
  // src/rp8/reader.ts
8911
8991
  function along(point, columns2) {
8912
8992
  return {
@@ -9043,9 +9123,27 @@ function tablePipeStyles(tables) {
9043
9123
  tables.blockTokenizers.table = function(eat, value2, silent) {
9044
9124
  const end = tableEnd(value2);
9045
9125
  if (end === -1) return void 0;
9046
- return stockTable.call(this, eat, value2.slice(0, end), silent);
9126
+ const table2 = stockTable.call(this, eat, value2.slice(0, end), silent);
9127
+ if (table2 && typeof table2 === "object" && !startsWithPipe(value2)) {
9128
+ leadingIndentCells(table2, value2.slice(0, end));
9129
+ }
9130
+ return table2;
9047
9131
  };
9048
9132
  }
9133
+ function leadingIndentCells(table2, source) {
9134
+ const lines = source.split("\n");
9135
+ lines.splice(1, 1);
9136
+ for (const [index, row] of (table2.children ?? []).entries()) {
9137
+ const pipe = /^[ \t]+\|/u.exec(lines[index] ?? "");
9138
+ if (!pipe) continue;
9139
+ const at3 = along(row.position.start, pipe[0].length - 1);
9140
+ row.children?.unshift({
9141
+ type: "tableCell",
9142
+ children: [],
9143
+ position: { start: at3, end: { ...at3 } }
9144
+ });
9145
+ }
9146
+ }
9049
9147
  var Wikilink = /^!?\[\[(?:(?!\[\[)[^\n\r\u2028\u2029])+?\]\]/;
9050
9148
  function wikilinks(tables) {
9051
9149
  const wikilink2 = function(eat, value2, silent) {
@@ -9108,10 +9206,12 @@ function highlights(tables) {
9108
9206
  if (body === null) return void 0;
9109
9207
  if (silent === true) return true;
9110
9208
  const now = eat.now();
9111
- return eat(`==${body}==`)({
9112
- type: "highlight",
9113
- children: this.tokenizeInline(body, along(now, 2))
9114
- });
9209
+ const marker = colourMarkerLength(body);
9210
+ const children = marker > 0 && body.charAt(marker) === "#" ? [
9211
+ ...this.tokenizeInline(body.slice(0, marker), along(now, 2)),
9212
+ ...this.tokenizeInline(body.slice(marker), along(now, 2 + marker))
9213
+ ] : this.tokenizeInline(body, along(now, 2));
9214
+ return eat(`==${body}==`)({ type: "highlight", children });
9115
9215
  };
9116
9216
  highlight2.locator = (value2, fromIndex) => value2.indexOf("==", fromIndex);
9117
9217
  tables.inlineTokenizers.highlight = highlight2;
@@ -9637,12 +9737,20 @@ function wikilink(raw) {
9637
9737
  const writtenHeading = node2.heading;
9638
9738
  node2.path = resolveTarget(node2.path);
9639
9739
  node2.heading = resolveTarget(node2.heading);
9640
- const spelled = spelledTarget(written2, writtenHeading, node2);
9740
+ let spelled = spelledTarget(written2, writtenHeading, node2);
9641
9741
  const resolved = parts.heading ? "heading" : parts.path ? "path" : void 0;
9642
9742
  let stripped;
9643
9743
  if (resolved && node2[resolved].endsWith("\\")) {
9644
- node2[resolved] = node2[resolved].slice(0, -1);
9744
+ const kept = node2[resolved].slice(0, -1);
9745
+ node2[resolved] = dropClosingBackslash(node2[resolved]);
9645
9746
  stripped = resolved;
9747
+ if (kept !== node2[resolved] && !parts.alias) {
9748
+ const key = resolved === "path" ? "spelledPath" : "spelledHeading";
9749
+ spelled = {
9750
+ ...spelled,
9751
+ [key]: resolved === "path" ? written2 : writtenHeading
9752
+ };
9753
+ }
9646
9754
  }
9647
9755
  const value2 = embedded ? node2.path : parts.alias ? node2.alias : [written2, ...writtenHeading.split("#")].filter(Boolean).join(" > ");
9648
9756
  return {
@@ -12779,9 +12887,14 @@ function wikilinkFromMarkdown() {
12779
12887
  const parts = partsOf(node2);
12780
12888
  const resolved = parts.heading ? "heading" : parts.path ? "path" : void 0;
12781
12889
  if (resolved !== void 0 && String(node2[resolved]).endsWith("\\")) {
12782
- node2[resolved] = String(node2[resolved]).slice(0, -1);
12890
+ const kept = String(node2[resolved]).slice(0, -1);
12891
+ node2[resolved] = dropClosingBackslash(String(node2[resolved]));
12783
12892
  if (!parts.alias) {
12784
12893
  node2.data = { ...node2.data, strippedBackslash: resolved };
12894
+ if (kept !== node2[resolved]) {
12895
+ const key = resolved === "path" ? "spelledPath" : "spelledHeading";
12896
+ node2.data[key] = resolved === "path" ? written2 : writtenHeading;
12897
+ }
12785
12898
  }
12786
12899
  }
12787
12900
  node2.value = node2.embedded ? node2.path : partsOf(node2).alias ? node2.alias : (
@@ -13456,6 +13569,8 @@ function detect(tree, source, lines, parse3, options) {
13456
13569
  const calloutTitles2 = /* @__PURE__ */ new Set();
13457
13570
  const listStarts = /* @__PURE__ */ new Map();
13458
13571
  const definitions = [];
13572
+ const keepsLazyLines = /* @__PURE__ */ new WeakSet();
13573
+ const nextItemLine = /* @__PURE__ */ new WeakMap();
13459
13574
  const walk = (node2, inDefinition, item, hostLine, parent, container, index = -1) => {
13460
13575
  if (options.comments && !inDefinition && (container === "blockquote" || container === "listItem")) {
13461
13576
  const split = node2.type === "paragraph" ? lazyCommentOpener(node2, lines) : node2.type === "comment" && parent && parent.children[0] !== node2 ? lazyCommentNode(node2, parent, lines) : void 0;
@@ -13486,10 +13601,24 @@ function detect(tree, source, lines, parse3, options) {
13486
13601
  }
13487
13602
  if (!inDefinition && (container === "blockquote" || container === "listItem") && (node2.type === "math" || options.comments && node2.type === "comment")) {
13488
13603
  if (!closesMidline(node2, parent, index)) {
13489
- late.push(...keptLazyLines(node2, source, lines, parse3));
13604
+ const lazy = keptLazyLines(node2, source, lines, parse3);
13605
+ late.push(...lazy);
13606
+ if (lazy.length > 0) keepsLazyLines.add(node2);
13490
13607
  }
13491
13608
  if (node2.type === "math") edits.push(...containedMathOpener(node2, lines));
13492
13609
  }
13610
+ if (!inDefinition && node2.type === "math" && parent && (parent.type === "root" || parent.type === "blockquote" || parent.type === "listItem") && !keepsLazyLines.has(node2) && !closesSpanAbove(node2, parent, index)) {
13611
+ edits.push(
13612
+ ...lastLineMathOpener(node2, parent, lines, nextItemLine.get(parent))
13613
+ );
13614
+ }
13615
+ if (node2.type === "list") {
13616
+ const items = node2.children;
13617
+ items.forEach((listItem3, at3) => {
13618
+ const next = items[at3 + 1]?.position?.start.line;
13619
+ if (next !== void 0) nextItemLine.set(listItem3, next);
13620
+ });
13621
+ }
13493
13622
  if (node2.type === "footnoteReference" && parent) {
13494
13623
  const caret2 = linkedReference(node2, parent, index, source);
13495
13624
  if (caret2 !== void 0)
@@ -13713,6 +13842,36 @@ function containedMathOpener(node2, lines) {
13713
13842
  { kind: "neutralize", offset: position.start.offset + 1 }
13714
13843
  ];
13715
13844
  }
13845
+ function lastLineMathOpener(node2, parent, lines, nextItem) {
13846
+ const position = node2.position;
13847
+ if (!position || position.start.offset === void 0) return [];
13848
+ const line = position.start.line;
13849
+ if (endLine(position.end) !== line) return [];
13850
+ const start = position.start.offset - (lines.starts[line - 1] ?? 0);
13851
+ if (!/^\$\$[ \t]*$/u.test((lines.text[line - 1] ?? "").slice(start))) {
13852
+ return [];
13853
+ }
13854
+ const last = parent.type === "root" ? line === lines.text.length : parent.position !== void 0 && Math.min(endLine(parent.position.end), (nextItem ?? Infinity) - 1) === line;
13855
+ if (!last) return [];
13856
+ return [
13857
+ { kind: "neutralize", offset: position.start.offset },
13858
+ { kind: "neutralize", offset: position.start.offset + 1 }
13859
+ ];
13860
+ }
13861
+ function closesSpanAbove(node2, parent, index) {
13862
+ const before2 = parent.children[index - 1];
13863
+ if (!before2 || before2.position?.end.line !== (node2.position?.start.line ?? 0) - 1) {
13864
+ return false;
13865
+ }
13866
+ let text3 = "";
13867
+ visitText(before2, (value2) => {
13868
+ text3 += value2;
13869
+ });
13870
+ return (text3.match(/\$\$/gu)?.length ?? 0) % 2 === 1;
13871
+ }
13872
+ function endLine(end) {
13873
+ return end.column === 1 && end.line > 1 ? end.line - 1 : end.line;
13874
+ }
13716
13875
  function lazyCommentUnderDefinition(definition2, lines) {
13717
13876
  const comment2 = definition2.children.at(-1);
13718
13877
  const paragraph2 = definition2.children.at(-2);
@@ -14430,10 +14589,10 @@ function calloutTitleInterruptTransform(tree, source, reparse) {
14430
14589
  const titleLine = titled ? callout.children[0]?.position?.start?.line : callout.position?.start?.line;
14431
14590
  const from = next.position?.start?.offset;
14432
14591
  const startLine = next.position?.start?.line;
14433
- const endLine = next.position?.end?.line;
14592
+ const endLine2 = next.position?.end?.line;
14434
14593
  if (titleLine === void 0 || from === void 0) return;
14435
- if (startLine === void 0 || endLine === void 0) return;
14436
- if (startLine !== titleLine + 1 || endLine !== startLine) return;
14594
+ if (startLine === void 0 || endLine2 === void 0) return;
14595
+ if (startLine !== titleLine + 1 || endLine2 !== startLine) return;
14437
14596
  const nl = source.indexOf("\n", from);
14438
14597
  const text3 = source.slice(from, nl === -1 ? void 0 : nl);
14439
14598
  const ordered = ORDERED.exec(text3);
@@ -14893,7 +15052,6 @@ function frontmatterTailTransform(tree, source, reparse) {
14893
15052
  }
14894
15053
  if (closeIndex === -1) return;
14895
15054
  const tail = lines[closeIndex].slice(3);
14896
- if (tail.trim() === "") return;
14897
15055
  let offset = 0;
14898
15056
  const lineStarts = lines.map((line) => {
14899
15057
  const start = offset;
@@ -14901,6 +15059,13 @@ function frontmatterTailTransform(tree, source, reparse) {
14901
15059
  return start;
14902
15060
  });
14903
15061
  const closeEnd = lineStarts[closeIndex] + 3;
15062
+ if (tail.trim() === "") {
15063
+ const yaml2 = tree.children[0];
15064
+ if (tail !== "" && yaml2?.type === "yaml" && yaml2.position && (yaml2.position.end.offset ?? 0) > closeEnd) {
15065
+ yaml2.position.end = { line: closeIndex + 1, column: 4, offset: closeEnd };
15066
+ }
15067
+ return;
15068
+ }
14904
15069
  const rest = [tail, ...lines.slice(closeIndex + 1)].join("\n");
14905
15070
  const map = {
14906
15071
  text: rest,
@@ -15262,6 +15427,12 @@ function textToMarkdown(tags2, highlights2 = false, legacy = false) {
15262
15427
  defaultHandlers6.text(node2, parent, state, info),
15263
15428
  tags2
15264
15429
  ).replace(/(?<!\\)((?:\\\\)*)\\(\s+)$/u, "$1\\\\$2");
15430
+ if (tags2 && parent?.type === "highlight" && parent.children[0] === node2) {
15431
+ const marker = colourMarkerLength(written2);
15432
+ if (marker > 0 && /^#[^\s]/u.test(written2.slice(marker))) {
15433
+ written2 = `${written2.slice(0, marker)}\\${written2.slice(marker)}`;
15434
+ }
15435
+ }
15265
15436
  const siblingsOf2 = parent?.children ?? [];
15266
15437
  const afterBreak = siblingsOf2[siblingsOf2.indexOf(node2) - 1]?.type === "break";
15267
15438
  written2 = written2.replace(/\u2E7F/gu, "&#11903;");
@@ -16747,6 +16918,8 @@ function remarkObsidian(userOpts) {
16747
16918
  definitionMathAbsorbTransform(tree, String(file));
16748
16919
  definitionEndBeforeLabelTransform(tree, String(file));
16749
16920
  tagAgainstTextTransform(tree, String(file));
16921
+ if (opts.tags && opts.highlights)
16922
+ colourMarkerTagTransform(tree, String(file));
16750
16923
  if (opts.inlineFootnotes && !opts.inertFootnoteRefs) {
16751
16924
  inlineFootnoteInertTransform(tree);
16752
16925
  }