# rehype-urls

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

Latest version **1.2.0** (published 2023-07-19) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install rehype-urls
pnpm add rehype-urls
yarn add rehype-urls
bun add rehype-urls
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2023-07-19 |
| First published | 2018-12-06 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 14 |
| Author | Brecht Savelkoul |
| Maintainers | savelbr |
| Keywords | rehype, hast, rewrite, url, link, src, href |

## Links

- npm: https://www.npmjs.com/package/rehype-urls
- Repository: https://github.com/brechtcs/rehype-urls
- Homepage: https://github.com/brechtcs/rehype-urls#readme
- Issues: https://github.com/brechtcs/rehype-urls/issues
- npm.io page: https://npm.io/package/rehype-urls

## Dependencies (3)

- [stdopt](https://npm.io/package/stdopt.md) ^2.0.0
- [unist-util-visit](https://npm.io/package/unist-util-visit.md) ^1.4.0
- [hast-util-has-property](https://npm.io/package/hast-util-has-property.md) ^1.0.2

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.2.0 (latest) — 2023-07-19
- 1.1.1 — 2019-11-22
- 1.1.0 — 2019-11-22
- 1.0.0 — 2018-12-06

## README

# rehype-urls

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

## Installation

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

## Usage

Given this markup:

```html
<article>
  <img src="http://internal.site/image.jpg">
  <a href="http://internal.site/page.html">page</a>
  <a href="http://example.com">link</a>
</article>
```

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
<article>
  <img src="/image.jpg">
  <a href="/page.html">page</a>
  <a href="http://example.com">link</a>
</article>
```

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

---
_Source: https://npm.io/package/rehype-urls · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
