@mirrordown/remd-spoiler 0.1.0 → 0.1.2

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,102 @@
1
1
  # Changelog
2
2
 
3
+
4
+
5
+ ## 0.1.2
6
+ <sub>2026-11-17</sub>
7
+
8
+ - *(patch)*
9
+ Fixes for cases where a `||spoiler||` formed in one engine and not another, which either hides text you meant to show or shows text you meant to hide.
10
+
11
+ - **Without `spoilerHastHandlers`, the remark plugin keeps the content covered** (remark). The handlers are still required for a spoiler that can be revealed; this is what happens when they are not passed to `remark-rehype`.
12
+
13
+ ```markdown
14
+ a ||b|| c
15
+ ```
16
+
17
+ Was `a <div>b</div> c`, the content in the clear. Now `a <span class="markdown-spoiler-content">b</span> c`, which the stylesheet covers and nothing reveals.
18
+
19
+ - **A spoiler in a table cell forms** (remark, Sätteri). In a cell the pipes have to be written `\|`.
20
+
21
+ ```markdown
22
+ | h |
23
+ | - |
24
+ | \|\|x\|\| |
25
+ ```
26
+
27
+ Was the text `||x||` in the cell. Now a spoiler, as in markdown-it.
28
+
29
+ - **An attribute block written right after a spoiler is read and dropped** (Sätteri, with its `attributes` feature on).
30
+
31
+ ```markdown
32
+ ||secret||{.danger}
33
+ ```
34
+
35
+ Was `<p class="danger">` around the spoiler, and mid-paragraph the block stayed on the page as text. Now the paragraph has no class and the block is gone, as in markdown-it and remark.
36
+
37
+ - **Escaped pipes stay text after another plugin's span** (remark, Sätteri).
38
+
39
+ ```markdown
40
+ ==a== \||b||
41
+ ```
42
+
43
+ Was a spoiler around `b`. Now `<mark>a</mark> ||b||`, as in markdown-it.
44
+
45
+ - **An escape is still honoured after smart punctuation** (Sätteri, with smart punctuation on).
46
+
47
+ ```markdown
48
+ "q" \||a||
49
+ ```
50
+
51
+ Was a spoiler around `a`. Now `“q” ||a||`.
52
+
53
+ - **A pipe written as a character reference is not a delimiter** (remark, Sätteri).
54
+
55
+ ```markdown
56
+ &#124;&#124;text||
57
+ ```
58
+
59
+ Was a spoiler around `text`. Now the text `||text||`.
60
+
61
+ - **The text of an autolink is left alone** (remark, Sätteri).
62
+
63
+ ```markdown
64
+ <http://x/||b||>
65
+ ```
66
+
67
+ Was a link with a spoiler around `b` in its text. Now the link text is the URL as written.
68
+
69
+ - **An escaped character or a character reference beside the pipes counts as punctuation** (remark, Sätteri). This decides whether a pair of pipes can open or close.
70
+
71
+ ```markdown
72
+ a||\*b||
73
+ ```
74
+
75
+ Was `a` and a spoiler around `*b`. Now the text `a||*b||`, as in markdown-it.
76
+
77
+ - **A backslash line break right after the opening pipes does not stop them from opening** (remark, Sätteri).
78
+
79
+ ```markdown
80
+ (||\
81
+ a||
82
+ ```
83
+
84
+ Now a spoiler around the line break and `a`, as in markdown-it.
85
+
86
+ - **Spoilers nest up to 50 deep** (all engines). The pipes of deeper ones stay text. Sätteri used to reject the whole document when spoilers nested too deep: each one counts twice toward its limit of 128 levels.
87
+
88
+ The guide now lists the differences between engines that remain.
89
+
90
+ ## 0.1.1
91
+ <sub>2026-10-28</sub>
92
+
93
+ - *(patch)*
94
+ Backslash-escaped delimiters are now literal in `remd-mark`, `remd-ins`, `remd-del`, `remd-kbd`, `remd-spoiler`, `remd-sub` and `remd-sup`, as they already were in the `mdit-*` plugins: `\==not marked==` renders as `==not marked==` instead of a `<mark>`, while an escaped backslash (`\==marked==`) still allows the markup. These plugins now depend on the new `@mirrordown/core` package, which holds logic shared across Mirrordown's engines.
95
+ - *(patch)*
96
+ `||spoiler||` now pairs delimiters with markdown-it's rules in every engine: `remd-spoiler` runs on `@mirrordown/core`'s delimiter scan (which gains the `|` marker), so a closer pairs with the nearest opener, spoilers nest, and an odd-length run's extra `|` stays outside; it drops its `unist-util-find-*` and `unist-builder` dependencies.
97
+
98
+ New `@mirrordown/sat-spoiler` renders the same JavaScript-free click-to-reveal markup, ships the same stylesheet, and is included in `@mirrordown/preset-satteri`.
99
+
3
100
  ## 0.1.0
4
101
  <sub>2026-07-01</sub>
5
102
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @mirrordown/remd-spoiler
2
2
 
