rehype-grouping-figure 0.1.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.
- package/LICENSE +21 -0
- package/README.md +202 -0
- package/dist/index.d.ts +325 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ari Palo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# rehype-grouping-figure
|
|
2
|
+
|
|
3
|
+
**rehype** plugin that groups one or more images into a `<figure>` when they are immediately followed by a `<blockquote>`, turning that blockquote into a `<figcaption>`.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- What is this?
|
|
8
|
+
- When should I use this?
|
|
9
|
+
- Install
|
|
10
|
+
- Use
|
|
11
|
+
- API
|
|
12
|
+
- `unified().use(rehypeGroupingFigure[, options])`
|
|
13
|
+
- Examples
|
|
14
|
+
- Types
|
|
15
|
+
- Compatibility
|
|
16
|
+
- Security
|
|
17
|
+
- License
|
|
18
|
+
|
|
19
|
+
## What is this?
|
|
20
|
+
|
|
21
|
+
This is a unified (rehype) plugin.
|
|
22
|
+
|
|
23
|
+
It walks the hast tree and looks for this pattern:
|
|
24
|
+
|
|
25
|
+
1. A paragraph (`<p>`) that contains only image elements (`<img>`) and optional whitespace text nodes
|
|
26
|
+
2. Immediately followed by a blockquote (`<blockquote>`) (whitespace text nodes between them are ignored)
|
|
27
|
+
|
|
28
|
+
When found, it rewrites both nodes into:
|
|
29
|
+
|
|
30
|
+
- `<figure class="group">`
|
|
31
|
+
- all matched `<img>` elements inside the figure
|
|
32
|
+
- `<figcaption>` containing the original blockquote children
|
|
33
|
+
|
|
34
|
+
## When should I use this?
|
|
35
|
+
|
|
36
|
+
Use this plugin when your markdown/HTML authoring style places captions in blockquotes after image-only paragraphs, and you want semantic figure markup in output HTML.
|
|
37
|
+
|
|
38
|
+
You probably **shouldn't** use it if:
|
|
39
|
+
|
|
40
|
+
- your captions are not represented as blockquotes
|
|
41
|
+
- you need configurable class names or matching rules (the current API is intentionally minimal)
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
This package is ESM only. In Node.js (version 20.19+):
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
npm install rehype-grouping-figure
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
pnpm add rehype-grouping-figure
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Use
|
|
56
|
+
|
|
57
|
+
Say we have the following markdown:
|
|
58
|
+
|
|
59
|
+
```md
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
> This is a caption for the cat image
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
...and a script `example.js`:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import {unified} from 'unified'
|
|
69
|
+
import remarkParse from 'remark-parse'
|
|
70
|
+
import remarkRehype from 'remark-rehype'
|
|
71
|
+
import rehypeStringify from 'rehype-stringify'
|
|
72
|
+
import rehypeGroupingFigure from 'rehype-grouping-figure'
|
|
73
|
+
|
|
74
|
+
const file = await unified()
|
|
75
|
+
.use(remarkParse)
|
|
76
|
+
.use(remarkRehype)
|
|
77
|
+
.use(rehypeGroupingFigure)
|
|
78
|
+
.use(rehypeStringify)
|
|
79
|
+
.process('\n\n> This is a caption for the cat image')
|
|
80
|
+
|
|
81
|
+
console.log(String(file))
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
...running `node example.js` yields:
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<figure class="group"><img src="./cat.jpg" alt="A beautiful cat"><figcaption><p>This is a caption for the cat image</p></figcaption></figure>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## API
|
|
91
|
+
|
|
92
|
+
This package exports:
|
|
93
|
+
|
|
94
|
+
- the default identifier `rehypeGroupingFigure`
|
|
95
|
+
- the named schema export `rehypeGroupingFigureSanitizeSchema`
|
|
96
|
+
- the TypeScript type `Options`
|
|
97
|
+
|
|
98
|
+
### `unified().use(rehypeGroupingFigure[, options])`
|
|
99
|
+
|
|
100
|
+
Groups image-only paragraphs followed by blockquotes into figure/figcaption markup.
|
|
101
|
+
|
|
102
|
+
###### Parameters
|
|
103
|
+
|
|
104
|
+
- `options` (`Options`, optional) — currently empty; reserved for future configuration
|
|
105
|
+
|
|
106
|
+
###### Returns
|
|
107
|
+
|
|
108
|
+
Transformer function.
|
|
109
|
+
|
|
110
|
+
### `rehypeGroupingFigureSanitizeSchema`
|
|
111
|
+
|
|
112
|
+
Schema extension object for `rehype-sanitize` so generated `figure` / `figcaption` markup and `figure.group` are allowed.
|
|
113
|
+
|
|
114
|
+
## Examples
|
|
115
|
+
|
|
116
|
+
### Two images + caption
|
|
117
|
+
|
|
118
|
+
Input:
|
|
119
|
+
|
|
120
|
+
```md
|
|
121
|
+

