rehype-external-links
Advanced tools
Comparing version
@@ -1,8 +0,4 @@ | ||
/** | ||
* Plugin to automatically add `target` and `rel` attributes to external links. | ||
* | ||
* @type {import('unified').Plugin<[Options?] | Array<void>, Root>} | ||
*/ | ||
export default function rehypeExternalLinks( | ||
options?: void | Options | undefined | ||
this: import('unified').Processor<void, import('hast').Root, void, void>, | ||
...settings: [(Options | undefined)?] | void[] | ||
): | ||
@@ -14,2 +10,3 @@ | void | ||
export type Element = import('hast').Element | ||
export type Test = import('hast-util-is-element').Test | ||
export type ElementChild = Element['children'][number] | ||
@@ -65,2 +62,8 @@ export type Target = '_self' | '_blank' | '_parent' | '_top' | ||
| undefined | ||
/** | ||
* Additional test to define which external link elements are modified. | ||
* Any test that can be given to `hast-util-is-element` is supported. | ||
* The default (no test) is to modify all external links. | ||
*/ | ||
test?: Test | ||
} |
13
index.js
@@ -5,2 +5,3 @@ /** | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('hast-util-is-element').Test} Test | ||
* | ||
@@ -57,2 +58,6 @@ * @typedef {Element['children'][number]} ElementChild | ||
* hast properties to add to the `span` wrapping `content`, when given. | ||
* @property {Test} [test] | ||
* Additional test to define which external link elements are modified. | ||
* Any test that can be given to `hast-util-is-element` is supported. | ||
* The default (no test) is to modify all external links. | ||
*/ | ||
@@ -62,2 +67,3 @@ | ||
import {parse} from 'space-separated-tokens' | ||
import {convertElement} from 'hast-util-is-element' | ||
import isAbsoluteUrl from 'is-absolute-url' | ||
@@ -88,8 +94,11 @@ import extend from 'extend' | ||
export default function rehypeExternalLinks(options = {}) { | ||
const is = convertElement(options.test) | ||
return (tree) => { | ||
visit(tree, 'element', (node) => { | ||
visit(tree, 'element', (node, index, parent) => { | ||
if ( | ||
node.tagName === 'a' && | ||
node.properties && | ||
typeof node.properties.href === 'string' | ||
typeof node.properties.href === 'string' && | ||
is(node, index, parent) | ||
) { | ||
@@ -96,0 +105,0 @@ const url = node.properties.href |
{ | ||
"name": "rehype-external-links", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "rehype plugin to automatically add `target` and `rel` attributes to external links", | ||
@@ -39,2 +39,3 @@ "license": "MIT", | ||
"extend": "^3.0.0", | ||
"hast-util-is-element": "^2.0.0", | ||
"is-absolute-url": "^4.0.0", | ||
@@ -55,4 +56,4 @@ "space-separated-tokens": "^2.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.51.0" | ||
"typescript": "^5.0.0", | ||
"xo": "^0.54.0" | ||
}, | ||
@@ -59,0 +60,0 @@ "scripts": { |
@@ -163,2 +163,11 @@ # rehype-external-links | ||
###### `options.test` | ||
Additional test to define which external link elements are modified. | ||
Any test that can be given to [hast-util-is-element](https://github.com/syntax-tree/hast-util-is-element) | ||
is supported. The default (no test) is to modify all external links. | ||
> 👉 **Note**: in this case it only makes sense to provide a test function | ||
> since this plugin will only consider `a` tags. | ||
## Examples | ||
@@ -165,0 +174,0 @@ |
19868
5.75%198
5.88%310
2.99%7
16.67%+ Added
+ Added