@mirrordown/remd-spoiler 0.1.0 → 0.1.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
+
4
+ ## 0.1.1
5
+ <sub>2026-10-28</sub>
6
+
7
+ - *(patch)*
8
+ 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.
9
+ - *(patch)*
10
+ `||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.
11
+
12
+ New `@mirrordown/sat-spoiler` renders the same JavaScript-free click-to-reveal markup, ships the same stylesheet, and is included in `@mirrordown/preset-satteri`.
13
+
3
14
  ## 0.1.0
4
15
  <sub>2026-07-01</sub>
5
16
 
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
 
@@ -23,9 +23,9 @@ The checkbox is the only interactive control, so it stays keyboard-operable and
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
 
@@ -33,34 +33,34 @@ In renderers without the plugin, `||…||` renders as plain text.
33
33
 
34
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.
35
35
 
36
- ```markdown
36
+ ````markdown
37
37
  The recipe needs ||**two** cups of `flour` and a
38
38
  [secret ingredient](https://example.com) that nobody expects||.
39
- ```
39
+ ````
40
40
 
41
41
  ### Image spoilers
42
42
 
43
43
  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
44
 
45
- ```markdown
45
+ ````markdown
46
46
  Careful, dinner photo incoming: ||![A young sapling](./assets/sapling.jpg "Sapling")||
47
- ```
47
+ ````
48
48
 
49
49
  ### Spoilered, zoomable images
50
50
 
51
51
  Combine with the [`lightbox`](/guide/plugins/lightbox) plugin (`||!![…]()||`) for a spoilered image that is also click-to-zoom once revealed.
52
52
 
53
- ```markdown
53
+ ````markdown
54
54
  The reveal: ||!![Mount Fuji at dawn](./assets/fuji.jpg "Mount Fuji")||
55
- ```
55
+ ````
56
56
 
57
57
  ### Inside links
58
58
 
59
59
  A spoiler works inside a link — the whole revealed content remains the link target.
60
60
 
61
- ```markdown
61
+ ````markdown
62
62
  Read [the ||shocking twist||](https://example.com) at your own risk.
63
- ```
63
+ ````
64
64
 
65
65
  ## Accessibility
66
66
 
@@ -70,14 +70,14 @@ The spoiler is a real checkbox with `aria-label="spoiler"`, so assistive technol
70
70
 
71
71
  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
72
 
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. |
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. |
81
81
 
82
82
  ```css
83
83
  :root {
@@ -127,24 +127,8 @@ import "@mirrordown/remd-spoiler/spoiler.css";
127
127
  export default defineConfig({
128
128
  markdown: {
129
129
  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
- }
130
+ remarkRehype: { handlers: spoilerHastHandlers },
131
+ },
148
132
  });
149
133
  ```
150
134
 
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":";;;;iBAOiB,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;;YChE3B;IACR,SAAS;;YAGD;IACR,SAAS"}
package/dist/index.mjs CHANGED
@@ -1,16 +1,6 @@
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
14
4
  /**
15
5
  * Build the `<label><input><span>...</span></label>` hast subtree. The checkbox
16
6
  * precedes the content span so CSS's `:checked ~ .content` combinator can reach
@@ -43,88 +33,34 @@ const buildSpoilerHast = (state, node) => {
43
33
  /** mdast-to-hast handler for spoiler nodes, for use with `remark-rehype`. */
44
34
  const spoilerHastHandlers = { spoiler: buildSpoilerHast };
45
35
  /**
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`.
36
+ * 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
37
  */
52
38
  const remarkSpoiler = () => {
53
- const constructSpoilerNode = (children) => ({
54
- type: `spoiler`,
55
- children
39
+ const transform = createDelimitedTransform(`|`, {
40
+ text: (value) => ({
41
+ type: `text`,
42
+ value
43
+ }),
44
+ wrap: (children) => ({
45
+ type: `spoiler`,
46
+ children
47
+ })
56
48
  });
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);
49
+ const transformer = (tree, file) => {
50
+ const restoreEscapes = maskTreeEscapes(tree, String(file));
51
+ const seen = /* @__PURE__ */ new WeakSet();
52
+ visit(tree, `text`, (_node, _index, parent) => {
53
+ if (!parent || seen.has(parent) || transform.built(parent)) return;
54
+ seen.add(parent);
55
+ const children = parent.children;
56
+ const next = transform(children, parent.type);
57
+ if (next !== children) parent.children = [...next];
58
+ return 0;
91
59
  });
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);
60
+ restoreEscapes();
124
61
  };
125
62
  return transformer;
126
63
  };
127
- //#endregion
128
64
  export { remarkSpoiler, spoilerHastHandlers };
129
65
 
130
66
  //# 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 };\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AQ,OAAO,UAAU,GAAG,QAAQ,CAAC,CAAC;AAG5D,MAAI,OAAO,SAAS,UAAU,SAC5B,eAAc,QAAQ,EAAE,QAAQ,OAAO,MAAM,UAAU,SAAS,CAAC,CAAC;AAGpE,SAAO,WAAW;GAChB,GAAG;GACH,qBAAqB,aAAa;GAClC,GAAG;GACJ;AAED,SAAO;;CAGT,MAAM,eAAkC,SAAS;AAC/C,QAAM,MAAM,QAAQ,aAAa;AACjC,QAAM,MAAM,QAAQ,cAAc;;AAGpC,QAAO"}
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/** 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 text: (value) => ({ type: `text`, value }),\n wrap: (children): Spoiler => ({ type: `spoiler`, children })\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 restoreEscapes = maskTreeEscapes(tree, String(file));\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);\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":";;;;;;;;AAkCA,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,yBAA0C,KAAK;EAC/D,OAAO,WAAW;GAAE,MAAM;GAAQ;EAAM;EACxC,OAAO,cAAuB;GAAE,MAAM;GAAW;EAAS;CAC5D,CAAC;CAED,MAAM,eAAkC,MAAM,SAAS;EAErD,MAAM,iBAAiB,gBAAgB,MAAM,OAAO,IAAI,CAAC;EACzD,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,IAAI;GAC5C,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.1",
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.1.0",
30
26
  "unist-util-visit": "^5.1.0"
31
27
  },
32
28
  "devDependencies": {