rehype-image-caption 2.0.9 → 2.0.11

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.
Files changed (2) hide show
  1. package/dist/index.js +11 -6
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -58,22 +58,25 @@ const rehypeImageCaption = () => {
58
58
  */
59
59
  // eslint-disable-next-line max-lines-per-function
60
60
  const transformer = (tree) => {
61
- // eslint-disable-next-line max-lines-per-function
61
+ // eslint-disable-next-line max-lines-per-function, max-statements
62
62
  visit(tree, "element", (node) => {
63
- if (!(node.tagName === "p" && isImage(node.children[0])))
63
+ const [firstChild] = node.children;
64
+ if (!firstChild)
65
+ return;
66
+ if (!(node.tagName === "p" && isImage(firstChild)))
64
67
  return;
65
68
  // eslint-disable-next-line no-magic-numbers
66
69
  if (node.children.length === 1) {
67
70
  // Image without caption
68
71
  node.tagName = "figure";
69
- node.children = [node.children[0]];
72
+ node.children = [firstChild];
70
73
  // eslint-disable-next-line no-magic-numbers
71
74
  }
72
75
  else if (node.children.length === 2 && isElement(node.children[1], "em")) {
73
76
  // Image with caption without line break
74
77
  node.tagName = "figure";
75
78
  node.children = [
76
- node.children[0],
79
+ firstChild,
77
80
  {
78
81
  type: "element",
79
82
  tagName: "figcaption",
@@ -85,13 +88,14 @@ const rehypeImageCaption = () => {
85
88
  else if (
86
89
  // eslint-disable-next-line no-magic-numbers
87
90
  node.children.length === 3 &&
91
+ node.children[1] &&
88
92
  node.children[1].type === "text" &&
89
93
  node.children[1].value.trim() === "" &&
90
94
  isElement(node.children[2], "em")) {
91
95
  // Image with caption with line break
92
96
  node.tagName = "figure";
93
97
  node.children = [
94
- node.children[0],
98
+ firstChild,
95
99
  {
96
100
  type: "element",
97
101
  tagName: "figcaption",
@@ -104,13 +108,14 @@ const rehypeImageCaption = () => {
104
108
  // eslint-disable-next-line no-magic-numbers
105
109
  node.children.length === 4 &&
106
110
  isElement(node.children[1], "br") &&
111
+ node.children[2] &&
107
112
  node.children[2].type === "text" &&
108
113
  node.children[2].value.trim() === "" &&
109
114
  isElement(node.children[3], "em")) {
110
115
  // Image with caption with line break (with remark-breaks plugin)
111
116
  node.tagName = "figure";
112
117
  node.children = [
113
- node.children[0],
118
+ firstChild,
114
119
  {
115
120
  type: "element",
116
121
  tagName: "figcaption",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rehype-image-caption",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "description": "rehype plugin to set captions for images in addition to alt text.",
5
5
  "publishConfig": {
6
6
  "provenance": true
@@ -39,7 +39,7 @@
39
39
  "@changesets/changelog-github": "^0.5.0",
40
40
  "@changesets/cli": "^2.27.1",
41
41
  "@robot-inventor/eslint-config": "^6.0.0",
42
- "@robot-inventor/tsconfig-base": "^1.1.0",
42
+ "@robot-inventor/tsconfig-base": "^4.0.0",
43
43
  "eslint": "^9.0.0",
44
44
  "prettier": "^3.2.5",
45
45
  "rehype-stringify": "^10.0.0",