Gathering detailed insights and metrics for rehype-link-processor
Gathering detailed insights and metrics for rehype-link-processor
Gathering detailed insights and metrics for rehype-link-processor
Gathering detailed insights and metrics for rehype-link-processor
rehype plugin to process links to add custom css class or attributes used by external or download links
npm install rehype-link-processor
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
1 Stars
9 Commits
2 Watchers
1 Branches
1 Contributors
Updated on May 09, 2025
Latest Version
1.0.4
Package Id
rehype-link-processor@1.0.4
Unpacked Size
27.34 kB
Size
6.27 kB
File Count
6
NPM Version
9.6.2
Node Version
18.13.0
Published on
May 02, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
rehype plugin to process links to
rel
or target
This package helps to decorate links usually in a markdown document where no extra attribute can be set.
Common scenarios:
When you write links in markdown, you're limited with just the url, text and title. You cannot add custom attributes, for example a css class to style that specific link as you want.
This package helps to process and transform links.
This is package is module. So an ESM compatible runtime is required (node 14+, deno, ...)
1npm i rehype-link-processor 2# or 3pnpm add rehype-link-processor 4# or 5yarn add rehype-link-processor
unified
pipelineInclude rehype-link-processor in the pipeline:
1import { unified } from "unified"; 2import remarkParse from "remark-parse"; 3import remarkRehype from "remark-rehype"; 4import rehypeLinkProcessor from "rehype-link-processor"; 5 6const file = await unified() 7 .use(remarkParse) 8 .use(remarkRehype) 9 .use(rehypeLinkProcessor) 10 .process("...") 11
mdx
compilationInclude rehype-link-processor in the rehype plugins:
1import {compile} from "@mdx-js/mdx"; 2import rehypeLinkProcessor from "rehype-link-processor"; 3 4const file = '[download:Get the pdf](/assets/article.pdf)'; 5 6await compile(file, { rehypePlugins: [rehypeLinkProcessor] });
Include rehype-link-processor as markdown rehype plugin in the astro.config.ts
:
1//file: astro.config.ts 2import { defineConfig } from "astro/config"; 3import rehypeLinkProcessor from "rehype-link-processor"; 4 5export default defineConfig({ 6 markdown: { 7 rehypePlugins: [rehypeLinkProcessor()] 8 } 9);
The processor works with rules. If a rule matches, the action is applied (the link is transformed), and the processing ends.
So the rule order is important, as the first match wins.
The rules are set via the options argument:
1rehypeLinkProcessor({ 2 rules: [ 3 // add rules here 4 ] 5})
This package provides some builtin rules covering common scenarios. You can also add custom rules to fit you needs.
There are three rule types:
object
function
All builtin rules are enabled by default.
Builtin rules can be disabled with useBuiltin
set to false
.
1rehypeLinkProcessor({ 2 useBuiltin: false 3})
When you disable builtin rules, you can add the ones you like manually:
1rehypeLinkProcessor({
2 rules: [
3 "external" // <-- enable the external link rule
4 ]
5})
or your use the helper R
when you need a configuration:
1rehypeLinkProcessor({
2 rules: [
3 R.download({
4 skipExtension: ["html"] // exclude html to be considered a download
5 })
6 ]
7})
The builtin rules are:
external
looks for external links matching when one of:
http:
or https
:external:
external:
if matched, the resulting a
will have the attributes:
class
= "external"target
= "_blank"rel
= "nofollow noopener"Markdown:
[Github](https://github.com)
HTML:
<a href="https://github.com" class="external" target="_blank" rel="nofollow noopener">Github<a>
Markdown:
[external:Discussion on Github](/discussion)
HTML:
<a href="/discussion" class="external" target="_blank" rel="nofollow noopener">Discussion on Github</a>
download
looks for download links matching when one of:
http:
or https:
and the path ends with .<ext>
where ext
is [1-4] chars long (excluded: html
, htm
, md
, mdx
)download:
download:
if matched, the resulting a
will have the attributes:
class
= "download"download
= the filename extracted or true
when detected by the prefixMarkdown:
[Download the pdf](/assets/my-article.pdf)
HTML:
<a download="my-article.pd" href="/assets/my-article.pdf" class="download">Download the pdf<a>
Markdown:
[download:Get the Archive](/directory?format=zip)
HTML:
<a download href="/directory?format=zip" class="download">Get the Archive</a>
email
looks for email links matching when one of:
mailto:
email:
email:
if matched, the resulting a
will have the attributes:
class
= "email"Markdown:
[Contact us](support@domain.com)
HTML:
<a href="mailto:support@domain.com" class="email">Contact us<a>
Markdown:
[email:Send us a mail](info@domain.com)
HTML:
<a href="mailto:info@domain.com" class="email">Send us a mail<a>
same-page
detect navigation within the same page, aka fragment navigation
#
the resulting a
will have the attributes
class
= "same-page"Markdown:
[Chapter 2](#chapter-2)
HTML:
<a href="#chapter-2" class="same-page">Chapter 2<a>
A match rule, works in two steps:
1rules: [ 2 { 3 match: link => link.href.startWith("mailto:"), 4 action: { className: "email" } 5 } 6]
If the match
returns a falsy value (false
, undefined
, ...). The rule is skipped.
You can specify multiple actions in an Array.
You can use the A
helper witch provides common actions.
1// rule to correct GiThuB link casing 2rules: [ 3 { 4 match: link => link.text?.toLowerCase() === "github" 5 action: [ 6 // add the class: the link can have preexisting class 7 A.mergeClass("brand-link"), 8 // another syntax for { text: "GitHub" } 9 A.set("text", "GitHub") 10 ] 11 } 12]
The match function can also return an object. It will be assigned over the link, overwriting the common fields.
It's useful in a scenario where you want to apply a transformation right away in the matching step.
1rules: [ 2 { 3 match: link => { 4 if(link.href.startsWith("http:")){ 5 return { href: link.href.replace("http:", "https:") }; 6 } 7 }, 8 action: [ 9 A.set("target", "_blank"), 10 ] 11 } 12]
With a transform rule you can analyze any like directly. The transform rule provides a function
based syntax to process links.
With the link as input, a transform rule can return:
You can add a transform rule like any other rule:
1{ 2 rules: [ 3 link => { 4 if (link.href?.includes("github.com")) { 5 return { title: "GitHub: Where this code lives" }; 6 } 7 } 8 ] 9}
This package is built in typescript so it has full typings support.
No vulnerabilities found.
No security vulnerabilities found.