Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brechtcs/rehype-urls

Rehype plugin to rewrite URLs
https://github.com/brechtcs/rehype-urls

Last synced: about 11 hours ago
JSON representation

Rehype plugin to rewrite URLs

Awesome Lists containing this project

README

        

# rehype-urls

Rehype plugin to rewrite URLs of `href` and `src` attributes.

## Installation

```sh
npm install rehype-urls
```

## Usage

Given this markup:

```html


page
link

```

You can use the following script:

```js
var rehype = require('rehype')
var urls = require('rehype-urls')

rehype()
.use(urls, removeBaseUrl)
.process(input, handleOutput)

function removeBaseUrl (url) {
if (url.host === 'internal.site') {
return url.path
}
}
```

Which will transform it into:

```html


page
link

```

You can also pass in an object:

```js
rehype()
.use(urls, { transform: removeBaseUrl })
.process(input, handleOutput)
```

### Mutate nodes

It's also possible to mutate the URL nodes directly. This example will add `target="_blank"` to any external links:

```js
var rehype = require('rehype')
var urls = require('rehype-urls')

rehype()
.use(urls, blankExternal)
.process(input, handleOutput)

function blankExternal (url, node) {
if (url.host !== 'internal.site') {
node.properties.target = '_blank'
}
}
```

## License

Apache-2.0