Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: special links not working bug in markdown #1524

Merged
merged 6 commits into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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