1
0
mirror of synced 2025-12-22 11:26:57 -05:00

AUTOTITLE on anchor links should error (#35823)

This commit is contained in:
Peter Bengtsson
2023-03-27 15:42:26 -04:00
committed by GitHub
parent fa8551e29a
commit ec85c1d228
6 changed files with 53 additions and 4 deletions

View File

@@ -11,6 +11,8 @@ import removeFPTFromPath from '../../remove-fpt-from-path.js'
import readJsonFile from '../../read-json-file.js'
import findPage from '../../find-page.js'
const isProd = process.env.NODE_ENV === 'production'
const supportedPlans = new Set(Object.values(allVersions).map((v) => v.plan))
const externalRedirects = readJsonFile('./lib/redirects/external-sites.json')
@@ -38,13 +40,20 @@ function setDataAttributesOnCurrentPath(path) {
}
// Matches any <a> tags with an href that starts with `/`
const matcher = (node) =>
const matcherInternalLinks = (node) =>
node.type === 'element' &&
node.tagName === 'a' &&
node.properties &&
node.properties.href &&
node.properties.href.startsWith('/')
const matcherAnchorLinks = (node) =>
node.type === 'element' &&
node.tagName === 'a' &&
node.properties &&
node.properties.href &&
node.properties.href.startsWith('#')
// Content authors write links like `/some/article/path`, but they need to be
// rewritten on the fly to match the current language and page version
export default function rewriteLocalLinks(context) {
@@ -55,7 +64,7 @@ export default function rewriteLocalLinks(context) {
return async (tree) => {
const promises = []
visit(tree, matcher, (node) => {
visit(tree, matcherInternalLinks, (node) => {
const newHref = getNewHref(node, currentLanguage, currentVersion)
if (newHref) {
node.properties.href = newHref
@@ -83,6 +92,21 @@ export default function rewriteLocalLinks(context) {
}
})
if (!isProd) {
// This runs when doing local preview, link checker tests, or
// running a script like `update-internal-links.js`.
visit(tree, matcherAnchorLinks, (node) => {
for (const child of node.children || []) {
if (child.value && AUTOTITLE.test(child.value)) {
throw new Error(
`Found anchor link with text AUTOTITLE ('${node.properties.href}'). ` +
'Update the anchor link with text that is not AUTOTITLE.'
)
}
}
})
}
if (promises.length) {
await Promise.all(promises)
}