3
- > Part of [Mirrordown](https://github.com/mirrordown/mirrordown) — a suite of markdown syntax extensions for the unified and markdown-it ecosystems.
3
+ > Part of [Mirrordown](https://github.com/mirrordown/mirrordown) — a suite of markdown syntax extensions for the unified, markdown-it and Sätteri ecosystems.
4
4
 
5
5
  A remark/rehype (unified) plugin for the `spoiler` syntax extension.
6
6
 
@@ -17,67 +17,80 @@ The entire interaction is built from HTML and CSS only — a `<label>` wrapping
17
17
  </label>
18
18
  ```
19
19
 
20
- The checkbox is the only interactive control, so it stays keyboard-operable and is announced by screen readers as a spoiler control — the hidden text is not read aloud until the reader chooses to reveal it.
20
+ The checkbox is the only interactive control, so it stays keyboard-operable and is announced by screen readers as a spoiler control. The stylesheet hides text by making it transparent on the bar, and hides elements inside the spoiler (emphasis, links, code) with `visibility: hidden`: plain text stays in the page, so it is hidden from sight and not necessarily from a screen reader.
21
21
 
22
22
  ## Syntax
23
23
 
24
24
  Wrap any inline content in `||…||`. Click a bar below to reveal it; click again to hide it.
25
25
 
26
- ```markdown
26
+ ````markdown
27
27
  The killer was ||the butler|| all along, hidden in ||the conservatory||.
28
- ```
28
+ ````
29
29
 
30
30
  In renderers without the plugin, `||…||` renders as plain text.
31
31
 
32
+ A backslash keeps a delimiter literal (`\||not hidden||`), and so does writing a pipe as a character reference (`&#124;`). A table cell is the exception: a pipe there has to be written `\|`, so a spoiler in a cell is written `\|\|hidden\|\|`.
33
+
34
+ Spans have to nest. When a spoiler crosses another span or crosses emphasis (`||a *b|| c*`), each engine resolves it its own way, and in remark and Sätteri the result depends on the order the plugins are listed in. Spoilers nest up to 50 deep; the pipes of deeper ones stay text.
35
+
36
+ A spoiler takes no attributes: with the [`attrs`](/guide/plugins/attrs) plugin or Sätteri's `attributes` feature on, an attribute block written right after one (`||text||{.name}`) is read and dropped in every engine.
37
+
38
+ A few things differ between the engines:
39
+
40
+ - In image alt text (`![||a||](x.png)`), markdown-it drops the pipes and remark and Sätteri keep the text as written.
41
+ - The remark plugin reads escapes from the source, so it needs the file: `processor.runSync(tree)` on a tree alone treats `\||` as `||`.
42
+ - In a table cell, two backslashes before a pipe (`\\|`) are a backslash and an escaped pipe to markdown-it, and an escaped backslash and a cell separator to remark and Sätteri, so a row holding them splits into cells differently before any plugin runs.
43
+
32
44
  ### Rich inline content
33
45
 
34
- A spoiler can wrap any inline markup — emphasis, code, links — and it stays obscured until revealed. A long spoiler wraps across lines with each line getting its own bar, just like Discord.
46
+ A spoiler can wrap any inline markup — emphasis, code, links — and it stays obscured until revealed. One exception today: with the [`ruby`](/guide/plugins/ruby) plugin on, braces around text that holds a spoiler are read as a ruby, whose first `|` is its separator, so `{note: ||secret||}` renders `note:` with `|secret||` printed above it. A long spoiler wraps across lines with each line getting its own bar, just like Discord.
35
47
 
36
- ```markdown
48
+ ````markdown
37
49
  The recipe needs ||**two** cups of `flour` and a
38
50
  [secret ingredient](https://example.com) that nobody expects||.
39
- ```
51
+ ````
40
52
 
41
53
  ### Image spoilers
42
54
 
43
55
  Wrap an image to hide it behind a blur and a **SPOILER** label — Discord's treatment for spoilered image embeds. Clicking reveals the sharp image.
44
56
 
45
- ```markdown
57
+ ````markdown
46
58
  Careful, dinner photo incoming: ||![A young sapling](./assets/sapling.jpg "Sapling")||
47
- ```
59
+ ````
48
60
 
49
61
  ### Spoilered, zoomable images
50
62
 
51
- Combine with the [`lightbox`](/guide/plugins/lightbox) plugin (`||!![…]()||`) for a spoilered image that is also click-to-zoom once revealed.
63
+ Combine with the [`lightbox`](/guide/plugins/lightbox) plugin (`||!![…]()||`) for a spoilered image that is also click-to-zoom once revealed. The zoom button sits inside the spoiler and can take keyboard focus of its own, so today <kbd>Tab</kbd> then <kbd>Enter</kbd> opens the image without revealing it first.
52
64
 
53
- ```markdown
65
+ ````markdown
54
66
  The reveal: ||!![Mount Fuji at dawn](./assets/fuji.jpg "Mount Fuji")||
55
- ```
67
+ ````
56
68
 
57
69
  ### Inside links
58
70
 
59
- A spoiler works inside a link — the whole revealed content remains the link target.
71
+ A spoiler works inside a link — the whole revealed content remains the link target. The markup puts the spoiler's `<label>` and checkbox inside the `<a>`.
60
72
 
61
- ```markdown
73
+ ````markdown
62
74
  Read [the ||shocking twist||](https://example.com) at your own risk.
63
- ```
75
+ ````
64
76
 
65
77
  ## Accessibility
66
78
 
67
- The spoiler is a real checkbox with `aria-label="spoiler"`, so assistive technology announces it as a control the user can operate rather than reading the hidden text aloud. Keyboard users <kbd>Tab</kbd> to the spoiler and press <kbd>Space</kbd> to reveal it; a focus ring marks the current spoiler. Revealing is reversible (unlike Discord, which locks open) — a superset of the expected behavior.
79
+ The spoiler is a real checkbox with `aria-label="spoiler"`, so assistive technology announces it as a control the user can operate. Keyboard users <kbd>Tab</kbd> to the spoiler and press <kbd>Space</kbd> to reveal it; a focus ring marks the current spoiler. Revealing is reversible (unlike Discord, which locks open) — a superset of the expected behavior.
68
80
 
69
81
  ## Customization
70
82
 
71
83
  The stylesheet keeps its rules in `@layer markdown-spoiler`, so any unlayered CSS you write overrides them without `!important`. The common knobs are exposed as custom properties — set them on `:root` or any ancestor:
72
84
 
73
- | Property | Default | Description |
74
- | ------------------------------- | ---------------------------------------- | ----------------------------------- |
75
- | `--markdown-spoiler-bar` | `#202225` | Obscured bar color (text spoilers). |
76
- | `--markdown-spoiler-radius` | `0.25rem` | Bar corner radius. |
77
- | `--markdown-spoiler-duration` | `0.15s` | Reveal transition duration. |
78
- | `--markdown-spoiler-revealed` | `color-mix(in srgb, currentColor 8%, …)` | Background behind revealed text. |
79
- | `--markdown-spoiler-media-blur` | `2.5rem` | Blur radius for image spoilers. |
80
- | `--markdown-spoiler-label` | `"SPOILER"` | Text of the image-spoiler pill. |
85
+ | Property | Default | Description |
86
+ | ----------------------------------- | ------------------------------------------- | --------------------------------------- |
87
+ | `--markdown-spoiler-bar` | `#202225` | Obscured bar color (text spoilers). |
88
+ | `--markdown-spoiler-bar-hover` | `--markdown-spoiler-bar` mixed 15% white | Bar color under the pointer. |
89
+ | `--markdown-spoiler-radius` | `0.25rem` | Bar corner radius. |
90
+ | `--markdown-spoiler-duration` | `0.15s` | Reveal transition duration. |
91
+ | `--markdown-spoiler-revealed` | `color-mix(in srgb, currentColor 8%, …)` | Background behind revealed text. |
92
+ | `--markdown-spoiler-media-blur` | `2.5rem` | Blur radius for image spoilers. |
93
+ | `--markdown-spoiler-label` | `"SPOILER"` | Text of the image-spoiler pill. |
81
94
 
82
95
  ```css
83
96
  :root {
@@ -93,7 +106,7 @@ Text spoilers work everywhere (checkbox + sibling selector). Image spoilers use
93
106
 
94
107
  ## Install
95
108
 
96
- Install `@mirrordown/remd-spoiler`. You must also pass `spoilerHastHandlers` to `remarkRehype` and import the stylesheet:
109
+ Install `@mirrordown/remd-spoiler`. You must also pass `spoilerHastHandlers` to `remarkRehype` (without them a spoiler renders as a bar that cannot be revealed) and import the stylesheet:
97
110
 
98
111
  ```sh
99
112
  npm install @mirrordown/remd-spoiler
@@ -127,24 +140,8 @@ import "@mirrordown/remd-spoiler/spoiler.css";
127
140
  export default defineConfig({
128
141
  markdown: {
129
142
  remarkPlugins: [remarkSpoiler],
130
- remarkRehype: { handlers: spoilerHastHandlers }
131
- }
132
- });
133
- ```
134
-
135
- ### VitePress
136
-
137
- ```ts
138
- // .vitepress/config.ts
139
- import { defineConfig } from "vitepress";
140
- import { remarkSpoiler, spoilerHastHandlers } from "@mirrordown/remd-spoiler";
141
- import "@mirrordown/remd-spoiler/spoiler.css";
142
-
143
- export default defineConfig({
144
- markdown: {
145
- remarkPlugins: [remarkSpoiler],
146
- remarkRehype: { handlers: spoilerHastHandlers }
147
- }
143
+ remarkRehype: { handlers: spoilerHastHandlers },
144
+ },
148
145
  });
149
146
  ```
150
147
 
package/dist/index.d.mts CHANGED
@@ -1,15 +1,13 @@
1
1
  import { Plugin } from "unified";
2
2
  import { Data, Parent, PhrasingContent, Root } from "mdast";
3
3
  import { Element, ElementContent } from "hast";
4
-
5
- //#region src/plugin.d.ts
6
4
  /** Optional data carried by a {@link Spoiler} node. */
7
- interface SpoilerData extends Data {}
5
+ export interface SpoilerData extends Data {}
8
6
  /**
9
7
  * An mdast node for `||spoiler||` text, rendered as a click-to-reveal spoiler
10
8
  * (a `<label>` wrapping a checkbox and a content `<span>`).
11
9
  */
12
- interface Spoiler extends Parent {
10
+ export interface Spoiler extends Parent {
13
11
  type: `spoiler`;
14
12
  children: PhrasingContent[];
15
13
  data?: SpoilerData | undefined;
@@ -20,17 +18,11 @@ interface HastState {
20
18
  }
21
19
  type SpoilerHandler = (state: HastState, node: Spoiler) => Element;
22
20
  /** mdast-to-hast handler for spoiler nodes, for use with `remark-rehype`. */
23
- declare const spoilerHastHandlers: Record<string, SpoilerHandler>;
21
+ export declare const spoilerHastHandlers: Record<string, SpoilerHandler>;
24
22
  /**
25
- * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each
26
- * match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via
27
- * `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a
28
- * click-to-reveal spoiler. Emits byte-identical HTML to
29
- * `@mirrordown/mdit-spoiler`.
23
+ * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a click-to-reveal spoiler. The scan is shared with markdown-it's pairing rules and the Sätteri plugin through `@mirrordown/core`, and the output is byte-identical to `@mirrordown/mdit-spoiler`.
30
24
  */
31
- declare const remarkSpoiler: Plugin<[], Root>;
32
- //#endregion
33
- //#region src/augment.d.ts
25
+ export declare const remarkSpoiler: Plugin<[], Root>;
34
26
  declare module "mdast" {
35
27
  interface PhrasingContentMap {
36
28
  spoiler: Spoiler;
@@ -38,7 +30,6 @@ declare module "mdast" {
38
30
  interface RootContentMap {
39
31
  spoiler: Spoiler;
40
32
  }
41
- } //# sourceMappingURL=augment.d.ts.map
42
- //#endregion
43
- export { Spoiler, SpoilerData, remarkSpoiler, spoilerHastHandlers };
33
+ }
34
+ export {};
44
35
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/plugin.ts","../src/augment.ts"],"mappings":";;;;;;UAYiB,WAAA,SAAoB,IAAA;;;;;UAMpB,OAAA,SAAgB,MAAA;EAC/B,IAAA;EACA,QAAA,EAAU,eAAA;EACV,IAAA,GAAO,WAAA;AAAA;AAAA,UAOC,SAAA;EACR,GAAA,GAAM,IAAA,EAAM,OAAA,KAAY,cAAA;EACxB,KAAA,GAAQ,IAAA,EAAM,OAAA,EAAS,EAAA,EAAI,OAAA;AAAA;AAAA,KAExB,cAAA,IAAkB,KAAA,EAAO,SAAA,EAAW,IAAA,EAAM,OAAA,KAAY,OAAA;;cA+C9C,mBAAA,EAAqB,MAAA,SAAe,cAAA;;;;;AAzDhD;;;cAoEY,aAAA,EAAe,MAAA,KAAW,IAAA;;;;YCpF3B,kBAAA;IACR,OAAA,EAAS,OAAA;EAAA;EAAA,UAGD,cAAA;IACR,OAAA,EAAS,OAAA;EAAA;AAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/plugin.ts","../src/augment.ts"],"mappings":";;;;iBAUiB,oBAAoB;;;;;iBAMpB,gBAAgB;EAC/B;EACA,UAAU;EACV,OAAO;;UAOC;EACR,MAAM,MAAM,YAAY;EACxB,QAAQ,MAAM,SAAS,IAAI;;KAExB,kBAAkB,OAAO,WAAW,MAAM,YAAY;;qBAoC9C,qBAAqB,eAAe;;;;qBAOpC,eAAe,WAAW;;YCnE3B;IACR,SAAS;;YAGD;IACR,SAAS"}
package/dist/index.mjs CHANGED
@@ -1,16 +1,7 @@
1
1
  import { visit } from "unist-util-visit";
2
- import { findAllBetween } from "unist-util-find-between-all";
3
- import { findAllBefore } from "unist-util-find-all-before";
4
- import { findAllAfter } from "unist-util-find-all-after";
5
- import { findAfter } from "unist-util-find-after";
6
- import { u } from "unist-builder";
7
- //#region src/plugin.ts
8
- const REGEX = /\|\|(?![\s|])([\s\S]*?)(?<![\s|])\|\|/;
9
- const REGEX_GLOBAL = /\|\|(?![\s|])([\s\S]*?)(?<![\s|])\|\|/g;
10
- const REGEX_STARTING = /\|\|(?![\s]|\|+\s)/;
11
- const REGEX_STARTING_GLOBAL = /\|\|(?![\s]|\|+\s)/g;
12
- const REGEX_ENDING = /(?<!\s|\s\||\s\|\||\s\|\|\||\s\|\|\|\|)\|\|/;
13
- const REGEX_ENDING_GLOBAL = /(?<!\s|\s\||\s\|\||\s\|\|\||\s\|\|\|\|)\|\|/g;
2
+ import { createDelimitedTransform, maskTreeEscapes } from "@mirrordown/core";
3
+ // src/plugin.ts
4
+ const MAX_DEPTH = 50;
14
5
  /**
15
6
  * Build the `<label><input><span>...</span></label>` hast subtree. The checkbox
16
7
  * precedes the content span so CSS's `:checked ~ .content` combinator can reach
@@ -43,88 +34,39 @@ const buildSpoilerHast = (state, node) => {
43
34
  /** mdast-to-hast handler for spoiler nodes, for use with `remark-rehype`. */
44
35
  const spoilerHastHandlers = { spoiler: buildSpoilerHast };
45
36
  /**
46
- * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each
47
- * match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via
48
- * `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a
49
- * click-to-reveal spoiler. Emits byte-identical HTML to
50
- * `@mirrordown/mdit-spoiler`.
37
+ * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a click-to-reveal spoiler. The scan is shared with markdown-it's pairing rules and the Sätteri plugin through `@mirrordown/core`, and the output is byte-identical to `@mirrordown/mdit-spoiler`.
51
38
  */
52
39
  const remarkSpoiler = () => {
53
- const constructSpoilerNode = (children) => ({
54
- type: `spoiler`,
55
- children
56
- });
57
- const visitorFirst = (node, index, parent) => {
58
- /* v8 ignore next */
59
- if (!parent || typeof index === `undefined`) return;
60
- if (!REGEX.test(node.value)) return;
61
- const children = [];
62
- const value = node.value;
63
- let tempValue = ``;
64
- let prevMatchIndex = 0;
65
- let prevMatchLength = 0;
66
- const matches = Array.from(value.matchAll(REGEX_GLOBAL));
67
- for (const match of matches) {
68
- const [matched, spoilerText] = match;
69
- const mIndex = match.index;
70
- const mLength = matched.length;
71
- const textPartIndex = prevMatchIndex + prevMatchLength;
72
- prevMatchIndex = mIndex;
73
- prevMatchLength = mLength;
74
- if (mIndex > textPartIndex) children.push(u(`text`, value.substring(textPartIndex, mIndex)));
75
- children.push(constructSpoilerNode([{
76
- type: `text`,
77
- value: (spoilerText ?? ``).trim()
78
- }]));
79
- tempValue = value.slice(mIndex + mLength);
80
- }
81
- if (tempValue) children.push(u(`text`, tempValue));
82
- if (children.length) parent.children.splice(index, 1, ...children);
83
- };
84
- const visitorSecond = (node, index, parent) => {
85
- /* v8 ignore next */
86
- if (!parent || typeof index === `undefined`) return;
87
- if (!REGEX_STARTING.test(node.value)) return;
88
- const openingNode = node;
89
- const closingNode = findAfter(parent, openingNode, (n) => {
90
- return n.type === `text` && REGEX_ENDING.test(n.value);
40
+ const transform = createDelimitedTransform(`|`, {
41
+ text: (value) => ({
42
+ type: `text`,
43
+ value
44
+ }),
45
+ wrap: (children) => ({
46
+ type: `spoiler`,
47
+ children,
48
+ data: {
49
+ hName: `span`,
50
+ hProperties: { className: [`markdown-spoiler-content`] }
51
+ }
52
+ })
53
+ }, MAX_DEPTH);
54
+ const transformer = (tree, file) => {
55
+ const source = String(file);
56
+ const restoreEscapes = maskTreeEscapes(tree, source);
57
+ const seen = /* @__PURE__ */ new WeakSet();
58
+ visit(tree, `text`, (_node, _index, parent) => {
59
+ if (!parent || seen.has(parent) || transform.built(parent)) return;
60
+ seen.add(parent);
61
+ const children = parent.children;
62
+ const next = transform(children, parent.type, source);
63
+ if (next !== children) parent.children = [...next];
64
+ return 0;
91
65
  });
92
- if (!closingNode) return;
93
- const beforeChildren = findAllBefore(parent, openingNode);
94
- const mainChildren = findAllBetween(parent, openingNode, closingNode);
95
- const afterChildren = findAllAfter(parent, closingNode);
96
- const value = openingNode.value;
97
- const match = Array.from(value.matchAll(REGEX_STARTING_GLOBAL))[0];
98
- /* v8 ignore next */
99
- if (!match) return;
100
- const [matched] = match;
101
- const mIndex = match.index;
102
- const mLength = matched.length;
103
- if (mIndex > 0) beforeChildren.push(u(`text`, value.substring(0, mIndex)));
104
- if (value.length > mIndex + mLength) mainChildren.unshift(u(`text`, value.slice(mIndex + mLength)));
105
- const value_ = closingNode.value;
106
- const match_ = Array.from(value_.matchAll(REGEX_ENDING_GLOBAL))[0];
107
- /* v8 ignore next */
108
- if (!match_) return;
109
- const [matched_] = match_;
110
- const mIndex_ = match_.index;
111
- const mLength_ = matched_.length;
112
- if (mIndex_ > 0) mainChildren.push(u(`text`, value_.substring(0, mIndex_)));
113
- if (value_.length > mIndex_ + mLength_) afterChildren.unshift(u(`text`, value_.slice(mIndex_ + mLength_)));
114
- parent.children = [
115
- ...beforeChildren,
116
- constructSpoilerNode(mainChildren),
117
- ...afterChildren
118
- ];
119
- return index;
120
- };
121
- const transformer = (tree) => {
122
- visit(tree, `text`, visitorFirst);
123
- visit(tree, `text`, visitorSecond);
66
+ restoreEscapes();
124
67
  };
125
68
  return transformer;
126
69
  };
127
- //#endregion
128
70
  export { remarkSpoiler, spoilerHastHandlers };
129
71
 
130
72
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import { visit } from \"unist-util-visit\";\nimport type { Visitor, VisitorResult } from \"unist-util-visit\";\nimport type { Plugin, Transformer } from \"unified\";\nimport type { Data, Parent, PhrasingContent, Root, Text } from \"mdast\";\nimport type { Element, ElementContent, Properties } from \"hast\";\nimport { findAllBetween } from \"unist-util-find-between-all\";\nimport { findAllBefore } from \"unist-util-find-all-before\";\nimport { findAllAfter } from \"unist-util-find-all-after\";\nimport { findAfter } from \"unist-util-find-after\";\nimport { u } from \"unist-builder\";\n\n/** Optional data carried by a {@link Spoiler} node. */\nexport interface SpoilerData extends Data {}\n\n/**\n * An mdast node for `||spoiler||` text, rendered as a click-to-reveal spoiler\n * (a `<label>` wrapping a checkbox and a content `<span>`).\n */\nexport interface Spoiler extends Parent {\n type: `spoiler`;\n children: PhrasingContent[];\n data?: SpoilerData | undefined;\n}\n\n// Minimal hast-handler surface. remark-rehype's real `State`/`Handler` types\n// carry the whole conversion API; we only need `all()` to convert this node's\n// mdast children into hast. Typed locally to avoid a dependency on\n// mdast-util-to-hast just for two type names.\ninterface HastState {\n all: (node: Spoiler) => ElementContent[];\n patch: (from: Spoiler, to: Element) => void;\n}\ntype SpoilerHandler = (state: HastState, node: Spoiler) => Element;\n\n// Twin of the marker matching in mark: doubled sigil, no leading/trailing\n// whitespace or extra sigil chars (so `a || b` and `|||` runs don't misfire).\nconst REGEX = /\\|\\|(?![\\s|])([\\s\\S]*?)(?<![\\s|])\\|\\|/;\nconst REGEX_GLOBAL = /\\|\\|(?![\\s|])([\\s\\S]*?)(?<![\\s|])\\|\\|/g;\n\nconst REGEX_STARTING = /\\|\\|(?![\\s]|\\|+\\s)/;\nconst REGEX_STARTING_GLOBAL = /\\|\\|(?![\\s]|\\|+\\s)/g;\n\nconst REGEX_ENDING = /(?<!\\s|\\s\\||\\s\\|\\||\\s\\|\\|\\||\\s\\|\\|\\|\\|)\\|\\|/;\nconst REGEX_ENDING_GLOBAL = /(?<!\\s|\\s\\||\\s\\|\\||\\s\\|\\|\\||\\s\\|\\|\\|\\|)\\|\\|/g;\n\n/**\n * Build the `<label><input><span>...</span></label>` hast subtree. The checkbox\n * precedes the content span so CSS's `:checked ~ .content` combinator can reach\n * it; the content is a following sibling, not a child, of the checkbox.\n */\nconst buildSpoilerHast = (state: HastState, node: Spoiler): Element => {\n const inputProps: Properties = {\n type: `checkbox`,\n class: `markdown-spoiler-toggle`,\n \"aria-label\": `spoiler`\n };\n const input: Element = {\n type: `element`,\n tagName: `input`,\n properties: inputProps,\n children: []\n };\n const content: Element = {\n type: `element`,\n tagName: `span`,\n properties: { class: `markdown-spoiler-content` },\n children: state.all(node)\n };\n const result: Element = {\n type: `element`,\n tagName: `label`,\n properties: { class: `markdown-spoiler` },\n children: [input, content]\n };\n state.patch(node, result);\n return result;\n};\n\n/** mdast-to-hast handler for spoiler nodes, for use with `remark-rehype`. */\nexport const spoilerHastHandlers: Record<string, SpoilerHandler> = {\n spoiler: buildSpoilerHast\n};\n\n/**\n * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each\n * match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via\n * `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a\n * click-to-reveal spoiler. Emits byte-identical HTML to\n * `@mirrordown/mdit-spoiler`.\n */\nexport const remarkSpoiler: Plugin<[], Root> = () => {\n const constructSpoilerNode = (children: PhrasingContent[]): Spoiler => ({\n type: `spoiler`,\n children\n });\n\n const visitorFirst: Visitor<Text, Parent> = (\n node,\n index,\n parent\n ): VisitorResult => {\n /* v8 ignore next */\n if (!parent || typeof index === `undefined`) return;\n\n if (!REGEX.test(node.value)) return;\n\n const children: PhrasingContent[] = [];\n const value = node.value;\n let tempValue = ``;\n let prevMatchIndex = 0;\n let prevMatchLength = 0;\n\n const matches = Array.from(value.matchAll(REGEX_GLOBAL));\n\n for (const match of matches) {\n const [matched, spoilerText] = match;\n const mIndex = match.index;\n const mLength = matched.length;\n\n const textPartIndex = prevMatchIndex + prevMatchLength;\n\n prevMatchIndex = mIndex;\n prevMatchLength = mLength;\n\n if (mIndex > textPartIndex) {\n children.push(u(`text`, value.substring(textPartIndex, mIndex)));\n }\n\n children.push(\n constructSpoilerNode([\n { type: `text`, value: (spoilerText ?? ``).trim() }\n ])\n );\n\n tempValue = value.slice(mIndex + mLength);\n }\n\n if (tempValue) {\n children.push(u(`text`, tempValue));\n }\n\n if (children.length) parent.children.splice(index, 1, ...children);\n };\n\n const visitorSecond: Visitor<Text, Parent> = (\n node,\n index,\n parent\n ): VisitorResult => {\n /* v8 ignore next */\n if (!parent || typeof index === `undefined`) return;\n\n if (!REGEX_STARTING.test(node.value)) return;\n\n const openingNode = node;\n\n const closingNode = findAfter(parent, openingNode, (n): n is Text => {\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n return n.type === `text` && REGEX_ENDING.test((n as Text).value);\n });\n\n if (!closingNode) return;\n\n // unist-util-find-all-* are generic over Parent; we know paragraph\n // siblings are PhrasingContent. The cast narrows the lib's `Node[]`\n // return to the concrete child type we know we're working with.\n /* oxlint-disable typescript/no-unsafe-type-assertion */\n const beforeChildren = findAllBefore(\n parent,\n openingNode\n ) as PhrasingContent[];\n const mainChildren = findAllBetween(\n parent,\n openingNode,\n closingNode\n ) as PhrasingContent[];\n const afterChildren = findAllAfter(\n parent,\n closingNode\n ) as PhrasingContent[];\n /* oxlint-enable typescript/no-unsafe-type-assertion */\n\n const value = openingNode.value;\n const match = Array.from(value.matchAll(REGEX_STARTING_GLOBAL))[0];\n /* v8 ignore next */\n if (!match) return;\n const [matched] = match;\n const mIndex = match.index;\n const mLength = matched.length;\n\n if (mIndex > 0) {\n beforeChildren.push(u(`text`, value.substring(0, mIndex)));\n }\n\n if (value.length > mIndex + mLength) {\n mainChildren.unshift(u(`text`, value.slice(mIndex + mLength)));\n }\n\n const value_ = closingNode.value;\n const match_ = Array.from(value_.matchAll(REGEX_ENDING_GLOBAL))[0];\n /* v8 ignore next */\n if (!match_) return;\n const [matched_] = match_;\n const mIndex_ = match_.index;\n const mLength_ = matched_.length;\n\n if (mIndex_ > 0) {\n mainChildren.push(u(`text`, value_.substring(0, mIndex_)));\n }\n\n if (value_.length > mIndex_ + mLength_) {\n afterChildren.unshift(u(`text`, value_.slice(mIndex_ + mLength_)));\n }\n\n parent.children = [\n ...beforeChildren,\n constructSpoilerNode(mainChildren),\n ...afterChildren\n ];\n\n return index; // re-visit after restructuring children\n };\n\n const transformer: Transformer<Root> = (tree) => {\n visit(tree, `text`, visitorFirst);\n visit(tree, `text`, visitorSecond);\n };\n\n return transformer;\n};\n"],"mappings":"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"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["import { visit } from \"unist-util-visit\";\nimport type { Plugin, Transformer } from \"unified\";\nimport type { Data, Parent, PhrasingContent, Root } from \"mdast\";\nimport type { Element, ElementContent, Properties } from \"hast\";\nimport { createDelimitedTransform, maskTreeEscapes } from \"@mirrordown/core\";\n\n// A spoiler nested deeper than this stays text, as in the markdown-it and Sätteri plugins: Sätteri rejects a document whose content nests deeper than 128 levels, and a spoiler is two levels there (the label and the content span).\nconst MAX_DEPTH = 50;\n\n/** Optional data carried by a {@link Spoiler} node. */\nexport interface SpoilerData extends Data {}\n\n/**\n * An mdast node for `||spoiler||` text, rendered as a click-to-reveal spoiler\n * (a `<label>` wrapping a checkbox and a content `<span>`).\n */\nexport interface Spoiler extends Parent {\n type: `spoiler`;\n children: PhrasingContent[];\n data?: SpoilerData | undefined;\n}\n\n// Minimal hast-handler surface. remark-rehype's real `State`/`Handler` types\n// carry the whole conversion API; we only need `all()` to convert this node's\n// mdast children into hast. Typed locally to avoid a dependency on\n// mdast-util-to-hast just for two type names.\ninterface HastState {\n all: (node: Spoiler) => ElementContent[];\n patch: (from: Spoiler, to: Element) => void;\n}\ntype SpoilerHandler = (state: HastState, node: Spoiler) => Element;\n\n/**\n * Build the `<label><input><span>...</span></label>` hast subtree. The checkbox\n * precedes the content span so CSS's `:checked ~ .content` combinator can reach\n * it; the content is a following sibling, not a child, of the checkbox.\n */\nconst buildSpoilerHast = (state: HastState, node: Spoiler): Element => {\n const inputProps: Properties = {\n type: `checkbox`,\n class: `markdown-spoiler-toggle`,\n \"aria-label\": `spoiler`\n };\n const input: Element = {\n type: `element`,\n tagName: `input`,\n properties: inputProps,\n children: []\n };\n const content: Element = {\n type: `element`,\n tagName: `span`,\n properties: { class: `markdown-spoiler-content` },\n children: state.all(node)\n };\n const result: Element = {\n type: `element`,\n tagName: `label`,\n properties: { class: `markdown-spoiler` },\n children: [input, content]\n };\n state.patch(node, result);\n return result;\n};\n\n/** mdast-to-hast handler for spoiler nodes, for use with `remark-rehype`. */\nexport const spoilerHastHandlers: Record<string, SpoilerHandler> = {\n spoiler: buildSpoilerHast\n};\n\n/**\n * remark plugin for Discord-style spoiler syntax (`||text||`), wrapping each match in a {@link Spoiler} node. Pair with {@link spoilerHastHandlers} via `remarkRehype({ handlers: spoilerHastHandlers })` so the node renders as a click-to-reveal spoiler. The scan is shared with markdown-it's pairing rules and the Sätteri plugin through `@mirrordown/core`, and the output is byte-identical to `@mirrordown/mdit-spoiler`.\n */\nexport const remarkSpoiler: Plugin<[], Root> = () => {\n const transform = createDelimitedTransform<PhrasingContent>(\n `|`,\n {\n text: (value) => ({ type: `text`, value }),\n // `spoilerHastHandlers` builds the element and never reads this data. It is what renders when the handlers were not passed to `remark-rehype`: the content span alone, which the stylesheet covers and nothing can reveal, where the default for an unknown node is a `<div>` with the content in the clear.\n wrap: (children): Spoiler => ({\n type: `spoiler`,\n children,\n data: {\n hName: `span`,\n hProperties: { className: [`markdown-spoiler-content`] }\n }\n })\n },\n MAX_DEPTH\n );\n\n const transformer: Transformer<Root> = (tree, file) => {\n // Escapes are resolved before this runs; mask escaped characters so a backslash-escaped delimiter is not matched, as in markdown-it.\n const source = String(file);\n const restoreEscapes = maskTreeEscapes(tree, source);\n const seen = new WeakSet<Parent>();\n visit(tree, `text`, (_node, _index, parent: Parent | undefined) => {\n // Spoilers the scan built have already had their content scanned.\n if (!parent || seen.has(parent) || transform.built(parent)) return;\n seen.add(parent);\n // A text node's parent holds phrasing content.\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n const children = parent.children as PhrasingContent[];\n const next = transform(children, parent.type, source);\n if (next !== children) parent.children = [...next];\n // Walk the restructured children from the start; this parent is done.\n return 0;\n });\n restoreEscapes();\n };\n\n return transformer;\n};\n"],"mappings":";;;AAOA,MAAM,YAAY;;;;;;AA8BlB,MAAM,oBAAoB,OAAkB,SAA2B;CAkBrE,MAAM,SAAkB;EACtB,MAAM;EACN,SAAS;EACT,YAAY,EAAE,OAAO,mBAAmB;EACxC,UAAU,CAAC;GAfX,MAAM;GACN,SAAS;GACT,YAAY;IAPZ,MAAM;IACN,OAAO;IACP,cAAc;GAKO;GACrB,UAAU,CAAC;EAYI,GAAG;GATlB,MAAM;GACN,SAAS;GACT,YAAY,EAAE,OAAO,2BAA2B;GAChD,UAAU,MAAM,IAAI,IAAI;EAMA,CAAC;CAC3B;CACA,MAAM,MAAM,MAAM,MAAM;CACxB,OAAO;AACT;;AAGA,MAAa,sBAAsD,EACjE,SAAS,iBACX;;;;AAKA,MAAa,sBAAwC;CACnD,MAAM,YAAY,yBAChB,KACA;EACE,OAAO,WAAW;GAAE,MAAM;GAAQ;EAAM;EAExC,OAAO,cAAuB;GAC5B,MAAM;GACN;GACA,MAAM;IACJ,OAAO;IACP,aAAa,EAAE,WAAW,CAAC,0BAA0B,EAAE;GACzD;EACF;CACF,GACA,SACF;CAEA,MAAM,eAAkC,MAAM,SAAS;EAErD,MAAM,SAAS,OAAO,IAAI;EAC1B,MAAM,iBAAiB,gBAAgB,MAAM,MAAM;EACnD,MAAM,uBAAO,IAAI,QAAgB;EACjC,MAAM,MAAM,SAAS,OAAO,QAAQ,WAA+B;GAEjE,IAAI,CAAC,UAAU,KAAK,IAAI,MAAM,KAAK,UAAU,MAAM,MAAM,GAAG;GAC5D,KAAK,IAAI,MAAM;GAGf,MAAM,WAAW,OAAO;GACxB,MAAM,OAAO,UAAU,UAAU,OAAO,MAAM,MAAM;GACpD,IAAI,SAAS,UAAU,OAAO,WAAW,CAAC,GAAG,IAAI;GAEjD,OAAO;EACT,CAAC;EACD,eAAe;CACjB;CAEA,OAAO;AACT"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mirrordown/remd-spoiler",
3
3
  "description": "A remark plugin for Discord-style spoiler syntax (||text||), rendering click-to-reveal spoilers.",
4
- "version": "0.1.0",
4
+ "version": "0.1.2",
5
5
  "license": "MIT",
6
6
  "author": "Drake Costa <drake@saeris.io> (https://saeris.gg)",
7
7
  "repository": {
@@ -22,11 +22,7 @@
22
22
  "./*.css": "./dist/*.css"
23
23
  },
24
24
  "dependencies": {
25
- "unist-builder": "^4.0.0",
26
- "unist-util-find-after": "^5.0.0",
27
- "unist-util-find-all-after": "^5.0.0",
28
- "unist-util-find-all-before": "^5.0.0",
29
- "unist-util-find-between-all": "^1.1.6",
25
+ "@mirrordown/core": "^0.2.0",
30
26
  "unist-util-visit": "^5.1.0"
31
27
  },
32
28
  "devDependencies": {