Skip to content

Commit 4837671

Browse files
committed
.
0 parents  commit 4837671

13 files changed

+630
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

.github/workflows/bb.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
jobs:
2+
main:
3+
runs-on: ubuntu-latest
4+
steps:
5+
- uses: unifiedjs/beep-boop-beta@main
6+
with:
7+
repo-token: ${{secrets.GITHUB_TOKEN}}
8+
name: bb
9+
on:
10+
issues:
11+
types: [closed, edited, labeled, opened, reopened, unlabeled]
12+
pull_request_target:
13+
types: [closed, edited, labeled, opened, reopened, unlabeled]

.github/workflows/main.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
jobs:
2+
main:
3+
name: ${{matrix.node}}
4+
runs-on: ubuntu-latest
5+
steps:
6+
- uses: actions/checkout@v4
7+
- uses: actions/setup-node@v4
8+
with:
9+
node-version: ${{matrix.node}}
10+
- run: npm install
11+
- run: npm test
12+
- uses: codecov/codecov-action@v4
13+
strategy:
14+
matrix:
15+
node:
16+
- lts/hydrogen
17+
- node
18+
name: main
19+
on:
20+
- pull_request
21+
- push

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.d.ts
2+
*.log
3+
*.map
4+
*.tsbuildinfo
5+
.DS_Store
6+
coverage/
7+
node_modules/
8+
yarn.lock

.npmrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore-scripts=true
2+
package-lock=false

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.html
3+
*.md

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './lib/index.js'

lib/index.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @import {Element, Root} from 'hast'
3+
*/
4+
5+
import {interactive} from 'hast-util-interactive'
6+
import {whitespace} from 'hast-util-whitespace'
7+
import {SKIP, visit} from 'unist-util-visit'
8+
9+
const unknown = 1
10+
const containsImage = 2
11+
const containsOther = 3
12+
13+
/**
14+
* Remove the wrapping paragraph for images.
15+
*
16+
* @returns
17+
* Transform.
18+
*/
19+
export default function rehypeUnwrapImages() {
20+
/**
21+
* Transform.
22+
*
23+
* @param {Root} tree
24+
* Tree.
25+
* @returns {undefined}
26+
* Nothing.
27+
*/
28+
return function (tree) {
29+
visit(tree, 'element', function (node, index, parent) {
30+
if (
31+
node.tagName === 'p' &&
32+
parent &&
33+
typeof index === 'number' &&
34+
applicable(node, false) === containsImage
35+
) {
36+
parent.children.splice(index, 1, ...node.children)
37+
return [SKIP, index]
38+
}
39+
})
40+
}
41+
}
42+
43+
/**
44+
* Check if a node can be unraveled.
45+
*
46+
* @param {Element} node
47+
* Node.
48+
* @param {boolean} inLink
49+
* Whether the node is in a link.
50+
* @returns {1 | 2 | 3}
51+
* Info.
52+
*/
53+
function applicable(node, inLink) {
54+
/** @type {1 | 2 | 3} */
55+
let image = unknown
56+
let index = -1
57+
58+
while (++index < node.children.length) {
59+
const child = node.children[index]
60+
61+
if (child.type === 'text' && whitespace(child.value)) {
62+
// Whitespace is fine.
63+
} else if (child.type === 'element' && child.tagName === 'img') {
64+
image = containsImage
65+
} else if (!inLink && interactive(child)) {
66+
// Cast as `interactive` is always `Element`.
67+
const linkResult = applicable(/** @type {Element} */ (child), true)
68+
69+
if (linkResult === containsOther) {
70+
return containsOther
71+
}
72+
73+
if (linkResult === containsImage) {
74+
image = containsImage
75+
}
76+
} else {
77+
return containsOther
78+
}
79+
}
80+
81+
return image
82+
}

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Titus Wormer
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
13+
all 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
21+
THE SOFTWARE.

package.json

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
3+
"bugs": "https://github.com/rehypejs/rehype-unwrap-images/issues",
4+
"contributors": [
5+
"Titus Wormer <[email protected]> (https://wooorm.com)"
6+
],
7+
"dependencies": {
8+
"@types/hast": "^3.0.0",
9+
"hast-util-interactive": "^3.0.0",
10+
"hast-util-whitespace": "^3.0.0",
11+
"unist-util-visit": "^5.0.0"
12+
},
13+
"description": "rehype plugin to remove the wrapping paragraph (`<p>`) for images (`<img>`)",
14+
"devDependencies": {
15+
"@types/node": "^22.0.0",
16+
"c8": "^10.0.0",
17+
"prettier": "^3.0.0",
18+
"rehype-parse": "^9.0.0",
19+
"rehype-stringify": "^10.0.0",
20+
"remark-cli": "^12.0.0",
21+
"remark-parse": "^11.0.0",
22+
"remark-preset-wooorm": "^10.0.0",
23+
"remark-rehype": "^11.0.0",
24+
"type-coverage": "^2.0.0",
25+
"typescript": "^5.0.0",
26+
"unified": "^11.0.0",
27+
"xo": "^0.59.0"
28+
},
29+
"exports": "./index.js",
30+
"files": [
31+
"lib/",
32+
"index.d.ts.map",
33+
"index.d.ts",
34+
"index.js"
35+
],
36+
"funding": {
37+
"type": "opencollective",
38+
"url": "https://opencollective.com/unified"
39+
},
40+
"keywords": [
41+
"hast",
42+
"html",
43+
"image",
44+
"plugin",
45+
"rehype-plugin",
46+
"rehype",
47+
"unified",
48+
"unwrap"
49+
],
50+
"license": "MIT",
51+
"name": "rehype-unwrap-images",
52+
"prettier": {
53+
"bracketSpacing": false,
54+
"singleQuote": true,
55+
"semi": false,
56+
"tabWidth": 2,
57+
"trailingComma": "none",
58+
"useTabs": false
59+
},
60+
"remarkConfig": {
61+
"plugins": [
62+
"remark-preset-wooorm"
63+
]
64+
},
65+
"repository": "rehypejs/rehype-unwrap-images",
66+
"scripts": {
67+
"build": "tsc --build --clean && tsc --build && type-coverage",
68+
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
69+
"prepack": "npm run build && npm run format",
70+
"test-api": "node --conditions development test.js",
71+
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
72+
"test": "npm run build && npm run format && npm run test-coverage"
73+
},
74+
"sideEffects": false,
75+
"typeCoverage": {
76+
"atLeast": 100,
77+
"detail": true,
78+
"ignoreCatch": true,
79+
"strict": true
80+
},
81+
"type": "module",
82+
"version": "0.0.0",
83+
"xo": {
84+
"prettier": true
85+
}
86+
}

0 commit comments

Comments
 (0)