rehypejs / rehype-external-links Goto Github PK
View Code? Open in Web Editor NEWrehype plugin to add rel (and target) to external links
Home Page: https://unifiedjs.com
License: MIT License
rehype plugin to add rel (and target) to external links
Home Page: https://unifiedjs.com
License: MIT License
For external links I'd like to add aria-label="(opens in a new window)"
to the <a>
tag. It would be great if there was an option to do this, e.g.:
markdown: {
rehypePlugins: [
[
rehypeExternalLinks, {
ariaLabel: '(opens in new window)',
protocols: ['http', 'https', 'mailto'],
rel: [],
target: '_blank',
}
],
],
},
Add ariaLabel
option as seen above.
For more flexibility, maybe offer a way to add any custom attribute, similar to options.contentProperties
but which applies to <a>
.
I'd like to use this plugin to add target
and rel
properties to links with relative urls. Currently only absolute urls are supported.
I suggest the test
option to be responsible for that. By default it filters links with absolute urls, and you can override it. This is a breaking change.
includeRelative
This plugin’s defaults behavior disagrees with reasonable web recommendations (https://css-tricks.com/use-target_blank/) accessibility recommendation (https://www.w3.org/TR/WCAG20-TECHS/G201.html), or precedence set by sites such as GitHub.
target
rel
should just be ['nofollow']
n/a
Right now all external links get targeted but there are instances where users may want to exclude a specific link.
In my use case, I use rehype-external-links
for all externals links, I then attach an icon to the external links to indicate that the link takes the user out of the page.
The problem comes when attaching an external link to images, an extra icon appears messing up layout. In my use case being able to exclude links individually would go a long way.
Thanks for creating this plugin.
1.0.0-2.0.0
https://codesandbox.io/s/relaxed-sound-irixmt?file=/src/index.js
Based on the example in the README:
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeStringify from 'rehype-stringify'
const file = await unified()
.use(rehypeParse)
.use(rehypeExternalLinks, {rel: ['nofollow']})
.use(rehypeStringify)
.process('<a href="//github.com/rehypejs/rehype">rehype</a>')
console.log(String(file))
This example uses a protocol-relative URL, which is widely considered an anti-pattern in actual development, but is still supported by browsers.
Protocol-relative URLs include the domain name, and thus on web sites are just like an external http/https URL. Thus we expect:
<html><head></head><body><a href="//github.com/rehypejs/rehype" rel="nofollow">rehype</a></body></html>
Because there is no protocol, this particular line determines the protocol for this URL is //github.com/rehypejs/rehyp
which is not in any reasonably-configured protocol list, so rel="nofollow" is not added.
<html><head></head><body><a href="//github.com/rehypejs/rehype">rehype</a></body></html>
This behavior was introduced in, best I can tell, remarkjs/remark-external-links#10, which added this option because of weird interactions between mailto:
links and target="_blank"
, the latter of which has since been removed from this codebase.
Node v16
npm 8
macOS
No response
The package in npm is not same with Github, npm package can't support dynamic target。
publish latest version to npm
。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.