@quartz-community/github-flavored-markdown 2.1.2 → 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,23 @@
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
+
14
+ ## 2.2.0
15
+
16
+ ### Minor Changes
17
+
18
+ - 63864df: `singleTilde` option, passed to remark-gfm: `false` strikes only `~~text~~`, as Obsidian does, so a `~` meaning "about" (`~20 W and (~€120)`) is no longer struck. On by default, as before (#2).
19
+ - 6b6704f: `replacements` option: text to replace as written, outside code and math, before SmartyPants (`{ "->": "→" }`). Empty by default. The README lists Quartz 4's `parseArrows` mapping, for sites that want their arrows back (obsidian-flavored-markdown#12).
20
+
3
21
  ## 2.1.2
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -26,16 +26,42 @@ This plugin must run before [obsidian-flavored-markdown](https://github.com/quar
26
26
 
27
27
  ## Configuration
28
28
 
29
- | Option | Type | Default | Description |
30
- | ------------------- | --------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
31
- | `enableSmartyPants` | `boolean` | `true` | Typographic enhancements: curly quotes, en/em dashes, ellipses. |
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). |
29
+ | Option | Type | Default | Description |
30
+ | ------------------- | --------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
31
+ | `enableSmartyPants` | `boolean` | `true` | Typographic enhancements: curly quotes, en/em dashes, ellipses. |
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
+ | `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, math and autolinks, before SmartyPants; the longest match wins. Empty: neither GitHub nor Obsidian replaces anything. See [Arrows](#arrows). |
35
+
36
+ ## Arrows
37
+
38
+ Quartz 4's Obsidian Flavored Markdown turned typed arrows into arrow characters (`parseArrows`). Obsidian doesn't, so Quartz 5 leaves them as typed. To render them as Quartz 4 did:
39
+
40
+ ```yaml title="quartz.config.yaml"
41
+ plugins:
42
+ - source: "@quartz-community/github-flavored-markdown"
43
+ options:
44
+ replacements:
45
+ "->": "→"
46
+ "-->": "⇒"
47
+ "=>": "⇒"
48
+ "==>": "⇒"
49
+ "<-": "←"
50
+ "<--": "⇐"
51
+ "<=": "⇐"
52
+ "<==": "⇐"
53
+ ```
54
+
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.
33
58
 
34
59
  ## Heading ids
35
60
 
36
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:
37
62
 
38
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"`.
39
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.
40
66
  - A same-page link written as a heading's text, such as `[x](<#My Heading>)`, points at the heading's id.
41
67
 
package/dist/index.d.ts CHANGED
@@ -4,6 +4,18 @@ export { QuartzTransformerPlugin } from '@quartz-community/types';
4
4
  interface GfmOptions {
5
5
  enableSmartyPants: boolean;
6
6
  linkHeadings: boolean;
7
+ /**
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`):
10
+ * `{ "->": "→", "=>": "⇒" }` brings back Quartz 4's `parseArrows`. Empty by default: neither
11
+ * GitHub nor Obsidian replaces anything.
12
+ */
13
+ replacements: Record<string, string>;
14
+ /**
15
+ * Strike text between single tildes (`~text~`), as GitHub does. `false` strikes only
16
+ * `~~text~~`, as Obsidian does, so `about ~20 W and (~€120)` stays as written.
17
+ */
18
+ singleTilde: boolean;
7
19
  }
8
20
  declare const GitHubFlavoredMarkdown: QuartzTransformerPlugin<Partial<GfmOptions>>;
9
21
 
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);
@@ -6682,74 +6694,152 @@ function rehypeHeadingIds() {
6682
6694
  };
6683
6695
  }
6684
6696
 
6697
+ // src/textReplacements.ts
6698
+ function remarkTextReplacements(replacements) {
6699
+ const keys2 = Object.keys(replacements).filter((key) => key !== "").sort((a, b) => b.length - a.length);
6700
+ if (keys2.length === 0) return () => {
6701
+ };
6702
+ const pattern = new RegExp(
6703
+ keys2.map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"),
6704
+ "g"
6705
+ );
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);
6727
+ };
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;
6768
+ }
6769
+
6685
6770
  // src/transformer.ts
6686
6771
  var defaultOptions = {
6687
6772
  enableSmartyPants: true,
6688
- linkHeadings: true
6773
+ linkHeadings: true,
6774
+ replacements: {},
6775
+ singleTilde: true
6689
6776
  };
6690
6777
  var GitHubFlavoredMarkdown = (userOpts) => {
6691
6778
  const opts = { ...defaultOptions, ...userOpts };
6692
6779
  return {
6693
6780
  name: "GitHubFlavoredMarkdown",
6694
6781
  markdownPlugins() {
6695
- if (!opts.enableSmartyPants) return [remarkGfm];
6696
- return opts.linkHeadings ? [remarkGfm, remarkRememberHeadingText, plugin_default, remarkRecordHeadingRewrites] : [remarkGfm, plugin_default];
6782
+ const gfm2 = [remarkGfm, { singleTilde: opts.singleTilde }];
6783
+ const rewrites = [];
6784
+ if (Object.keys(opts.replacements).length > 0)
6785
+ rewrites.push([remarkTextReplacements, opts.replacements]);
6786
+ if (opts.enableSmartyPants) rewrites.push(plugin_default);
6787
+ if (rewrites.length === 0) return [gfm2];
6788
+ return [gfm2, remarkRememberHeadingText, ...rewrites, remarkRecordHeadingRewrites];
6697
6789
  },
6698
6790
  htmlPlugins() {
6699
- if (opts.linkHeadings) {
6700
- return [
6701
- rehypeHeadingIds,
6702
- [
6703
- rehypeAutolinkHeadings,
6704
- {
6705
- 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",
6706
6811
  properties: {
6707
- role: "anchor",
6708
- // A string: `true` renders as a bare `aria-hidden`, which isn't
6709
- // "true", so the icon link was exposed as a link without a name
6710
- // and an unknown role.
6711
- ariaHidden: "true",
6712
- tabIndex: -1,
6713
- "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"
6714
6820
  },
6715
- content: {
6716
- type: "element",
6717
- tagName: "svg",
6718
- properties: {
6719
- width: 18,
6720
- height: 18,
6721
- viewBox: "0 0 24 24",
6722
- fill: "none",
6723
- stroke: "currentColor",
6724
- "stroke-width": "2",
6725
- "stroke-linecap": "round",
6726
- "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: []
6727
6829
  },
6728
- children: [
6729
- {
6730
- type: "element",
6731
- tagName: "path",
6732
- properties: {
6733
- d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"
6734
- },
6735
- 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"
6736
6835
  },
6737
- {
6738
- type: "element",
6739
- tagName: "path",
6740
- properties: {
6741
- d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"
6742
- },
6743
- children: []
6744
- }
6745
- ]
6746
- }
6836
+ children: []
6837
+ }
6838
+ ]
6747
6839
  }
6748
- ]
6749
- ];
6750
- } else {
6751
- return [];
6752
- }
6840
+ }
6841
+ ]
6842
+ ];
6753
6843
  }
6754
6844
  };
6755
6845
  };