Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 11, 2024
0 parents commit 4837671
Show file tree
Hide file tree
Showing 13 changed files with 630 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
13 changes: 13 additions & 0 deletions .github/workflows/bb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: unifiedjs/beep-boop-beta@main
with:
repo-token: ${{secrets.GITHUB_TOKEN}}
name: bb
on:
issues:
types: [closed, edited, labeled, opened, reopened, unlabeled]
pull_request_target:
types: [closed, edited, labeled, opened, reopened, unlabeled]
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v4
strategy:
matrix:
node:
- lts/hydrogen
- node
name: main
on:
- pull_request
- push
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.d.ts
*.log
*.map
*.tsbuildinfo
.DS_Store
coverage/
node_modules/
yarn.lock
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore-scripts=true
package-lock=false
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
*.html
*.md
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './lib/index.js'
82 changes: 82 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @import {Element, Root} from 'hast'
*/

import {interactive} from 'hast-util-interactive'
import {whitespace} from 'hast-util-whitespace'
import {SKIP, visit} from 'unist-util-visit'

const unknown = 1
const containsImage = 2
const containsOther = 3

/**
* Remove the wrapping paragraph for images.
*
* @returns
* Transform.
*/
export default function rehypeUnwrapImages() {
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'element', function (node, index, parent) {
if (
node.tagName === 'p' &&
parent &&
typeof index === 'number' &&
applicable(node, false) === containsImage
) {
parent.children.splice(index, 1, ...node.children)
return [SKIP, index]
}
})
}
}

/**
* Check if a node can be unraveled.
*
* @param {Element} node
* Node.
* @param {boolean} inLink
* Whether the node is in a link.
* @returns {1 | 2 | 3}
* Info.
*/
function applicable(node, inLink) {
/** @type {1 | 2 | 3} */
let image = unknown
let index = -1

while (++index < node.children.length) {
const child = node.children[index]

if (child.type === 'text' && whitespace(child.value)) {
// Whitespace is fine.
} else if (child.type === 'element' && child.tagName === 'img') {
image = containsImage
} else if (!inLink && interactive(child)) {
// Cast as `interactive` is always `Element`.
const linkResult = applicable(/** @type {Element} */ (child), true)

if (linkResult === containsOther) {
return containsOther
}

if (linkResult === containsImage) {
image = containsImage
}
} else {
return containsOther
}
}

return image
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Titus Wormer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
86 changes: 86 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"bugs": "https://github.com/rehypejs/rehype-unwrap-images/issues",
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"dependencies": {
"@types/hast": "^3.0.0",
"hast-util-interactive": "^3.0.0",
"hast-util-whitespace": "^3.0.0",
"unist-util-visit": "^5.0.0"
},
"description": "rehype plugin to remove the wrapping paragraph (`<p>`) for images (`<img>`)",
"devDependencies": {
"@types/node": "^22.0.0",
"c8": "^10.0.0",
"prettier": "^3.0.0",
"rehype-parse": "^9.0.0",
"rehype-stringify": "^10.0.0",
"remark-cli": "^12.0.0",
"remark-parse": "^11.0.0",
"remark-preset-wooorm": "^10.0.0",
"remark-rehype": "^11.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unified": "^11.0.0",
"xo": "^0.59.0"
},
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts.map",
"index.d.ts",
"index.js"
],
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"keywords": [
"hast",
"html",
"image",
"plugin",
"rehype-plugin",
"rehype",
"unified",
"unwrap"
],
"license": "MIT",
"name": "rehype-unwrap-images",
"prettier": {
"bracketSpacing": false,
"singleQuote": true,
"semi": false,
"tabWidth": 2,
"trailingComma": "none",
"useTabs": false
},
"remarkConfig": {
"plugins": [
"remark-preset-wooorm"
]
},
"repository": "rehypejs/rehype-unwrap-images",
"scripts": {
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . --frail --output --quiet && prettier . --log-level warn --write && xo --fix",
"prepack": "npm run build && npm run format",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"sideEffects": false,
"typeCoverage": {
"atLeast": 100,
"detail": true,
"ignoreCatch": true,
"strict": true
},
"type": "module",
"version": "0.0.0",
"xo": {
"prettier": true
}
}
Loading

0 comments on commit 4837671

Please sign in to comment.