import { promises as fs } from 'fs'
import fg from 'fast-glob'
import { font, preconnectHomeLinks, preconnectLinks } from '../docs-data'
const preconnect = `
${preconnectLinks.map(l => ``).join('\n')}
${preconnectLinks.map(l => ``).join('\n')}
`
const preconnectHome = `
${preconnectHomeLinks.map(l => ``).join('\n')}
${preconnectHomeLinks.map(l => ``).join('\n')}
`
export const optimizePages = async (pwa: boolean) => {
const names = await fg('./.vitepress/dist/**/*.html', { onlyFiles: true })
await Promise.all(names.map(async (i) => {
let html = await fs.readFile(i, 'utf-8')
let prefetchImg = '\n\t'
let usePreconnect = preconnect
if (i.endsWith('/dist/index.html')) {
usePreconnect = preconnectHome
prefetchImg = `
${prefetchImg}
\t
\t
`
}
// we need the font on development, so the font entry is added in vitepress head
html = html.replace(``, '')
html = html.replace(
//g,
`
${usePreconnect}
`).trim()
if (pwa) {
html = html.replace(
'',
`
\t${prefetchImg}
\t\n`,
)
}
else {
html = html.replace(
'',
`
${prefetchImg}
`,
)
}
// TODO: dark/light theme, don't remove yet
// html = html.replace(
// '',
// '\t\n',
// )
html = html.replace(
/aria-hidden="true"/gi,
'tabindex="-1" aria-hidden="true"',
).replace(
/![]()