rehype-image-caption 1.0.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 +86 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +87 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 roboin
|
|
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,86 @@
|
|
|
1
|
+
# rehype-image-caption
|
|
2
|
+
|
|
3
|
+
rehype plugin to set captions for images in addition to alt text.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install rehype-image-caption
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Example
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import remarkParse from "remark-parse";
|
|
15
|
+
import rehypeStringify from "rehype-stringify";
|
|
16
|
+
import remarkRehype from "remark-rehype";
|
|
17
|
+
import { unified } from "unified";
|
|
18
|
+
import rehypeImageCaption from "rehype-image-caption";
|
|
19
|
+
|
|
20
|
+
const processor = unified()
|
|
21
|
+
.use(remarkParse)
|
|
22
|
+
.use(remarkRehype)
|
|
23
|
+
.use(rehypeImageCaption)
|
|
24
|
+
.use(rehypeStringify);
|
|
25
|
+
|
|
26
|
+
const markdown = `
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
*caption text*
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
*caption text*
|
|
33
|
+
`;
|
|
34
|
+
|
|
35
|
+
processor.process(markdown).then((result) => {
|
|
36
|
+
console.log(result.toString());
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The above code will output the following:
|
|
41
|
+
|
|
42
|
+
```html
|
|
43
|
+
<figure>
|
|
44
|
+
<img src="image.jpg" alt="alt text">
|
|
45
|
+
</figure>
|
|
46
|
+
<figure>
|
|
47
|
+
<img src="image.jpg" alt="alt text">
|
|
48
|
+
<figcaption>caption text</figcaption>
|
|
49
|
+
</figure>
|
|
50
|
+
<figure>
|
|
51
|
+
<img src="image.jpg" alt="alt text">
|
|
52
|
+
<figcaption>caption text</figcaption>
|
|
53
|
+
</figure>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
### Build
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
npm run build
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Format
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
npm run format
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
or
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
npm run format:check
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Lint
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
npm run lint
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Test
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
npm run test
|
|
86
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Plugin } from "unified";
|
|
2
|
+
import type { Root } from "hast";
|
|
3
|
+
/**
|
|
4
|
+
* `rehype` plugin to set captions for images in addition to alt text.
|
|
5
|
+
* @returns Transformer
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* Input:
|
|
9
|
+
*
|
|
10
|
+
* ```markdown
|
|
11
|
+
* 
|
|
12
|
+
*
|
|
13
|
+
* *caption text*
|
|
14
|
+
*
|
|
15
|
+
* 
|
|
16
|
+
* *caption text*
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Output:
|
|
20
|
+
*
|
|
21
|
+
* ```html
|
|
22
|
+
* <figure>
|
|
23
|
+
* <img src="image.jpg" alt="alt text">
|
|
24
|
+
* </figure>
|
|
25
|
+
* <figure>
|
|
26
|
+
* <img src="image.jpg" alt="alt text">
|
|
27
|
+
* <figcaption>caption text</figcaption>
|
|
28
|
+
* </figure>
|
|
29
|
+
* <figure>
|
|
30
|
+
* <img src="image.jpg" alt="alt text">
|
|
31
|
+
* <figcaption>caption text</figcaption>
|
|
32
|
+
* </figure>
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
declare const rehypeImageCaption: Plugin<[], Root>;
|
|
36
|
+
export default rehypeImageCaption;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { isElement } from "hast-util-is-element";
|
|
2
|
+
import { visit } from "unist-util-visit";
|
|
3
|
+
/**
|
|
4
|
+
* `rehype` plugin to set captions for images in addition to alt text.
|
|
5
|
+
* @returns Transformer
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* Input:
|
|
9
|
+
*
|
|
10
|
+
* ```markdown
|
|
11
|
+
* 
|
|
12
|
+
*
|
|
13
|
+
* *caption text*
|
|
14
|
+
*
|
|
15
|
+
* 
|
|
16
|
+
* *caption text*
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Output:
|
|
20
|
+
*
|
|
21
|
+
* ```html
|
|
22
|
+
* <figure>
|
|
23
|
+
* <img src="image.jpg" alt="alt text">
|
|
24
|
+
* </figure>
|
|
25
|
+
* <figure>
|
|
26
|
+
* <img src="image.jpg" alt="alt text">
|
|
27
|
+
* <figcaption>caption text</figcaption>
|
|
28
|
+
* </figure>
|
|
29
|
+
* <figure>
|
|
30
|
+
* <img src="image.jpg" alt="alt text">
|
|
31
|
+
* <figcaption>caption text</figcaption>
|
|
32
|
+
* </figure>
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
// eslint-disable-next-line max-lines-per-function
|
|
36
|
+
const rehypeImageCaption = () => {
|
|
37
|
+
/**
|
|
38
|
+
* Transformer
|
|
39
|
+
* @param tree Root node
|
|
40
|
+
*/
|
|
41
|
+
const transformer = (tree) => {
|
|
42
|
+
visit(tree, "element", (node) => {
|
|
43
|
+
if (!(node.tagName === "p" && isElement(node.children[0], "img")))
|
|
44
|
+
return;
|
|
45
|
+
// eslint-disable-next-line no-magic-numbers
|
|
46
|
+
if (node.children.length === 1) {
|
|
47
|
+
// Image without caption
|
|
48
|
+
node.tagName = "figure";
|
|
49
|
+
node.children = [node.children[0]];
|
|
50
|
+
// eslint-disable-next-line no-magic-numbers
|
|
51
|
+
}
|
|
52
|
+
else if (node.children.length === 2 && isElement(node.children[1], "em")) {
|
|
53
|
+
// Image with caption without line break
|
|
54
|
+
node.tagName = "figure";
|
|
55
|
+
node.children = [
|
|
56
|
+
node.children[0],
|
|
57
|
+
{
|
|
58
|
+
type: "element",
|
|
59
|
+
tagName: "figcaption",
|
|
60
|
+
properties: {},
|
|
61
|
+
children: node.children[1].children
|
|
62
|
+
}
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
else if (
|
|
66
|
+
// eslint-disable-next-line no-magic-numbers
|
|
67
|
+
node.children.length === 3 &&
|
|
68
|
+
node.children[1].type === "text" &&
|
|
69
|
+
node.children[1].value.trim() === "" &&
|
|
70
|
+
isElement(node.children[2], "em")) {
|
|
71
|
+
// Image with caption with line break
|
|
72
|
+
node.tagName = "figure";
|
|
73
|
+
node.children = [
|
|
74
|
+
node.children[0],
|
|
75
|
+
{
|
|
76
|
+
type: "element",
|
|
77
|
+
tagName: "figcaption",
|
|
78
|
+
properties: {},
|
|
79
|
+
children: node.children[2].children
|
|
80
|
+
}
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
return transformer;
|
|
86
|
+
};
|
|
87
|
+
export default rehypeImageCaption;
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "rehype-image-caption",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "rehype plugin to set captions for images in addition to alt text.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"./dist/"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "vitest",
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"prepublishOnly": "npm run build",
|
|
15
|
+
"format": "prettier ./src/ --write",
|
|
16
|
+
"format:check": "prettier ./src/ --check",
|
|
17
|
+
"lint": "npx eslint ./src/**/*.ts"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Robot-Inventor/rehype-image-caption.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"rehype",
|
|
25
|
+
"rehype-plugin"
|
|
26
|
+
],
|
|
27
|
+
"author": "Robot-Inventor",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/Robot-Inventor/rehype-image-caption/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/Robot-Inventor/rehype-image-caption#readme",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^7.2.0",
|
|
35
|
+
"@typescript-eslint/parser": "^7.2.0",
|
|
36
|
+
"eslint": "^8.57.0",
|
|
37
|
+
"eslint-config-prettier": "^9.1.0",
|
|
38
|
+
"eslint-plugin-jsdoc": "^48.2.1",
|
|
39
|
+
"prettier": "^3.2.5",
|
|
40
|
+
"rehype-stringify": "^10.0.0",
|
|
41
|
+
"remark-parse": "^11.0.0",
|
|
42
|
+
"remark-rehype": "^11.1.0",
|
|
43
|
+
"typescript": "^5.4.2",
|
|
44
|
+
"vitest": "^1.4.0"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"hast-util-is-element": "^3.0.0",
|
|
48
|
+
"rehype": "^13.0.1",
|
|
49
|
+
"unified": "^11.0.4",
|
|
50
|
+
"unist-util-visit": "^5.0.0"
|
|
51
|
+
}
|
|
52
|
+
}
|