|
|
122
|
+

|
|
123
|
+
|
|
124
|
+
> These are our beloved pets
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Output:
|
|
128
|
+
|
|
129
|
+
```html
|
|
130
|
+
<figure class="group">
|
|
131
|
+
<img src="./cat.jpg" alt="A beautiful cat">
|
|
132
|
+
<img src="./dog.jpg" alt="A playful dog">
|
|
133
|
+
<figcaption>
|
|
134
|
+
<p>These are our beloved pets</p>
|
|
135
|
+
</figcaption>
|
|
136
|
+
</figure>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Multi-paragraph captions are preserved
|
|
140
|
+
|
|
141
|
+
Input:
|
|
142
|
+
|
|
143
|
+
```md
|
|
144
|
+

|
|
145
|
+
|
|
146
|
+
> This is a multi-line caption.
|
|
147
|
+
>
|
|
148
|
+
> It spans multiple paragraphs and provides
|
|
149
|
+
> detailed information about the image.
|
|
150
|
+
>
|
|
151
|
+
> **Bold text** and *italic text* are also supported.
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Output `figcaption` keeps all blockquote content structure.
|
|
155
|
+
|
|
156
|
+
### Not grouped when content appears in between
|
|
157
|
+
|
|
158
|
+
If any non-whitespace content appears between the image paragraph and blockquote, no grouping happens.
|
|
159
|
+
|
|
160
|
+
## Types
|
|
161
|
+
|
|
162
|
+
This package is fully typed with TypeScript.
|
|
163
|
+
|
|
164
|
+
## Compatibility
|
|
165
|
+
|
|
166
|
+
- Node.js 20.19+
|
|
167
|
+
- `unified` 11+
|
|
168
|
+
|
|
169
|
+
## Security
|
|
170
|
+
|
|
171
|
+
This plugin only reshapes existing HTML structure (`p` + `img` + `blockquote` -> `figure` + `figcaption`) and does not fetch external data or execute code.
|
|
172
|
+
|
|
173
|
+
If you sanitize downstream with `rehype-sanitize`, merge in the exported schema:
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
import rehypeSanitize, {defaultSchema} from 'rehype-sanitize'
|
|
177
|
+
import rehypeGroupingFigure, {
|
|
178
|
+
rehypeGroupingFigureSanitizeSchema
|
|
179
|
+
} from 'rehype-grouping-figure'
|
|
180
|
+
|
|
181
|
+
unified()
|
|
182
|
+
.use(rehypeGroupingFigure)
|
|
183
|
+
.use(rehypeSanitize, {
|
|
184
|
+
...defaultSchema,
|
|
185
|
+
tagNames: [
|
|
186
|
+
...(defaultSchema.tagNames || []),
|
|
187
|
+
...rehypeGroupingFigureSanitizeSchema.tagNames
|
|
188
|
+
],
|
|
189
|
+
attributes: {
|
|
190
|
+
...defaultSchema.attributes,
|
|
191
|
+
...rehypeGroupingFigureSanitizeSchema.attributes
|
|
192
|
+
},
|
|
193
|
+
ancestors: {
|
|
194
|
+
...defaultSchema.ancestors,
|
|
195
|
+
...rehypeGroupingFigureSanitizeSchema.ancestors
|
|
196
|
+
}
|
|
197
|
+
})
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT © Ari Palo
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
//#region node_modules/.pnpm/@types+unist@3.0.3/node_modules/@types/unist/index.d.ts
|
|
2
|
+
// ## Interfaces
|
|
3
|
+
/**
|
|
4
|
+
* Info associated with nodes by the ecosystem.
|
|
5
|
+
*
|
|
6
|
+
* This space is guaranteed to never be specified by unist or specifications
|
|
7
|
+
* implementing unist.
|
|
8
|
+
* But you can use it in utilities and plugins to store data.
|
|
9
|
+
*
|
|
10
|
+
* This type can be augmented to register custom data.
|
|
11
|
+
* For example:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* declare module 'unist' {
|
|
15
|
+
* interface Data {
|
|
16
|
+
* // `someNode.data.myId` is typed as `number | undefined`
|
|
17
|
+
* myId?: number | undefined
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface Data$1 {}
|
|
23
|
+
/**
|
|
24
|
+
* One place in a source file.
|
|
25
|
+
*/
|
|
26
|
+
interface Point {
|
|
27
|
+
/**
|
|
28
|
+
* Line in a source file (1-indexed integer).
|
|
29
|
+
*/
|
|
30
|
+
line: number;
|
|
31
|
+
/**
|
|
32
|
+
* Column in a source file (1-indexed integer).
|
|
33
|
+
*/
|
|
34
|
+
column: number;
|
|
35
|
+
/**
|
|
36
|
+
* Character in a source file (0-indexed integer).
|
|
37
|
+
*/
|
|
38
|
+
offset?: number | undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Position of a node in a source document.
|
|
42
|
+
*
|
|
43
|
+
* A position is a range between two points.
|
|
44
|
+
*/
|
|
45
|
+
interface Position {
|
|
46
|
+
/**
|
|
47
|
+
* Place of the first character of the parsed source region.
|
|
48
|
+
*/
|
|
49
|
+
start: Point;
|
|
50
|
+
/**
|
|
51
|
+
* Place of the first character after the parsed source region.
|
|
52
|
+
*/
|
|
53
|
+
end: Point;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Abstract unist node.
|
|
57
|
+
*
|
|
58
|
+
* The syntactic unit in unist syntax trees are called nodes.
|
|
59
|
+
*
|
|
60
|
+
* This interface is supposed to be extended.
|
|
61
|
+
* If you can use {@link Literal} or {@link Parent}, you should.
|
|
62
|
+
* But for example in markdown, a `thematicBreak` (`***`), is neither literal
|
|
63
|
+
* nor parent, but still a node.
|
|
64
|
+
*/
|
|
65
|
+
interface Node$1 {
|
|
66
|
+
/**
|
|
67
|
+
* Node type.
|
|
68
|
+
*/
|
|
69
|
+
type: string;
|
|
70
|
+
/**
|
|
71
|
+
* Info from the ecosystem.
|
|
72
|
+
*/
|
|
73
|
+
data?: Data$1 | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Position of a node in a source document.
|
|
76
|
+
*
|
|
77
|
+
* Nodes that are generated (not in the original source document) must not
|
|
78
|
+
* have a position.
|
|
79
|
+
*/
|
|
80
|
+
position?: Position | undefined;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region node_modules/.pnpm/@types+hast@3.0.4/node_modules/@types/hast/index.d.ts
|
|
84
|
+
// ## Interfaces
|
|
85
|
+
/**
|
|
86
|
+
* Info associated with hast nodes by the ecosystem.
|
|
87
|
+
*
|
|
88
|
+
* This space is guaranteed to never be specified by unist or hast.
|
|
89
|
+
* But you can use it in utilities and plugins to store data.
|
|
90
|
+
*
|
|
91
|
+
* This type can be augmented to register custom data.
|
|
92
|
+
* For example:
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* declare module 'hast' {
|
|
96
|
+
* interface Data {
|
|
97
|
+
* // `someNode.data.myId` is typed as `number | undefined`
|
|
98
|
+
* myId?: number | undefined
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
interface Data extends Data$1 {}
|
|
104
|
+
/**
|
|
105
|
+
* Info associated with an element.
|
|
106
|
+
*/
|
|
107
|
+
interface Properties {
|
|
108
|
+
[PropertyName: string]: boolean | number | string | null | undefined | Array<string | number>;
|
|
109
|
+
}
|
|
110
|
+
// ## Content maps
|
|
111
|
+
/**
|
|
112
|
+
* Union of registered hast nodes that can occur in {@link Element}.
|
|
113
|
+
*
|
|
114
|
+
* To register mote custom hast nodes, add them to {@link ElementContentMap}.
|
|
115
|
+
* They will be automatically added here.
|
|
116
|
+
*/
|
|
117
|
+
type ElementContent = ElementContentMap[keyof ElementContentMap];
|
|
118
|
+
/**
|
|
119
|
+
* Registry of all hast nodes that can occur as children of {@link Element}.
|
|
120
|
+
*
|
|
121
|
+
* For a union of all {@link Element} children, see {@link ElementContent}.
|
|
122
|
+
*/
|
|
123
|
+
interface ElementContentMap {
|
|
124
|
+
comment: Comment;
|
|
125
|
+
element: Element;
|
|
126
|
+
text: Text;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Union of registered hast nodes that can occur in {@link Root}.
|
|
130
|
+
*
|
|
131
|
+
* To register custom hast nodes, add them to {@link RootContentMap}.
|
|
132
|
+
* They will be automatically added here.
|
|
133
|
+
*/
|
|
134
|
+
type RootContent = RootContentMap[keyof RootContentMap];
|
|
135
|
+
/**
|
|
136
|
+
* Registry of all hast nodes that can occur as children of {@link Root}.
|
|
137
|
+
*
|
|
138
|
+
* > 👉 **Note**: {@link Root} does not need to be an entire document.
|
|
139
|
+
* > it can also be a fragment.
|
|
140
|
+
*
|
|
141
|
+
* For a union of all {@link Root} children, see {@link RootContent}.
|
|
142
|
+
*/
|
|
143
|
+
interface RootContentMap {
|
|
144
|
+
comment: Comment;
|
|
145
|
+
doctype: Doctype;
|
|
146
|
+
element: Element;
|
|
147
|
+
text: Text;
|
|
148
|
+
}
|
|
149
|
+
// ## Abstract nodes
|
|
150
|
+
/**
|
|
151
|
+
* Abstract hast node.
|
|
152
|
+
*
|
|
153
|
+
* This interface is supposed to be extended.
|
|
154
|
+
* If you can use {@link Literal} or {@link Parent}, you should.
|
|
155
|
+
* But for example in HTML, a `Doctype` is neither literal nor parent, but
|
|
156
|
+
* still a node.
|
|
157
|
+
*
|
|
158
|
+
* To register custom hast nodes, add them to {@link RootContentMap} and other
|
|
159
|
+
* places where relevant (such as {@link ElementContentMap}).
|
|
160
|
+
*
|
|
161
|
+
* For a union of all registered hast nodes, see {@link Nodes}.
|
|
162
|
+
*/
|
|
163
|
+
interface Node extends Node$1 {
|
|
164
|
+
/**
|
|
165
|
+
* Info from the ecosystem.
|
|
166
|
+
*/
|
|
167
|
+
data?: Data | undefined;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Abstract hast node that contains the smallest possible value.
|
|
171
|
+
*
|
|
172
|
+
* This interface is supposed to be extended if you make custom hast nodes.
|
|
173
|
+
*
|
|
174
|
+
* For a union of all registered hast literals, see {@link Literals}.
|
|
175
|
+
*/
|
|
176
|
+
interface Literal extends Node {
|
|
177
|
+
/**
|
|
178
|
+
* Plain-text value.
|
|
179
|
+
*/
|
|
180
|
+
value: string;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Abstract hast node that contains other hast nodes (*children*).
|
|
184
|
+
*
|
|
185
|
+
* This interface is supposed to be extended if you make custom hast nodes.
|
|
186
|
+
*
|
|
187
|
+
* For a union of all registered hast parents, see {@link Parents}.
|
|
188
|
+
*/
|
|
189
|
+
interface Parent extends Node {
|
|
190
|
+
/**
|
|
191
|
+
* List of children.
|
|
192
|
+
*/
|
|
193
|
+
children: RootContent[];
|
|
194
|
+
}
|
|
195
|
+
// ## Concrete nodes
|
|
196
|
+
/**
|
|
197
|
+
* HTML comment.
|
|
198
|
+
*/
|
|
199
|
+
interface Comment extends Literal {
|
|
200
|
+
/**
|
|
201
|
+
* Node type of HTML comments in hast.
|
|
202
|
+
*/
|
|
203
|
+
type: "comment";
|
|
204
|
+
/**
|
|
205
|
+
* Data associated with the comment.
|
|
206
|
+
*/
|
|
207
|
+
data?: CommentData | undefined;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Info associated with hast comments by the ecosystem.
|
|
211
|
+
*/
|
|
212
|
+
interface CommentData extends Data {}
|
|
213
|
+
/**
|
|
214
|
+
* HTML document type.
|
|
215
|
+
*/
|
|
216
|
+
interface Doctype extends Node$1 {
|
|
217
|
+
/**
|
|
218
|
+
* Node type of HTML document types in hast.
|
|
219
|
+
*/
|
|
220
|
+
type: "doctype";
|
|
221
|
+
/**
|
|
222
|
+
* Data associated with the doctype.
|
|
223
|
+
*/
|
|
224
|
+
data?: DoctypeData | undefined;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Info associated with hast doctypes by the ecosystem.
|
|
228
|
+
*/
|
|
229
|
+
interface DoctypeData extends Data {}
|
|
230
|
+
/**
|
|
231
|
+
* HTML element.
|
|
232
|
+
*/
|
|
233
|
+
interface Element extends Parent {
|
|
234
|
+
/**
|
|
235
|
+
* Node type of elements.
|
|
236
|
+
*/
|
|
237
|
+
type: "element";
|
|
238
|
+
/**
|
|
239
|
+
* Tag name (such as `'body'`) of the element.
|
|
240
|
+
*/
|
|
241
|
+
tagName: string;
|
|
242
|
+
/**
|
|
243
|
+
* Info associated with the element.
|
|
244
|
+
*/
|
|
245
|
+
properties: Properties;
|
|
246
|
+
/**
|
|
247
|
+
* Children of element.
|
|
248
|
+
*/
|
|
249
|
+
children: ElementContent[];
|
|
250
|
+
/**
|
|
251
|
+
* When the `tagName` field is `'template'`, a `content` field can be
|
|
252
|
+
* present.
|
|
253
|
+
*/
|
|
254
|
+
content?: Root | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* Data associated with the element.
|
|
257
|
+
*/
|
|
258
|
+
data?: ElementData | undefined;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Info associated with hast elements by the ecosystem.
|
|
262
|
+
*/
|
|
263
|
+
interface ElementData extends Data {}
|
|
264
|
+
/**
|
|
265
|
+
* Document fragment or a whole document.
|
|
266
|
+
*
|
|
267
|
+
* Should be used as the root of a tree and must not be used as a child.
|
|
268
|
+
*
|
|
269
|
+
* Can also be used as the value for the content field on a `'template'` element.
|
|
270
|
+
*/
|
|
271
|
+
interface Root extends Parent {
|
|
272
|
+
/**
|
|
273
|
+
* Node type of hast root.
|
|
274
|
+
*/
|
|
275
|
+
type: "root";
|
|
276
|
+
/**
|
|
277
|
+
* Children of root.
|
|
278
|
+
*/
|
|
279
|
+
children: RootContent[];
|
|
280
|
+
/**
|
|
281
|
+
* Data associated with the hast root.
|
|
282
|
+
*/
|
|
283
|
+
data?: RootData | undefined;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Info associated with hast root nodes by the ecosystem.
|
|
287
|
+
*/
|
|
288
|
+
interface RootData extends Data {}
|
|
289
|
+
/**
|
|
290
|
+
* HTML character data (plain text).
|
|
291
|
+
*/
|
|
292
|
+
interface Text extends Literal {
|
|
293
|
+
/**
|
|
294
|
+
* Node type of HTML character data (plain text) in hast.
|
|
295
|
+
*/
|
|
296
|
+
type: "text";
|
|
297
|
+
/**
|
|
298
|
+
* Data associated with the text.
|
|
299
|
+
*/
|
|
300
|
+
data?: TextData | undefined;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Info associated with hast texts by the ecosystem.
|
|
304
|
+
*/
|
|
305
|
+
interface TextData extends Data {}
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/index.d.ts
|
|
308
|
+
interface Options {}
|
|
309
|
+
/**
|
|
310
|
+
* Schema extension for `rehype-sanitize` so output from this plugin is preserved.
|
|
311
|
+
* Compatible with https://github.com/syntax-tree/hast-util-sanitize
|
|
312
|
+
*/
|
|
313
|
+
declare const rehypeGroupingFigureSanitizeSchema: {
|
|
314
|
+
readonly ancestors: {
|
|
315
|
+
readonly figcaption: ["figure"];
|
|
316
|
+
};
|
|
317
|
+
readonly attributes: {
|
|
318
|
+
readonly figure: [["className", "group"]];
|
|
319
|
+
};
|
|
320
|
+
readonly tagNames: ["figure", "figcaption"];
|
|
321
|
+
};
|
|
322
|
+
declare function rehypeGroupingFigure(_options?: Options): (tree: Root) => void;
|
|
323
|
+
//#endregion
|
|
324
|
+
export { Options, rehypeGroupingFigure as default, rehypeGroupingFigureSanitizeSchema };
|
|
325
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["Data","Point","line","column","offset","Position","start","end","Literal","Node","value","type","data","position","Parent","children","Data","UnistData","Literal","UnistLiteral","Node","UnistNode","Parent","UnistParent","Properties","Array","PropertyName","ElementContent","ElementContentMap","Comment","Element","Text","comment","element","text","RootContent","RootContentMap","Doctype","doctype","Content","Literals","Nodes","Extract","Root","Parents","data","value","children","CommentData","type","DoctypeData","ElementData","tagName","properties","content","RootData","TextData"],"sources":["../node_modules/.pnpm/@types+unist@3.0.3/node_modules/@types/unist/index.d.ts","../node_modules/.pnpm/@types+hast@3.0.4/node_modules/@types/hast/index.d.ts","../src/index.ts"],"x_google_ignoreList":[0,1],"mappings":";;;;;;AA0BA;;;;;;;;;AAqBA;;;;;;UA1BiBA,MAAAA;;;AAgEjB;UA3DiBC,KAAAA;;;;EAIbC,IAAAA;EAwEAW;;;EAnEAV,MAAAA;;;;EAIAC,MAAAA;AAAAA;;;ACZJ;;;UDoBiBC,QAAAA;ECnB+D;AAWhF;;EDYIC,KAAAA,EAAOL,KAAAA;ECZ2D;AAOtE;;EDUIM,GAAAA,EAAKN,KAAAA;AAAAA;ACYT;;;;;;;;;;AAAA,UDiBiBQ,MAAAA;ECfb6B;;;EDmBA3B,IAAAA;ECjBMoB;;;EDsBNnB,IAAAA,GAAOZ,MAAAA;;;;;;;EAQPa,QAAAA,GAAWR,QAAAA;AAAAA;;;;;;AA5Ef;;;;;;;;;AAqBA;;;;;;;UCzBiBW,IAAAA,SAAaC,MAAAA;;AD+D9B;;UC1DiBO,UAAAA;EAAAA,CACZE,YAAAA,0DAAsED,KAAAA;AAAAA;AAAAA;;;;;;;KAW/DE,cAAAA,GAAiBC,iBAAAA,OAAwBA,iBAAAA;;;;AAZrD;;UAmBiBA,iBAAAA;EACbI,OAAAA,EAASH,OAAAA;EACTI,OAAAA,EAASH,OAAAA;EACTI,IAAAA,EAAMH,IAAAA;AAAAA;;;AAHV;;;;KAYYI,WAAAA,GAAcC,cAAAA,OAAqBA,cAAAA;;;;;;;;;UAU9BA,cAAAA;EACbJ,OAAAA,EAASH,OAAAA;EACTS,OAAAA,EAASD,OAAAA;EACTJ,OAAAA,EAASH,OAAAA;EACTI,IAAAA,EAAMH,IAAAA;AAAAA;AAAAA;;;;;AA8FV;;;;;;;;;UAxCiBX,IAAAA,SAAaC,MAAAA;EAsDb2B;;;EAlDbH,IAAAA,GAAO7B,IAAAA;AAAAA;;;;;;;;UAUME,OAAAA,SAAgBE,IAAAA;EAqDX;AAMtB;;EAvDI0B,KAAAA;AAAAA;AA4DJ;;;;;;;AAAA,UAlDiBxB,MAAAA,SAAeF,IAAAA;EAkDO;;;EA9CnC2B,QAAAA,EAAUZ,WAAAA;AAAAA;AAAAA;;;;UAQGN,OAAAA,SAAgBX,OAAAA;EA+DtBiC;;;EA3DPF,IAAAA;EAiEwB;;;EA7DxBJ,IAAAA,GAAOG,WAAAA;AAAAA;;;;UAMMA,WAAAA,SAAoBhC,IAAAA;;;;UAKpBqB,OAAAA,SAAgBhB,MAAAA;EAmEnBc;;;EA/DVc,IAAAA;EAmEe;AAMnB;;EArEIJ,IAAAA,GAAOK,WAAAA;AAAAA;AA0EX;;;AAAA,UApEiBA,WAAAA,SAAoBlC,IAAAA;;;;UAKpBc,OAAAA,SAAgBR,MAAAA;EAuEd;AAMnB;;EAzEI2B,IAAAA;EAyE8BjC;;;EArE9BoC,OAAAA;;ACjNJ;;EDqNIC,UAAAA,EAAY7B,UAAAA;ECrNQ;;AAMxB;EDmNIuB,QAAAA,EAAUpB,cAAAA;;;;;EAKV2B,OAAAA,GAAUX,IAAAA;;;;EAIVE,IAAAA,GAAOM,WAAAA;AAAAA;;;;UAMMA,WAAAA,SAAoBnC,IAAAA;;;;;;;;UASpB2B,IAAAA,SAAarB,MAAAA;;;;EAI1B2B,IAAAA;;;;EAIAF,QAAAA,EAAUZ,WAAAA;;;;EAIVU,IAAAA,GAAOU,QAAAA;AAAAA;;;;UAMMA,QAAAA,SAAiBvC,IAAAA;;;;UAKjBe,IAAAA,SAAab,OAAAA;;;;EAI1B+B,IAAAA;;;;EAIAJ,IAAAA,GAAOW,QAAAA;AAAAA;;;;UAMMA,QAAAA,SAAiBxC,IAAAA;;;UCtRjB,OAAA;;;;AFuBjB;cEjBa,kCAAA;EAAA;;;;;;;;iBAUW,oBAAA,CAAqB,QAAA,GAAU,OAAA,IAC7C,IAAA,EAAM,IAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//#region src/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Schema extension for `rehype-sanitize` so output from this plugin is preserved.
|
|
4
|
+
* Compatible with https://github.com/syntax-tree/hast-util-sanitize
|
|
5
|
+
*/
|
|
6
|
+
const rehypeGroupingFigureSanitizeSchema = {
|
|
7
|
+
ancestors: { figcaption: ["figure"] },
|
|
8
|
+
attributes: { figure: [["className", "group"]] },
|
|
9
|
+
tagNames: ["figure", "figcaption"]
|
|
10
|
+
};
|
|
11
|
+
function rehypeGroupingFigure(_options = {}) {
|
|
12
|
+
return (tree) => {
|
|
13
|
+
processNode(tree);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function processNode(node) {
|
|
17
|
+
if (!node.children || !Array.isArray(node.children)) return;
|
|
18
|
+
const children = node.children;
|
|
19
|
+
const newChildren = [];
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (i < children.length) {
|
|
22
|
+
const current = children[i];
|
|
23
|
+
if (current.type === "element" && current.tagName === "p") {
|
|
24
|
+
const images = current.children.filter((child) => child.type === "element" && child.tagName === "img");
|
|
25
|
+
const nonImageChildren = current.children.filter((child) => !(child.type === "element" && child.tagName === "img") && !(child.type === "text" && child.value.trim() === ""));
|
|
26
|
+
if (images.length > 0 && nonImageChildren.length === 0) {
|
|
27
|
+
let nextElementIndex = i + 1;
|
|
28
|
+
while (nextElementIndex < children.length && children[nextElementIndex].type === "text" && "value" in children[nextElementIndex] && children[nextElementIndex].value.trim() === "") nextElementIndex++;
|
|
29
|
+
if (nextElementIndex < children.length) {
|
|
30
|
+
const nextElement = children[nextElementIndex];
|
|
31
|
+
if (nextElement.type === "element" && nextElement.tagName === "blockquote") {
|
|
32
|
+
const figcaption = {
|
|
33
|
+
type: "element",
|
|
34
|
+
tagName: "figcaption",
|
|
35
|
+
properties: {},
|
|
36
|
+
children: nextElement.children
|
|
37
|
+
};
|
|
38
|
+
const figure = {
|
|
39
|
+
type: "element",
|
|
40
|
+
tagName: "figure",
|
|
41
|
+
properties: { className: "group" },
|
|
42
|
+
children: [...images, figcaption]
|
|
43
|
+
};
|
|
44
|
+
newChildren.push(figure);
|
|
45
|
+
i = nextElementIndex + 1;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (current.type === "element") processNode(current);
|
|
52
|
+
newChildren.push(current);
|
|
53
|
+
i++;
|
|
54
|
+
}
|
|
55
|
+
node.children = newChildren;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
export { rehypeGroupingFigure as default, rehypeGroupingFigureSanitizeSchema };
|
|
59
|
+
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Element, Root, RootContent, ElementContent } from 'hast';\nimport type { Schema } from \"hast-util-sanitize\";\n\nexport interface Options {}\n\n/**\n * Schema extension for `rehype-sanitize` so output from this plugin is preserved.\n * Compatible with https://github.com/syntax-tree/hast-util-sanitize\n */\nexport const rehypeGroupingFigureSanitizeSchema = {\n ancestors: {\n figcaption: ['figure'],\n },\n attributes: {\n figure: [['className', 'group']],\n },\n tagNames: ['figure', 'figcaption'],\n} as const satisfies Schema;\n\nexport default function rehypeGroupingFigure(_options: Options = {}) {\n return (tree: Root) => {\n processNode(tree);\n };\n}\n\nfunction processNode(node: Element | Root) {\n if (!node.children || !Array.isArray(node.children)) {\n return;\n }\n\n const children = node.children;\n const newChildren: (RootContent | ElementContent)[] = [];\n let i = 0;\n\n while (i < children.length) {\n const current = children[i];\n\n // Check if current element is a paragraph containing images\n if (current.type === 'element' && current.tagName === 'p') {\n // Extract all image elements from the paragraph\n const images = current.children.filter(child =>\n child.type === 'element' && child.tagName === 'img'\n ) as Element[];\n\n // Check if this paragraph contains only images and whitespace/text\n const nonImageChildren = current.children.filter(child =>\n !(child.type === 'element' && child.tagName === 'img') &&\n !(child.type === 'text' && child.value.trim() === '')\n );\n\n // If we have images and no other significant content\n if (images.length > 0 && nonImageChildren.length === 0) {\n // Check if the next element is a blockquote (skip text nodes)\n let nextElementIndex = i + 1;\n\n // Skip over text nodes that are just whitespace\n while (nextElementIndex < children.length &&\n children[nextElementIndex].type === 'text' &&\n 'value' in children[nextElementIndex] &&\n (children[nextElementIndex] as any).value.trim() === '') {\n nextElementIndex++;\n }\n\n if (nextElementIndex < children.length) {\n const nextElement = children[nextElementIndex];\n\n if (nextElement.type === 'element' && nextElement.tagName === 'blockquote') {\n // Create figcaption from blockquote content\n const figcaption: Element = {\n type: 'element',\n tagName: 'figcaption',\n properties: {},\n children: nextElement.children\n };\n\n // Create figure element\n const figure: Element = {\n type: 'element',\n tagName: 'figure',\n properties: {\n className: 'group'\n },\n children: [\n ...images,\n figcaption\n ]\n };\n\n newChildren.push(figure);\n\n // Skip all the elements we've processed (including whitespace text nodes)\n i = nextElementIndex + 1;\n continue;\n }\n }\n }\n }\n\n // If no pattern was found, recursively process child elements\n if (current.type === 'element') {\n processNode(current);\n }\n\n // Add the current element\n newChildren.push(current);\n i++;\n }\n\n node.children = newChildren as any;\n}\n"],"mappings":";;;;;AASA,MAAa,qCAAqC;CAChD,WAAW,EACT,YAAY,CAAC,SAAS,EACvB;CACD,YAAY,EACV,QAAQ,CAAC,CAAC,aAAa,QAAQ,CAAC,EACjC;CACD,UAAU,CAAC,UAAU,aAAa;CACnC;AAED,SAAwB,qBAAqB,WAAoB,EAAE,EAAE;AACnE,SAAQ,SAAe;AACrB,cAAY,KAAK;;;AAIrB,SAAS,YAAY,MAAsB;AACzC,KAAI,CAAC,KAAK,YAAY,CAAC,MAAM,QAAQ,KAAK,SAAS,CACjD;CAGF,MAAM,WAAW,KAAK;CACtB,MAAM,cAAgD,EAAE;CACxD,IAAI,IAAI;AAER,QAAO,IAAI,SAAS,QAAQ;EAC1B,MAAM,UAAU,SAAS;AAGzB,MAAI,QAAQ,SAAS,aAAa,QAAQ,YAAY,KAAK;GAEzD,MAAM,SAAS,QAAQ,SAAS,QAAO,UACrC,MAAM,SAAS,aAAa,MAAM,YAAY,MAC/C;GAGD,MAAM,mBAAmB,QAAQ,SAAS,QAAO,UAC/C,EAAE,MAAM,SAAS,aAAa,MAAM,YAAY,UAChD,EAAE,MAAM,SAAS,UAAU,MAAM,MAAM,MAAM,KAAK,IACnD;AAGD,OAAI,OAAO,SAAS,KAAK,iBAAiB,WAAW,GAAG;IAEtD,IAAI,mBAAmB,IAAI;AAG3B,WAAO,mBAAmB,SAAS,UAC5B,SAAS,kBAAkB,SAAS,UACpC,WAAW,SAAS,qBACnB,SAAS,kBAA0B,MAAM,MAAM,KAAK,GAC1D;AAGF,QAAI,mBAAmB,SAAS,QAAQ;KACtC,MAAM,cAAc,SAAS;AAE7B,SAAI,YAAY,SAAS,aAAa,YAAY,YAAY,cAAc;MAE1E,MAAM,aAAsB;OAC1B,MAAM;OACN,SAAS;OACT,YAAY,EAAE;OACd,UAAU,YAAY;OACvB;MAGD,MAAM,SAAkB;OACtB,MAAM;OACN,SAAS;OACT,YAAY,EACV,WAAW,SACZ;OACD,UAAU,CACR,GAAG,QACH,WACD;OACF;AAED,kBAAY,KAAK,OAAO;AAGxB,UAAI,mBAAmB;AACvB;;;;;AAOR,MAAI,QAAQ,SAAS,UACnB,aAAY,QAAQ;AAItB,cAAY,KAAK,QAAQ;AACzB;;AAGF,MAAK,WAAW"}
|
package/package.json
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rehype-grouping-figure",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "rehype plugin that groups images followed by blockquote into a figure and figcaption",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Ari Palo",
|
|
8
|
+
"email": "opensource@aripalo.com",
|
|
9
|
+
"url": "https://aripalo.technology"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=20.19.0"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"hast-util-sanitize": "^5.0.2",
|
|
32
|
+
"isomorphic-dompurify": "^2.36.0",
|
|
33
|
+
"unist-util-visit": "^5.1.0",
|
|
34
|
+
"vfile": "^6.0.3"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"unified": ">=11"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/hast": "^3.0.4",
|
|
41
|
+
"@types/node": "^22.0.0",
|
|
42
|
+
"@vitest/coverage-v8": "^4.1.4",
|
|
43
|
+
"rehype-format": "^5.0.1",
|
|
44
|
+
"rehype-stringify": "^10.0.1",
|
|
45
|
+
"remark-parse": "^11.0.0",
|
|
46
|
+
"remark-rehype": "^11.1.2",
|
|
47
|
+
"tsdown": "^0.21.9",
|
|
48
|
+
"typescript": "^5.6.0",
|
|
49
|
+
"unified": "^11.0.5",
|
|
50
|
+
"vitest": "^4.0.18"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"rehype",
|
|
54
|
+
"rehype-plugin",
|
|
55
|
+
"unified",
|
|
56
|
+
"html",
|
|
57
|
+
"hast",
|
|
58
|
+
"figure",
|
|
59
|
+
"figcaption",
|
|
60
|
+
"grouping"
|
|
61
|
+
],
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "git+https://github.com/aripalo/rehype-grouping-figure.git"
|
|
65
|
+
},
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/aripalo/rehype-grouping-figure/issues",
|
|
68
|
+
"email": "opensource@aripalo.com"
|
|
69
|
+
},
|
|
70
|
+
"homepage": "https://github.com/aripalo/rehype-grouping-figure#readme",
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsdown",
|
|
73
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest watch",
|
|
76
|
+
"test:coverage": "vitest run --coverage"
|
|
77
|
+
}
|
|
78
|
+
}
|