userquin/feat-include-ids-and-anchor-check
userquin 2 years ago
parent b4115a7a56
commit 5e3f0ba1c1

@ -16,7 +16,7 @@ export async function checkIdsAndAnchorHrefs(siteConfig: SiteConfig) {
// TODO: export this function for testing purposes? // TODO: export this function for testing purposes?
async function* collectErrors(siteConfig: SiteConfig) { async function* collectErrors(siteConfig: SiteConfig) {
const outDir = siteConfig.outDir const cwd = siteConfig.outDir
const files = new Set( const files = new Set(
siteConfig.pages.map((page) => siteConfig.pages.map((page) =>
`${siteConfig.rewrites.map[page] || page}` `${siteConfig.rewrites.map[page] || page}`
@ -26,50 +26,48 @@ async function* collectErrors(siteConfig: SiteConfig) {
) )
// add public html files to the list: i.e. VP docs has public/pure.html // add public html files to the list: i.e. VP docs has public/pure.html
for await (const file of fg.stream('*.html', { for await (const file of fg.stream('*.html', {
cwd: outDir, cwd,
deep: 1 deep: 1
})) { })) {
files.add(file.toString().replace(/\\/g, '/')) files.add(file.toString().replace(/\\/g, '/'))
} }
const checkHtmlExt = siteConfig.site.cleanUrls === false const checkHtmlExt = siteConfig.site.cleanUrls === false
const stream = fg.stream('**/*.html', { for await (const file of fg.stream('**/*.html', {
cwd: siteConfig.outDir cwd
}) })) {
for await (const file of stream) {
const links = new Set<string>() const links = new Set<string>()
const ids = new Set<string>() const ids = new Set<string>()
const errors: string[] = [] const errors: string[] = []
const content = parse(
await fs.promises.readFile(resolve(outDir, file.toString()), 'utf8')
)
// collect ids and href anchors // collect ids and href anchors
walkSync(content, (node) => { walkSync(
if (node.type === ELEMENT_NODE) { parse(await fs.promises.readFile(resolve(cwd, file.toString()), 'utf8')),
const id = node.attributes.id (node) => {
if (id) { if (node.type === ELEMENT_NODE) {
if (ids.has(id)) errors.push(`duplicate id "${id}"`) const id = node.attributes.id
else ids.add(id) if (id) {
} if (ids.has(id)) errors.push(`duplicate id "${id}"`)
if (node.name.toLowerCase() === 'a') { else ids.add(id)
const href = node.attributes.href }
if ( if (node.name.toLowerCase() === 'a') {
!href || const href = node.attributes.href
href.startsWith('http://') || if (
href.startsWith('https://') !href ||
) href.startsWith('http://') ||
return href.startsWith('https://')
)
return
links.add(href) links.add(href)
}
} }
} }
}) )
// check for local hrefs and external links // check for local hrefs and external links
for (const href of links) { for (const href of links) {
// 1) check for local ids // 1) check for local ids
if (href[0] === '#') { if (href[0] === '#') {
const id = href.slice(1) if (!ids.has(href.slice(1)))
if (!ids.has(id)) errors.push(`missing local id for "${href}"`) errors.push(`missing local id for "${href}"`)
continue continue
} }
// 2) check for external links // 2) check for external links

Loading…
Cancel
Save