@quartz-community/github-flavored-markdown 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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 2.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 22cdbf9: `replacements`:
8
+
9
+ - An autolink's text, its URL, stays as written (`https://e.com/q->r` showed `q→r`).
10
+ - A wikilink's alias is replaced like a Markdown link's text (`[[a|x -> y]]` was left alone while `[x -> y](a)` was replaced); an embed's alias is not, like an image's alt text.
11
+ - A backslash-escaped character (or a character reference) keeps a key literal: `a\->b` stays `a->b`.
12
+ - A heading changed by `replacements` or SmartyPants keeps its text as written in `data-heading`, as its id and Obsidian's links already did (`## a -> b` got `data-heading="a → b"` and `id="a---b"`).
13
+
3
14
  ## 2.2.0
4
15
 
5
16
  ### Minor Changes
package/README.md CHANGED
@@ -31,7 +31,7 @@ This plugin must run before [obsidian-flavored-markdown](https://github.com/quar
31
31
  | `enableSmartyPants` | `boolean` | `true` | Typographic enhancements: curly quotes, en/em dashes, ellipses. |
32
32
  | `linkHeadings` | `boolean` | `true` | Gives headings their `id`s and adds an anchor link to each. `false` also turns off heading ids, so links to headings (`[[page#Heading]]`) and section embeds no longer reach them (the table of contents plugin still adds ids on pages that show it). |
33
33
  | `singleTilde` | `boolean` | `true` | Strikes text between single tildes (`~text~`), as GitHub does. `false` strikes only `~~text~~`, as Obsidian does, so a `~` meaning "about" (`~20 W and (~€120)`) stays as written. Under obsidian-flavored-markdown's default `legacy` preset, which parses as Obsidian, single tildes never strike. |
34
- | `replacements` | `object` | `{}` | Text to replace as written, outside code and math, before SmartyPants; the longest match wins. Empty: neither GitHub nor Obsidian replaces anything. See [Arrows](#arrows). |
34
+ | `replacements` | `object` | `{}` | Text to replace as written, outside code, math and autolinks, before SmartyPants; the longest match wins. Empty: neither GitHub nor Obsidian replaces anything. See [Arrows](#arrows). |
35
35
 
36
36
  ## Arrows
37
37
 
@@ -52,13 +52,16 @@ plugins:
52
52
  "<==": "⇐"
53
53
  ```
54
54
 
55
- Text in code, inline code and math is left alone. Note that `<=` then also replaces a "less than or equal" written in prose.
55
+ Text in code, inline code and math is left alone, and so is an autolink's text (`https://e.com/a->b`), which is its URL. A wikilink's alias (`[[note|a -> b]]`) is replaced like a Markdown link's text; an embed's alias (`![[pic.png|a -> b]]`) is not, like an image's alt text. A backslash before a character of a key keeps it as written: `a\->b` stays `a->b`. Note that `<=` then also replaces a "less than or equal" written in prose.
56
+
57
+ Under [Obsidian Flavored Markdown](https://github.com/quartz-community/obsidian-flavored-markdown)'s `preset: strict`, versions of it that bundle `@quartz-community/remark-obsidian` 2.2.3 or earlier re-parse a note holding a callout after these replacements and SmartyPants ran, which undoes them in that note.
56
58
 
57
59
  ## Heading ids
58
60
 
59
61
  Heading ids are made from the heading as written, with the same slug function that links and the table of contents use, and match GitHub's and Obsidian's:
60
62
 
61
63
  - With SmartyPants on, a heading containing `--` keeps it in its id: `## Range 1--2` → `range-1--2` (before Quartz 5.1: `range-12`). `[[page#Range 1--2]]` keeps working.
64
+ - A heading that SmartyPants or `replacements` changed keeps its text as written in Obsidian's `data-heading` attribute, too: `## a -> b` shows `a → b` and has `data-heading="a -> b"`.
62
65
  - Headings containing `:`, `|`, `^`, `#` or `\` get an extra anchor with the id Obsidian links use (spaces instead of those characters), so such links and section embeds reach them.
63
66
  - A same-page link written as a heading's text, such as `[x](<#My Heading>)`, points at the heading's id.
64
67
 
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ interface GfmOptions {
5
5
  enableSmartyPants: boolean;
6
6
  linkHeadings: boolean;
7
7
  /**
8
- * Text to replace as written, applied before SmartyPants, outside code and math:
8
+ * Text to replace as written, applied before SmartyPants, outside code, math and autolinks,
9
+ * and in wikilink aliases (`[[note|a -> b]]`); a backslash escape keeps a key (`a\->b`):
9
10
  * `{ "->": "→", "=>": "⇒" }` brings back Quartz 4's `parseArrows`. Empty by default: neither
10
11
  * GitHub nor Obsidian replaces anything.
11
12
  */
package/dist/index.js CHANGED
@@ -6645,6 +6645,17 @@ function headingText(el, rewrites = []) {
6645
6645
  walk(el);
6646
6646
  return text3;
6647
6647
  }
6648
+ function keepWrittenHeadingData(el, rewrites) {
6649
+ if (rewrites.length === 0 || el.properties.dataHeading !== void 0) return;
6650
+ el.properties.dataHeading = headingText(el, rewrites).trim();
6651
+ }
6652
+ function rehypeWrittenHeadingData() {
6653
+ return (tree) => {
6654
+ eachElement(tree, (el) => {
6655
+ if (isHeading(el)) keepWrittenHeadingData(el, takeRewrites(el));
6656
+ });
6657
+ };
6658
+ }
6648
6659
  function rehypeHeadingIds() {
6649
6660
  return (tree) => {
6650
6661
  const slugger = new HeadingSlugger();
@@ -6652,6 +6663,7 @@ function rehypeHeadingIds() {
6652
6663
  eachElement(tree, (el) => {
6653
6664
  if (!isHeading(el)) return;
6654
6665
  const rewrites = takeRewrites(el);
6666
+ keepWrittenHeadingData(el, rewrites);
6655
6667
  if (el.properties.id) return;
6656
6668
  const text3 = headingText(el, rewrites);
6657
6669
  el.properties.id = slugger.slug(text3);
@@ -6691,14 +6703,68 @@ function remarkTextReplacements(replacements) {
6691
6703
  keys2.map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"),
6692
6704
  "g"
6693
6705
  );
6694
- const walk = (node2) => {
6695
- if (node2.type === "text") {
6696
- node2.value = node2.value.replace(pattern, (match) => replacements[match] ?? match);
6697
- return;
6698
- }
6699
- if ("children" in node2) for (const child of node2.children) walk(child);
6706
+ const replace2 = (value, literal) => value.replace(pattern, (match, offset) => {
6707
+ if (literal?.slice(offset, offset + match.length).some(Boolean)) return match;
6708
+ return replacements[match] ?? match;
6709
+ });
6710
+ return (tree, file) => {
6711
+ const source = typeof file?.value === "string" ? file.value : void 0;
6712
+ const walk = (node2) => {
6713
+ if (node2.type === "text") {
6714
+ node2.value = replace2(node2.value, source ? literalChars(node2, source) : void 0);
6715
+ return;
6716
+ }
6717
+ if (node2.type === "link" && isAutolink(node2)) return;
6718
+ const wikilink = node2;
6719
+ if (wikilink.type === "wikilink") {
6720
+ if (!wikilink.embedded && typeof wikilink.alias === "string")
6721
+ wikilink.alias = replace2(wikilink.alias);
6722
+ return;
6723
+ }
6724
+ if ("children" in node2) for (const child of node2.children) walk(child);
6725
+ };
6726
+ walk(tree);
6700
6727
  };
6701
- return (tree) => walk(tree);
6728
+ }
6729
+ function isAutolink(node2) {
6730
+ if (node2.children.length !== 1 || node2.children[0].type !== "text") return false;
6731
+ const text3 = node2.children[0].value;
6732
+ return node2.url === text3 || node2.url === `mailto:${text3}` || node2.url === `http://${text3}` || node2.url === `https://${text3}`;
6733
+ }
6734
+ var ASCII_PUNCTUATION = /[!-/:-@[-`{-~]/;
6735
+ var CHARACTER_REFERENCE = /^&(?:#\d{1,7}|#[xX][\da-fA-F]{1,6}|[A-Za-z][A-Za-z\d]{0,31});/;
6736
+ function literalChars(node2, source) {
6737
+ const start = node2.position?.start.offset;
6738
+ const end = node2.position?.end.offset;
6739
+ if (start === void 0 || end === void 0) return void 0;
6740
+ const raw = source.slice(start, end);
6741
+ const value = node2.value;
6742
+ if (raw === value) return void 0;
6743
+ const literal = new Array(value.length).fill(false);
6744
+ let i = 0;
6745
+ for (let j = 0; j < value.length && i < raw.length; ) {
6746
+ const char = raw[i];
6747
+ if (char === "\\" && i + 1 < raw.length && ASCII_PUNCTUATION.test(raw[i + 1])) {
6748
+ if (value[j] === raw[i + 1]) {
6749
+ literal[j] = true;
6750
+ i += 2;
6751
+ j += 1;
6752
+ continue;
6753
+ }
6754
+ }
6755
+ if (char === "&" && value[j] !== "&") {
6756
+ const reference = CHARACTER_REFERENCE.exec(raw.slice(i));
6757
+ if (reference) {
6758
+ literal[j] = true;
6759
+ i += reference[0].length;
6760
+ j += 1;
6761
+ continue;
6762
+ }
6763
+ }
6764
+ if (char === value[j]) j += 1;
6765
+ i += 1;
6766
+ }
6767
+ return literal;
6702
6768
  }
6703
6769
 
6704
6770
  // src/transformer.ts
@@ -6719,63 +6785,61 @@ var GitHubFlavoredMarkdown = (userOpts) => {
6719
6785
  rewrites.push([remarkTextReplacements, opts.replacements]);
6720
6786
  if (opts.enableSmartyPants) rewrites.push(plugin_default);
6721
6787
  if (rewrites.length === 0) return [gfm2];
6722
- return opts.linkHeadings ? [gfm2, remarkRememberHeadingText, ...rewrites, remarkRecordHeadingRewrites] : [gfm2, ...rewrites];
6788
+ return [gfm2, remarkRememberHeadingText, ...rewrites, remarkRecordHeadingRewrites];
6723
6789
  },
6724
6790
  htmlPlugins() {
6725
- if (opts.linkHeadings) {
6726
- return [
6727
- rehypeHeadingIds,
6728
- [
6729
- rehypeAutolinkHeadings,
6730
- {
6731
- behavior: "append",
6791
+ const rewrites = Object.keys(opts.replacements).length > 0 || opts.enableSmartyPants;
6792
+ if (!opts.linkHeadings) return rewrites ? [rehypeWrittenHeadingData] : [];
6793
+ return [
6794
+ rehypeHeadingIds,
6795
+ [
6796
+ rehypeAutolinkHeadings,
6797
+ {
6798
+ behavior: "append",
6799
+ properties: {
6800
+ role: "anchor",
6801
+ // A string: `true` renders as a bare `aria-hidden`, which isn't
6802
+ // "true", so the icon link was exposed as a link without a name
6803
+ // and an unknown role.
6804
+ ariaHidden: "true",
6805
+ tabIndex: -1,
6806
+ "data-no-popover": true
6807
+ },
6808
+ content: {
6809
+ type: "element",
6810
+ tagName: "svg",
6732
6811
  properties: {
6733
- role: "anchor",
6734
- // A string: `true` renders as a bare `aria-hidden`, which isn't
6735
- // "true", so the icon link was exposed as a link without a name
6736
- // and an unknown role.
6737
- ariaHidden: "true",
6738
- tabIndex: -1,
6739
- "data-no-popover": true
6812
+ width: 18,
6813
+ height: 18,
6814
+ viewBox: "0 0 24 24",
6815
+ fill: "none",
6816
+ stroke: "currentColor",
6817
+ "stroke-width": "2",
6818
+ "stroke-linecap": "round",
6819
+ "stroke-linejoin": "round"
6740
6820
  },
6741
- content: {
6742
- type: "element",
6743
- tagName: "svg",
6744
- properties: {
6745
- width: 18,
6746
- height: 18,
6747
- viewBox: "0 0 24 24",
6748
- fill: "none",
6749
- stroke: "currentColor",
6750
- "stroke-width": "2",
6751
- "stroke-linecap": "round",
6752
- "stroke-linejoin": "round"
6821
+ children: [
6822
+ {
6823
+ type: "element",
6824
+ tagName: "path",
6825
+ properties: {
6826
+ d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"
6827
+ },
6828
+ children: []
6753
6829
  },
6754
- children: [
6755
- {
6756
- type: "element",
6757
- tagName: "path",
6758
- properties: {
6759
- d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"
6760
- },
6761
- children: []
6830
+ {
6831
+ type: "element",
6832
+ tagName: "path",
6833
+ properties: {
6834
+ d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"
6762
6835
  },
6763
- {
6764
- type: "element",
6765
- tagName: "path",
6766
- properties: {
6767
- d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"
6768
- },
6769
- children: []
6770
- }
6771
- ]
6772
- }
6836
+ children: []
6837
+ }
6838
+ ]
6773
6839
  }
6774
- ]
6775
- ];
6776
- } else {
6777
- return [];
6778
- }
6840
+ }
6841
+ ]
6842
+ ];
6779
6843
  }
6780
6844
  };
6781
6845
  };