https://github.com/brechtcs/rehype-urls
  
  
    Rehype plugin to rewrite URLs 
    https://github.com/brechtcs/rehype-urls
  
        Last synced: 7 months ago 
        JSON representation
    
Rehype plugin to rewrite URLs
- Host: GitHub
- URL: https://github.com/brechtcs/rehype-urls
- Owner: brechtcs
- License: apache-2.0
- Created: 2018-12-06T16:29:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T18:57:25.000Z (over 2 years ago)
- Last Synced: 2025-03-28T05:24:35.116Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 10.7 KB
- Stars: 14
- Watchers: 2
- Forks: 3
- Open Issues: 1
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
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
```
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
```
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
        
 
