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,24 +26,22 @@ 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(
parse(await fs.promises.readFile(resolve(cwd, file.toString()), 'utf8')),
(node) => {
if (node.type === ELEMENT_NODE) { if (node.type === ELEMENT_NODE) {
const id = node.attributes.id const id = node.attributes.id
if (id) { if (id) {
@ -62,14 +60,14 @@ async function* collectErrors(siteConfig: SiteConfig) {
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