Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .remarkrc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import remarkPresetLintRecommended from 'remark-preset-lint-recommended';
import remarkNoInlineCodeFences from './src/plugins/remark-no-inline-code-fences.mjs';
import remarkNoHtmlLinks from './src/plugins/remark-no-html-links.mjs';
import remarkLintNoDeadUrls from 'remark-lint-no-dead-urls';
import remarkForceRootRelative from './src/plugins/remark-force-root-relative.mjs';
import remarkLintNoLiteralUrls from 'remark-lint-no-literal-urls';

export default {
Expand Down Expand Up @@ -33,5 +34,6 @@ export default {
},
]
: () => undefined,
remarkForceRootRelative,
],
};
2 changes: 2 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import remarkImageAttributes from './src/plugins/remark-image-attributes';
import { remarkMdxGlobalImports } from './src/plugins/remark-mdx-global-imports';
import remarkCodeRegion from './src/plugins/remark-code-region';
import { unified } from '@astrojs/markdown-remark';
import rehypeTargetBlank from './src/plugins/rehype-external-links';

export default defineConfig({
site: 'https://frcsoftware.org',
Expand All @@ -25,6 +26,7 @@ export default defineConfig({
remarkMdxGlobalImports,
remarkCodeRegion,
],
rehypePlugins: [rehypeTargetBlank],
remarkRehype: {
footnoteLabel: 'References',
footnoteLabelTagName: 'h4',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@astrojs/check": "^0.9.10",
"@eslint/js": "^10.0.1",
"@types/estree": "^1.0.9",
"@types/hast": "^3.0.5",
"@types/mdast": "^4.0.4",
"@types/node": "^26.4.0",
"@frcsoftware/vale": "3.19.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

33 changes: 33 additions & 0 deletions src/plugins/rehype-external-links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { visit } from 'unist-util-visit';
import type { Root } from 'hast';
export default function rehypeTargetBlank() {
return (tree: Root) => {
visit(tree, 'element', (node) => {
if (
node.tagName !== 'a' ||
typeof node.properties.href !== 'string' ||
!URL.canParse(node.properties.href) ||
// only match http/s
!/^https?:/.test(URL.parse(node.properties.href)!.protocol)
) {
// only run on valid external links
return;
}
const target = node.properties.target;
node.properties.target = target ? target : '_blank';
let rel = node.properties.rel;
if (!rel) {
rel = ['noreferrer', 'noopener'];
} else {
// just in case someone overrides the remark rule
if (!(rel.includes('opener') || rel.includes('noopener'))) {
rel.push('noopener');
}
if (!rel.includes('noreferrer')) {
rel.push('noreferrer');
}
}
node.properties.rel = rel;
});
};
}
29 changes: 29 additions & 0 deletions src/plugins/remark-force-root-relative.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { visit } from 'unist-util-visit';

export default function remarkForceRootRelative() {
/**
* @param {import("mdast").Root} tree
* @param {import("vfile").VFile} file
*/
return (tree, file) => {
visit(tree, ['link', 'image', 'definition'], (node) => {
if (
(node.type !== 'link' &&
node.type !== 'image' &&
node.type !== 'definition') ||
!URL.canParse(node.url)
) {
return;
}
// can't use the site property from astro.config.mjs, see https://github.com/withastro/starlight/pull/3572
const hostname = 'frcsoftware.org';
const url = new URL(node.url);
if (url.hostname === hostname) {
file.message(
'Use root-relative URL syntax for internal links.',
node,
).fatal = true;
}
});
};
}
Loading