Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rehype-remove-images #47

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/rehype-remove-images/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
43 changes: 43 additions & 0 deletions packages/rehype-remove-images/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* rehype plugin to remove images.
*
* ## What is this?
*
* This package is a plugin that removes images.
*
* ## When should I use this?
*
* You can use this plugin when you want to improve the transfer size of HTML
* documents.
*
* ## API
*
* ### `unified().use(rehypeRemoveImages[, options])`
*
* Remove comments.
*
* @example
* <img src="/something.png" />
*/

/**
* @typedef {import('hast').Root} Root
* @typedef {import('unist').Node} Node
* @typedef {Root|Root['children'][number]} HastNode
*/

import { remove } from "unist-util-remove"
import { isElement } from "hast-util-is-element"

/**
* Remove images from HTML
*
* @type {import('unified').Plugin<[Options?] | Array<void>, Root>}
*/
export default function rehypeRemoveImages() {
return (tree) =>
remove(tree, { cascade: true }, (node) => {
// note that `type` is not the same as `tagName`!
return isElement(node, "img")
}) || undefined
}
49 changes: 49 additions & 0 deletions packages/rehype-remove-images/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "rehype-remove-images",
"version": "1.0.0",
"description": "rehype plugin to remove images",
"license": "MIT",
"keywords": [
"unified",
"rehype",
"rehype-plugin",
"plugin",
"html",
"minify",
"mangle",
"comment"
],
"repository": "https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-remove-images",
"bugs": "https://github.com/rehypejs/rehype-minify/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Michael Bianco <[email protected]> (http://mikebian.co)",
"contributors": [
"Michael Bianco <[email protected]>"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"unist-util-remove": "^4.0.0",
"hast-util-is-element": "^2.1.3"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"test": "node --conditions development test.js"
},
"xo": false,
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
237 changes: 237 additions & 0 deletions packages/rehype-remove-images/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<!--This file is generated-->

# rehype-remove-comments

[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

**[rehype][]** plugin to remove comments.

## Contents

* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`unified().use(rehypeRemoveComments[, options])`](#unifieduserehyperemovecomments-options)
* [Example](#example)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Contribute](#contribute)
* [License](#license)

## What is this?

This package is a plugin that removes comments.
By default it keeps conditional comments, optionally it removes them too.

## When should I use this?

You can use this plugin when you want to improve the transfer size of HTML
documents.

## Install

This package is [ESM only][esm].
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:

```sh
npm install rehype-remove-comments
```

In Deno with [`esm.sh`][esmsh]:

```js
import rehypeRemoveComments from 'https://esm.sh/rehype-remove-comments@5'
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
import rehypeRemoveComments from 'https://esm.sh/rehype-remove-comments@5?bundle'
</script>
```

## Use

On the API:

```js
import {read} from 'to-vfile'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import rehypeRemoveComments from 'rehype-remove-comments'

main()

async function main() {
const file = await unified()
.use(rehypeParse)
.use(rehypeRemoveComments)
.use(rehypeStringify)
.process(await read('index.html'))

console.log(String(file))
}
```

On the CLI:

```sh
rehype input.html --use rehype-remove-comments --output output.html
```

On the CLI in a config file (here a `package.json`):

```diff
"rehype": {
"plugins": [
+ "rehype-remove-comments",
]
}
```

## API

This package exports no identifiers.
The default export is `rehypeRemoveComments`.

### `unified().use(rehypeRemoveComments[, options])`

Remove comments.

##### `options`

Configuration (optional).

###### `options.removeConditional`

Whether to remove conditional comments too (`boolean`, default: `false`).
The default behavior is to keep conditional comments.
Conditional comments are a legacy feature that was specific to Internet
Explorer.
They were no longer used in IE 10.

## Example

###### In

```html
<!--Hello-->
<!--[if IE 6]>OK<![endif]-->
```

###### Out

```html
<!--[if IE 6]>OK<![endif]-->
```

## Syntax

HTML is handled according to WHATWG HTML (the living standard), which is also
followed by browsers such as Chrome and Firefox.

## Syntax tree

The syntax tree format used is [`hast`][hast].

## Types

This package is fully typed with [TypeScript][].

## Compatibility

Projects maintained by the unified collective are compatible with all maintained
versions of Node.js.
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
Our projects sometimes work with older versions, but this is not guaranteed.

## Security

As **rehype** works on HTML, and improper use of HTML can open you up to a
[cross-site scripting (XSS)][xss] attack, use of rehype can also be unsafe.
Use [`rehype-sanitize`][rehype-sanitize] to make the tree safe.

## Contribute

See [`contributing.md`][contributing] in [`rehypejs/.github`][health] for ways
to get started.
See [`support.md`][support] for ways to get help.

This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.

## License

[MIT][license] © [Titus Wormer][author]

[build-badge]: https://github.com/rehypejs/rehype-minify/workflows/main/badge.svg

[build]: https://github.com/rehypejs/rehype-minify/actions

[coverage-badge]: https://img.shields.io/codecov/c/github/rehypejs/rehype-minify.svg

[coverage]: https://codecov.io/github/rehypejs/rehype-minify

[downloads-badge]: https://img.shields.io/npm/dm/rehype-remove-comments.svg

[downloads]: https://www.npmjs.com/package/rehype-remove-comments

[size-badge]: https://img.shields.io/bundlephobia/minzip/rehype-remove-comments.svg

[size]: https://bundlephobia.com/result?p=rehype-remove-comments

[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[backers-badge]: https://opencollective.com/unified/backers/badge.svg

[collective]: https://opencollective.com/unified

[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg

[chat]: https://github.com/rehypejs/rehype/discussions

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[npm]: https://docs.npmjs.com/cli/install

[esmsh]: https://esm.sh

[typescript]: https://www.typescriptlang.org

[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize

[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

[health]: https://github.com/rehypejs/.github

[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md

[support]: https://github.com/rehypejs/.github/blob/main/support.md

[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md

[license]: https://github.com/rehypejs/rehype-minify/blob/main/license

[author]: https://wooorm.com

[hast]: https://github.com/syntax-tree/hast

[rehype]: https://github.com/rehypejs/rehype
34 changes: 34 additions & 0 deletions packages/rehype-remove-images/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'tape'
import {rehype} from 'rehype'
import {u} from 'unist-builder'
import {h} from 'hastscript'
import min from './index.js'

test('rehype-remove-comments', (t) => {
t.deepEqual(
rehype()
.use(min)
.runSync(u('root', [h('div', [{type: 'comment', value: 'foo'}])])),
u('root', [h('div')])
)

t.deepEqual(
rehype()
.use(min)
.runSync(
u('root', [h('div', [{type: 'comment', value: '[if IE]>…<![endif]'}])])
),
u('root', [h('div', [{type: 'comment', value: '[if IE]>…<![endif]'}])])
)

t.deepEqual(
rehype()
.use(min, {removeConditional: true})
.runSync(
u('root', [h('div', [{type: 'comment', value: '[if IE]>…<![endif]'}])])
),
u('root', [h('div')])
)

t.end()
})
4 changes: 4 additions & 0 deletions packages/rehype-remove-images/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["*.js"]
}
Loading