import getRedirect from '@/redirects/lib/get-redirect' import { getPathWithoutLanguage, getPathWithoutVersion } from '@/frame/lib/path-utils' const liquidStartRex = /^{%-?\s*ifversion .+?\s*%}/ const liquidEndRex = /{%-?\s*endif\s*-?%}$/ // Return // // /foo/bar // // if the text input was // // {% ifversion ghes%}/foo/bar{%endif %} // // And if no liquid, just return as is. function stripLiquid(text: string): string { if (liquidStartRex.test(text) && liquidEndRex.test(text)) { return text.replace(liquidStartRex, '').replace(liquidEndRex, '').trim() } else if (text.includes('{')) { throw new Error(`Unsupported Liquid in frontmatter link list (${text})`) } return text } // Given a URI that does not start with a specific language, // return undefined if it can found as a known page. // Otherwise, return an object with information that is used to // print a useful test error message in the assertion. // Using 'any' type for redirectsContext parameter as it's a complex context object // with dynamic structure that would require extensive type definitions export function checkURL(uri: string, index: number, redirectsContext: any) { const url = `/en${stripLiquid(uri).split('#')[0]}` if (!(url in redirectsContext.pages)) { // Some are written without a version, but don't work with the // default version. let redirects = getRedirect(url, redirectsContext) // If it does indeed redirect to a different version, // strip that and compare again. if (redirects) { const withoutVersion = getPathWithoutVersion(redirects) if (withoutVersion === url) { // That means, it's actually fine return null } redirects = getPathWithoutLanguage(withoutVersion) } return { uri, index, redirects } } return null // Falsy value will be filtered out later }