Skip to content

Commit

Permalink
feat: use rehype-external-links
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Mar 8, 2024
1 parent e60d0b1 commit bbb444a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
69 changes: 67 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"mdast-util-gfm-strikethrough": "^2.0.0",
"micromark-extension-gfm-strikethrough": "^2.0.0",
"micromark-util-combine-extensions": "^2.0.0",
"rehype-external-links": "^3.0.0",
"rehype-format": "^5.0.0",
"rehype-parse": "^9.0.0",
"rehype-raw": "^7.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/html2md2html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe('HTML to Markdown: Basic Formats', () => {
test('link', () => {
html2md2html(
`
<p>My favorite search engine is <a href="https://duckduckgo.com" target="_blank">Duck Duck Go</a>.</p>
<p>My favorite search engine is <a href="https://duckduckgo.com" target="_blank" rel="noopener nofollow noreferrer">Duck Duck Go</a>.</p>
`,
'My favorite search engine is [Duck Duck Go](https://duckduckgo.com).',
)
Expand All @@ -291,7 +291,7 @@ describe('HTML to Markdown: Basic Formats', () => {
html2md2html(
`
<p>
<a href="https://www.flickr.com/photos/beaurogers/31833779864/in/abc.png" target="_blank">
<a href="https://www.flickr.com/photos/beaurogers/31833779864/in/abc.png" target="_blank" rel="noopener nofollow noreferrer">
<img src="https://mdg.imgix.net/assets/images/shiprock.jpg" alt="An old rock in the desert" title="Shiprock, New Mexico by Beau Rogers" />
</a>
</p>
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('HTML to Markdown: Full Content', () => {
<p>Dolore labore laboris <strong>consequat nostrud</strong> Lorem irure excepteur incididunt adipisicing id.</p>
<p>Ullamco cillum <strong><em>esse anim dolore</em></strong> duis adipisicing.</p>
<p>Cillum incididunt <strong><em>nostrud sunt occaecat</em></strong> fugiat commodo quis in pariatur exercitation.</p>
<p>Veniam pariatur labore <a href="https://google.com" target="_blank">consectetur</a> laborum.</p>
<p>Veniam pariatur labore <a href="https://google.com" target="_blank" rel="noopener nofollow noreferrer">consectetur</a> laborum.</p>
<blockquote>
<p>Fugiat consectetur culpa anim enim sit nisi culpa consequat Lorem ipsum.<br><br>Qui proident non pariatur veniam est irure.</p>
</blockquote>
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/md2html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import remarkDirectiveRehype from 'remark-directive-rehype'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'
import rehypeExternalLinks from 'rehype-external-links'

import {
rehypeRewriteOptions,
Expand All @@ -28,6 +29,7 @@ const formatter = unified()
.use(rehypeRewrite, rehypeRewriteOptions)
.use(rehypeRaw)
.use(rehypeSanitize, rehypeSanitizeOptions)
.use(rehypeExternalLinks, { rel: ['noopener', 'nofollow', 'noreferrer'] })
.use(rehypeFormat)
.use(rehypeStringify, rehypeStringifyOptions)

Expand Down

0 comments on commit bbb444a

Please sign in to comment.