Skip to content

Commit

Permalink
fix: special links not working bug in markdown (#1524)
Browse files Browse the repository at this point in the history
* fix: absolute links and schema links lost bug in markdown

* refactor: update relative link condition

* refactor: update relative link condition for data url

* Update src/loaders/markdown/transformer/rehypeLink.ts

* Update src/loaders/markdown/transformer/rehypeLink.ts

* refactor: new way to handle special links
  • Loading branch information
PeachScript committed Mar 6, 2023
1 parent edd743e commit 7e250d8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/loaders/markdown/transformer/rehypeLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ export default function rehypeLink(
): Transformer<Root> {
return (tree) => {
visit<Root, 'element'>(tree, 'element', (node, i, parent) => {
if (node.tagName === 'a' && typeof node.properties?.href === 'string') {
if (
node.tagName === 'a' &&
typeof node.properties?.href === 'string' &&
// skip target specified link
!node.properties?.target &&
// skip download link
!node.properties?.download
) {
const href = node.properties.href;
const parsedUrl = url.parse(href);
const hostAbsPath = getHostForTabRouteFile(opts.fileAbsPath);

// handle internal link
if (parsedUrl.hostname) return SKIP;
// skip external or special links:
// - http://www.example.com or mailto:xxx@example.com or data:image/xxx
// - //www.example.com
if (parsedUrl.protocol || href.startsWith('//')) return SKIP;

if (/\.md$/i.test(parsedUrl.pathname!)) {
// handle markdown link
Expand All @@ -41,10 +50,7 @@ export default function rehypeLink(
parsedUrl.pathname = routes[key].absPath;
}
});
} else if (
/^\.?\.\//.test(parsedUrl.pathname!) ||
/^(\w+:)?\/\//.test(parsedUrl.pathname!)
) {
} else if (parsedUrl.pathname && /^[^/]+/.test(parsedUrl.pathname)) {
// handle relative link
// transform relative link to absolute link
// because react-router@6 and HTML href are different in processing relative link
Expand Down

0 comments on commit 7e250d8

Please sign in to comment.