remark-text-link
v0.1.0
Published
remark plugin to convert text to link by global config.
Downloads
12
Maintainers
Readme
remark-text-link
This package is unified (remark) plugin to convert text to link by global config.
Install
npm install remark-text-linkUsage
see example.ts
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'
import remarkTextLink from 'remark-text-link'
const file = await unified()
.use(remarkParse)
.use(remarkTextLink, {
// your config here
'next.js': {
url: 'https://github.com/vercel/next.js',
text: 'Next.js',
},
})
.use(remarkRehype)
.use(rehypeStringify)
.process('next.js')
console.log(String(file))
// output:
// <p><a href="https://github.com/vercel/next.js">Next.js</a></p>Skip text
You might not want the text to be converted, you can use a safer options renderText = false.
await unified()
.use(remarkParse)
.use(remarkTextLink, {
// your config here
'next.js': {
url: 'https://github.com/vercel/next.js',
text: 'Next.js',
},
{ renderText: false } // options
})Option
The options type [TextLinkValueType, TextLinkPluginOptions] definitions:
import type { Link, PhrasingContent } from 'mdast'
type TextLinkValueType =
| string
| ({
text?: string
children?: PhrasingContent[]
} & Omit<Link, 'type' | 'position' | 'children'>)
interface TextLinkPluginOptions {
/**
* render Text node
* @default true
*/
renderText?: boolean
/**
* render Link node
* @default true
*/
renderLink?: boolean
}Markdown example
See the innput file input.md and the output file output.md.
Test
run pnpm test
