@quartz-community/remark-obsidian 2.1.0 → 2.2.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.
@@ -160,3 +160,50 @@ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
160
160
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
161
161
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
162
162
  ```
163
+
164
+ ## npm packages bundled into `dist`
165
+
166
+ esbuild bundles these from `node_modules` into `dist/index.js`, at the versions
167
+ `package-lock.json` pins. They are separate from the vendored `character-entities` 1.x and
168
+ `micromark-extension-math` source above: `dist` holds both. Each is MIT:
169
+
170
+ - `character-entities`, `character-entities-legacy`, `fault`, `mdast-util-to-string`,
171
+ `unist-util-is` -- Copyright (c) 2015 Titus Wormer <tituswormer@gmail.com>
172
+ - `longest-streak` -- Copyright (c) 2015 Titus Wormer <mailto:tituswormer@gmail.com>
173
+ - `unist-util-visit-parents` -- Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
174
+ - `mdast-util-frontmatter`, `mdast-util-math`, `micromark-extension-frontmatter`,
175
+ `micromark-extension-math` -- Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
176
+ - `devlop` -- Copyright (c) 2023 Titus Wormer <tituswormer@gmail.com>
177
+ - `decode-named-character-reference`, `micromark-core-commonmark`, `micromark-factory-space`,
178
+ `micromark-util-character`,
179
+ `micromark-util-decode-numeric-character-reference`, `micromark-util-decode-string`,
180
+ `micromark-util-normalize-identifier`, `micromark-util-symbol` -- Copyright (c) Titus Wormer
181
+ <tituswormer@gmail.com>
182
+ - `escape-string-regexp` -- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com>
183
+ (https://sindresorhus.com)
184
+ - `format` -- Copyright 2010 - 2014 Sami Samhuri <sami@samhuri.net> (from its `Readme.md`, which
185
+ links the MIT licence, as do `format.js`' header and `package.json`. The 0.2.2 tarball carries no
186
+ licence file; the upstream repository's `License.md`, added later, is MIT from the same holder)
187
+
188
+ ```
189
+ (The MIT License)
190
+
191
+ Permission is hereby granted, free of charge, to any person obtaining
192
+ a copy of this software and associated documentation files (the
193
+ 'Software'), to deal in the Software without restriction, including
194
+ without limitation the rights to use, copy, modify, merge, publish,
195
+ distribute, sublicense, and/or sell copies of the Software, and to
196
+ permit persons to whom the Software is furnished to do so, subject to
197
+ the following conditions:
198
+
199
+ The above copyright notice and this permission notice shall be
200
+ included in all copies or substantial portions of the Software.
201
+
202
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
203
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
205
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
206
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
207
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
208
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
209
+ ```
package/README.md CHANGED
@@ -70,26 +70,49 @@ emphasis followed by `*` under `strict`, as in CommonMark.
70
70
 
71
71
  ## Options
72
72
 
73
- Every option is a boolean, works under both presets, and wins over the preset's value for it.
74
-
75
- | option | default | what it does |
76
- | ------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
77
- | `preset` | `"legacy"` | `"legacy"` or `"strict"`, as above |
78
- | `wikilinks` | on | `[[target#heading\|alias]]` and `![[embed]]` as `wikilink` nodes |
79
- | `highlights` | on | `==text==` as `highlight` |
80
- | `tags` | on | `#tag` as `tag` |
81
- | `math` | on | `$inline$` and `$$display$$` |
82
- | `comments` | on | `%%comment%%` |
83
- | `keepComments` | on | keep a comment as a `comment` node rather than deleting it; it renders as an empty `<obsidian-comment>`, which `rehype-obsidian` removes |
84
- | `parseComments` | on | parse a kept comment's body into its `children`, so a reference inside one is a `footnoteReference` |
85
- | `callouts` | on | `> [!type] Title` as a `callout` holding a `calloutTitle` |
86
- | `inlineFootnotes` | on | `^[note]` as a `footnote` |
87
- | `customTaskChars` | on | any one character in a task box is a checkbox (`- [-]`), kept as `data.taskChar` |
88
- | `frontmatter` | on | a leading `---` block as a `yaml` node |
89
- | `bareFootnoteRefs` | on under `legacy`, off under `strict` | Obsidian renders an undefined `[^x]` as `x`; the node stays a `footnoteReference`, and `rehype-obsidian` drops the delimiters when rendering |
90
- | `positionedBreaks` | on under `legacy`, off under `strict` | a soft line ending becomes a positioned `break`, as `remark-breaks` would make it |
91
- | `lineOffsets` | on | `root.data.lineOffsets`: how many columns each line's containers consume |
92
- | `escapedLineBreaks` | off | write a `break` back as a trailing backslash rather than a newline, for a chain without `remark-breaks` |
73
+ Every option is a boolean, works under both presets (`readsPastEnd` and `keepUndefinedReferences` are `legacy`'s alone), and wins
74
+ over the preset's value for it.
75
+
76
+ | option | default | what it does |
77
+ | ------------------------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
78
+ | `preset` | `"legacy"` | `"legacy"` or `"strict"`, as above |
79
+ | `wikilinks` | on | `[[target#heading\|alias]]` and `![[embed]]` as `wikilink` nodes |
80
+ | `highlights` | on | `==text==` as `highlight` |
81
+ | `tags` | on | `#tag` as `tag` |
82
+ | `math` | on | `$inline$` and `$$display$$` |
83
+ | `comments` | on | `%%comment%%` |
84
+ | `keepComments` | on | keep a comment as a `comment` node rather than deleting it; it renders as an empty `<obsidian-comment>`, which `rehype-obsidian` removes |
85
+ | `parseComments` | on | parse a kept comment's body into its `children`, so a reference inside one is a `footnoteReference` |
86
+ | `callouts` | on | `> [!type] Title` as a `callout` holding a `calloutTitle`, with the marker's span in `data.marker` and its line's end in `data.lineEnd` |
87
+ | `inlineFootnotes` | on | `^[note]` as a `footnote` |
88
+ | `inertFootnoteRefs` | off | keep a reference inside `^[...]`, which Obsidian does not read as one, as a `footnoteReference` with `data: { inert: true }`, for a consumer reading the tree; a chain that renders should leave it off |
89
+ | `customTaskChars` | on | any one character in a task box is a checkbox (`- [-]`), kept as `data.taskChar` |
90
+ | `frontmatter` | on | a leading `---` block as a `yaml` node |
91
+ | `bareFootnoteRefs` | on under `legacy`, off under `strict` | Obsidian renders an undefined `[^x]` as `x`; the node stays a `footnoteReference`, and `rehype-obsidian` drops the delimiters when rendering |
92
+ | `keepUndefinedReferences` | off | `legacy` only: keep a link or image reference this parse found no definition for as a `linkReference` or `imageReference` with `data: { defined: false }`, rather than text, for a consumer parsing part of a note or a table cell alone; a chain that renders should leave it off |
93
+ | `positionedBreaks` | on under `legacy`, off under `strict` | a soft line ending becomes a positioned `break`, as `remark-breaks` would make it |
94
+ | `lineOffsets` | on | `root.data.lineOffsets`: how many columns each line's containers consume |
95
+ | `readsPastEnd` | off | `root.data.readsPastEnd`, `legacy` only: would a top-level link definition read differently with more lines after the end? Trust `false` only for a part read with its next line attached (see the option's doc) |
96
+ | `escapedLineBreaks` | off | write a `break` back as a trailing backslash rather than a newline, for a chain without `remark-breaks` |
97
+ | `bomOffsets` | off | count a leading byte order mark in positions, so an offset indexes the parsed string; off, they match Obsidian's cache, which leaves it out |
98
+
99
+ ## Reading without a processor
100
+
101
+ A consumer that only reads, such as an Obsidian plugin, can skip `unified` and everything the
102
+ `legacy` preset does not run:
103
+
104
+ ```js
105
+ import { parse } from "@quartz-community/remark-obsidian/legacy";
106
+
107
+ const tree = parse("A [[note|link]], ==a highlight== and a #tag.\n");
108
+ const part = parse(laterPartOfANote, { frontmatter: false });
109
+ ```
110
+
111
+ `parse(text, options)` returns the tree `unified().use(remarkObsidian, options).parse(text)`
112
+ returns, with the same defaults. It takes the options above except `preset` and
113
+ `escapedLineBreaks`. The main entry holds both presets and every writer in one function, so a
114
+ bundler cannot drop any of them from it; this entry reaches only the `legacy` reader, which took
115
+ one plugin's minified bundle from 519,667 to 367,710 bytes.
93
116
 
94
117
  ## Upgrading from 1.x
95
118
 
@@ -102,7 +125,7 @@ comments. Nothing was removed from the API.
102
125
  ## Obsidian parity
103
126
 
104
127
  This plugin targets the **reading-mode and `metadataCache` semantics of a specific public
105
- Obsidian release**, currently 1.14.5. A harness renders a generated corpus inside real Obsidian
128
+ Obsidian release**, currently 1.15.0. A harness renders a generated corpus inside real Obsidian
106
129
  and compares the result against ours, so the claim is measured rather than asserted —
107
130
  including the divergences, which are enumerated rather than implied:
108
131
 
@@ -110,12 +133,27 @@ including the divergences, which are enumerated rather than implied:
110
133
  two are a rendering difference between Obsidian's own two renderers and an embed that
111
134
  resolves a note into itself.
112
135
  - **On 6,844 notes whose footnote answers Obsidian's cache recorded**, `legacy` agrees on 6,830,
113
- down to every definition's and reference's exact start and end offsets. The 14 left are
114
- places where the cache misplaces a fact or the answers' recording added one.
136
+ down to every definition's and reference's exact start and end offsets, as 1.15.0 reported
137
+ them. The 14 left are places where the cache misplaces a fact or the answers' recording added
138
+ one.
115
139
  - **`strict` matches `remark-parse` and `remark-gfm`** on every CommonMark and GFM spec example
116
140
  except eight: six that hold Obsidian syntax, and two task lists where it adds
117
141
  `data.taskChar`. It answers 513 live probes
118
- about Obsidian's syntax and footnotes as Obsidian does, bar a few recorded exclusions.
142
+ about Obsidian's syntax and footnotes as Obsidian 1.15.0 does, bar a few recorded exclusions.
143
+
144
+ ## Known limits
145
+
146
+ Very deep nesting overflows the call stack somewhere, as in any recursive parser or writer. On
147
+ Node's default stack:
148
+
149
+ - **Parsing:** under `legacy`, remark-parse 8 itself recurses, to about 1,400 nested list items
150
+ or 1,700 nested quotes. Under `strict`, `unist-util-visit` does, to about 2,800 list items or
151
+ 5,700 quotes.
152
+ - **Formatting** with `remark-stringify`: `mdast-util-to-markdown` recurses at every level it
153
+ writes, to about 520 nested list items or 1,300 nested quotes, under either preset. Stock
154
+ `remark-stringify` stops at about the same depth.
155
+
156
+ Past these depths the call throws `RangeError: Maximum call stack size exceeded`.
119
157
 
120
158
  ## Development
121
159
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Literal, Parent, RootContent, Root, Nodes } from 'mdast';
1
+ import { Literal, Parent, RootContent, Data, Root, Nodes } from 'mdast';
2
+ import { Position, Point } from 'unist';
2
3
  import { Extension } from 'micromark-util-types';
3
4
  import { Extension as Extension$1 } from 'mdast-util-from-markdown';
4
5
  import { Options } from 'mdast-util-to-markdown';
@@ -53,16 +54,39 @@ interface InlineFootnote extends Parent {
53
54
  interface CalloutTitle extends Parent {
54
55
  type: "calloutTitle";
55
56
  }
57
+ /**
58
+ * Where a callout's marker and title line sit. Offsets count as every position does: a leading
59
+ * byte order mark is left out, as Obsidian's cache leaves it out.
60
+ */
61
+ interface CalloutData extends Data {
62
+ /** The marker as written, `[!type]` and its fold character. */
63
+ marker?: Position;
64
+ /** Where the marker's line ends. A title and any block on that line lie before it. */
65
+ lineEnd?: Point;
66
+ }
56
67
  interface Callout extends Parent {
57
68
  type: "callout";
58
69
  calloutType: string;
59
70
  title: string;
60
71
  collapsed: boolean | null;
72
+ data?: CalloutData;
61
73
  }
62
74
  declare module "mdast" {
63
75
  interface ListItemData {
64
76
  taskChar?: string;
65
77
  }
78
+ interface FootnoteReferenceData {
79
+ /** `true` on a reference `inertFootnoteRefs` kept inside an inline footnote: it refers to nothing. */
80
+ inert?: true;
81
+ }
82
+ interface LinkReferenceData {
83
+ /** `false` on a reference `keepUndefinedReferences` kept: this parse found no definition. */
84
+ defined?: false;
85
+ }
86
+ interface ImageReferenceData {
87
+ /** `false` on a reference `keepUndefinedReferences` kept: this parse found no definition. */
88
+ defined?: false;
89
+ }
66
90
  interface RootContentMap {
67
91
  wikilink: Wikilink;
68
92
  highlight: Highlight;
@@ -82,6 +106,11 @@ declare module "mdast" {
82
106
  * Only present when the `lineOffsets` option is on.
83
107
  */
84
108
  lineOffsets?: Record<number, number>;
109
+ /**
110
+ * Whether a top-level link definition would read differently with more lines after the end
111
+ * of this text. Only present when the `readsPastEnd` option is on, under `legacy`.
112
+ */
113
+ readsPastEnd?: boolean;
85
114
  }
86
115
  interface PhrasingContentMap {
87
116
  wikilink: Wikilink;
@@ -308,14 +337,67 @@ interface RemarkObsidianOptions {
308
337
  * parser keeps the same table, so reproducing its reported columns needs it.
309
338
  */
310
339
  lineOffsets?: boolean;
340
+ /**
341
+ * Record on the root as `data.readsPastEnd` whether a top-level link definition would read
342
+ * differently with more lines after the end of this text, for a caller parsing a note in parts.
343
+ * remark-parse 8 reads a definition past blank lines, so `[foo]: /url` alone is a definition
344
+ * and above `'tit` and a blank line is a paragraph.
345
+ *
346
+ * `false` is a promise only for a part cut at one of the whole note's block boundaries and read
347
+ * with its next line attached, ending where that line starts a top-level block: then the part
348
+ * reads as that stretch of the note does (held over the corpus and generated documents in
349
+ * `reads-past-end.test.ts`). Read without its next line, a part can differ with the signal
350
+ * `false` (`> [!note] t` over `2) b` or `---`).
351
+ *
352
+ * It speaks for link definitions at a part's end, nothing else. Frontmatter reads to its closer
353
+ * too, an `<a>` opened above a part's start changes what the part reads, and a part's references
354
+ * depend on definitions outside it: those stay with the caller.
355
+ *
356
+ * `legacy` only, and absent under `strict`. A CommonMark definition stops at a blank line, but
357
+ * `strict` reads other blocks differently when cut, so a constant `false` would promise too much.
358
+ */
359
+ readsPastEnd?: boolean;
311
360
  /**
312
361
  * Strip the delimiters from a footnote reference nothing defines, so `see [^x]`
313
362
  * reads `see x` as Obsidian renders it. GFM keeps an unmatched reference literal,
314
363
  * so this is a deliberate divergence from the spec in favour of matching the app.
315
364
  */
316
365
  bareFootnoteRefs?: boolean;
366
+ /**
367
+ * Keep a link or image reference whose label this parse found no definition for as a
368
+ * `linkReference` or `imageReference` with `data: { defined: false }`, rather than the text
369
+ * it reads as by default: `[a] b` holds a reference to `a`, not the text `[a] b`.
370
+ *
371
+ * For a consumer parsing part of a note, or a table cell alone, where the definition may be
372
+ * somewhere this parse never saw. Whether one is really a link is for the consumer to decide
373
+ * against the labels it knows, by `label` with case and runs of whitespace folded: Obsidian does
374
+ * not trim a label, so `[ a ]` is not `[a]`, where `identifier` trims.
375
+ *
376
+ * A chain that renders should leave it off. `mdast-util-to-hast` resolves by `identifier`, so
377
+ * `[ a ]` over `[a]: /url` becomes a link. An unclosed `[` the reader takes into a shortcut
378
+ * reference (`[a][ b`) is given back as a text sibling, so it still renders.
379
+ *
380
+ * Link and image references only: an undefined `[^x]` stays text unless `bareFootnoteRefs` is
381
+ * on, as without this option.
382
+ *
383
+ * `legacy` only, and does nothing under `strict`. remark-parse 8 reads a reference whatever
384
+ * defines it. micromark reads none, and read back from the text, some are references the note
385
+ * with that label defined does not hold (`x ^[^[note] y`).
386
+ */
387
+ keepUndefinedReferences?: boolean;
317
388
  /** Parse Obsidian's `^[body]` inline footnote, which GFM has no form for. */
318
389
  inlineFootnotes?: boolean;
390
+ /**
391
+ * Keep a footnote reference inside an inline footnote as a `footnoteReference` with
392
+ * `data: { inert: true }`, rather than the text it reads as by default. Obsidian reads no
393
+ * reference there, so the node refers to nothing; it is for a consumer that needs to see one,
394
+ * a linter reserving a name, say, without re-scanning the text. Like `parseComments`, which
395
+ * does this for `%%`. Needs `inlineFootnotes`.
396
+ *
397
+ * For a chain that reads the tree. `mdast-util-to-hast` renders every `footnoteReference` as a
398
+ * live call, so a chain that renders should leave this off.
399
+ */
400
+ inertFootnoteRefs?: boolean;
319
401
  /**
320
402
  * Promote `> [!type] Title` blockquotes into `callout` nodes. Obsidian reports
321
403
  * these as their own block type, so a blockquote here is a divergence rather
@@ -346,6 +428,15 @@ interface RemarkObsidianOptions {
346
428
  * `remark-breaks` and a `break` really is a hard break.
347
429
  */
348
430
  escapedLineBreaks?: boolean;
431
+ /**
432
+ * Count a leading byte order mark in every position and in `data.lineOffsets`, so an offset
433
+ * indexes the string that was parsed, and read the rest as if the mark were not there.
434
+ *
435
+ * Off by default, because Obsidian's `metadataCache` leaves the mark out: its offsets and
436
+ * first-line columns are one short of the editor's, and off is what the parity gates measure.
437
+ * Turn it on to edit the string by position.
438
+ */
439
+ bomOffsets?: boolean;
349
440
  }
350
441
  /**
351
442
  * Which body of behaviour to follow where Obsidian and CommonMark disagree.
@@ -374,4 +465,4 @@ type RemarkObsidianPreset = "legacy" | "strict";
374
465
  type RemarkObsidianFlags = Required<Omit<RemarkObsidianOptions, "preset">>;
375
466
  declare function remarkObsidian(userOpts?: RemarkObsidianOptions): undefined | ((tree: Root, file: any) => void);
376
467
 
377
- export { COMMENT_ELEMENT, type Callout, type CalloutTitle, type CollectWikilinksOptions, type Comment, type Highlight, type InlineFootnote, type RemarkObsidianFlags, type RemarkObsidianOptions, type RemarkObsidianPreset, type Tag, type Wikilink, bareFootnoteRefTransform, calloutTransform, collectEmbedPaths, collectWikilinks, commentFromMarkdown, commentSyntax, commentTailTransform, commentToMarkdown, customTaskCharTransform, remarkObsidian as default, highlightFromMarkdown, highlightSyntax, highlightToMarkdown, inlineFootnoteSyntax, isEmbed, isWikilink, mathDisplayTransform, tagFromMarkdown, tagSyntax, tagToMarkdown, taskCharToMarkdown, wikilinkFromMarkdown, wikilinkSyntax, wikilinkToMarkdown };
468
+ export { COMMENT_ELEMENT, type Callout, type CalloutData, type CalloutTitle, type CollectWikilinksOptions, type Comment, type Highlight, type InlineFootnote, type RemarkObsidianFlags, type RemarkObsidianOptions, type RemarkObsidianPreset, type Tag, type Wikilink, bareFootnoteRefTransform, calloutTransform, collectEmbedPaths, collectWikilinks, commentFromMarkdown, commentSyntax, commentTailTransform, commentToMarkdown, customTaskCharTransform, remarkObsidian as default, highlightFromMarkdown, highlightSyntax, highlightToMarkdown, inlineFootnoteSyntax, isEmbed, isWikilink, mathDisplayTransform, tagFromMarkdown, tagSyntax, tagToMarkdown, taskCharToMarkdown, wikilinkFromMarkdown, wikilinkSyntax, wikilinkToMarkdown };