rehype-image-caption 1.0.0 → 2.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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import type { Plugin } from "unified";
1
+ import "mdast-util-mdx-jsx";
2
2
  import type { Root } from "hast";
3
+ import type { Plugin } from "unified";
3
4
  /**
4
5
  * `rehype` plugin to set captions for images in addition to alt text.
5
6
  * @returns Transformer
package/dist/index.js CHANGED
@@ -1,5 +1,12 @@
1
+ import "mdast-util-mdx-jsx";
1
2
  import { isElement } from "hast-util-is-element";
2
3
  import { visit } from "unist-util-visit";
4
+ /**
5
+ * Check if a node is an image.
6
+ * @param node Node to check
7
+ * @returns `true` if the node is an image, `false` otherwise
8
+ */
9
+ const isImage = (node) => isElement(node, "img") || (node.type === "mdxJsxFlowElement" && ["astro-image", "img"].includes(node.name || ""));
3
10
  /**
4
11
  * `rehype` plugin to set captions for images in addition to alt text.
5
12
  * @returns Transformer
@@ -38,9 +45,11 @@ const rehypeImageCaption = () => {
38
45
  * Transformer
39
46
  * @param tree Root node
40
47
  */
48
+ // eslint-disable-next-line max-lines-per-function
41
49
  const transformer = (tree) => {
50
+ // eslint-disable-next-line max-lines-per-function
42
51
  visit(tree, "element", (node) => {
43
- if (!(node.tagName === "p" && isElement(node.children[0], "img")))
52
+ if (!(node.tagName === "p" && isImage(node.children[0])))
44
53
  return;
45
54
  // eslint-disable-next-line no-magic-numbers
46
55
  if (node.children.length === 1) {
@@ -80,6 +89,25 @@ const rehypeImageCaption = () => {
80
89
  }
81
90
  ];
82
91
  }
92
+ else if (
93
+ // eslint-disable-next-line no-magic-numbers
94
+ node.children.length === 4 &&
95
+ isElement(node.children[1], "br") &&
96
+ node.children[2].type === "text" &&
97
+ node.children[2].value.trim() === "" &&
98
+ isElement(node.children[3], "em")) {
99
+ // Image with caption with line break (with remark-breaks plugin)
100
+ node.tagName = "figure";
101
+ node.children = [
102
+ node.children[0],
103
+ {
104
+ type: "element",
105
+ tagName: "figcaption",
106
+ properties: {},
107
+ children: node.children[3].children
108
+ }
109
+ ];
110
+ }
83
111
  });
84
112
  };
85
113
  return transformer;
package/package.json CHANGED
@@ -1,52 +1,54 @@
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
- }
1
+ {
2
+ "name": "rehype-image-caption",
3
+ "version": "2.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-breaks": "^4.0.0",
42
+ "remark-parse": "^11.0.0",
43
+ "remark-rehype": "^11.1.0",
44
+ "typescript": "^5.4.2",
45
+ "vitest": "^1.4.0"
46
+ },
47
+ "dependencies": {
48
+ "hast-util-is-element": "^3.0.0",
49
+ "mdast-util-mdx-jsx": "^3.1.2",
50
+ "rehype": "^13.0.1",
51
+ "unified": "^11.0.4",
52
+ "unist-util-visit": "^5.0.0"
53
+ }
54
+ }