blob: df714e933e4b23af333fb127dbc2b1a5ec01bb4b [file] [edit]
import fs from 'node:fs'
import path from 'node:path'
import mdx from '@astrojs/mdx'
import sitemap from '@astrojs/sitemap'
import type { AstroIntegration } from 'astro'
import { getConfig } from './config'
import { getDocsFsPath, getDocsPublicFsPath, getDocsStaticFsPath, validateVersionedDocsPaths } from './path'
// A list of static file paths that will be aliased to a different path.
const staticFileAliases = {
'/docs/[version]/assets/img/favicons/apple-touch-icon.png': '/apple-touch-icon.png',
'/docs/[version]/assets/img/favicons/favicon.ico': '/favicon.ico'
}
// A list of pages that will be excluded from the sitemap.
const sitemapExcludes = ['/404', '/docs', `/docs/${getConfig().docs_version}`]
export function bootstrap(): AstroIntegration[] {
const sitemapExcludedUrls = sitemapExcludes.map((url) => `${getConfig().baseURL}${url}/`)
return [
{
name: 'bootstrap-integration',
hooks: {
'astro:config:setup': ({ addWatchFile }) => {
// Reload the config when the integration is modified.
addWatchFile(path.join(getDocsFsPath(), 'src/libs/astro.ts'))
},
'astro:config:done': () => {
cleanPublicDirectory()
copyBootstrap()
copyStatic()
aliasStatic()
copyPagefindIndex()
},
'astro:build:done': ({ dir }) => {
validateVersionedDocsPaths(dir)
}
}
},
// https://github.com/withastro/astro/issues/6475
mdx() as AstroIntegration,
sitemap({
filter: (page) => sitemapFilter(page, sitemapExcludedUrls)
})
]
}
function cleanPublicDirectory() {
fs.rmSync(getDocsPublicFsPath(), { force: true, recursive: true })
}
// Copy the previously-generated Pagefind search index from `_site/pagefind/`
// into `site/public/pagefind/` so `astro dev` can serve it at `/pagefind/`.
// The index is regenerated by `npm run docs-build`; this step is a no-op if
// no build has been run yet, in which case dev simply has no search results.
function copyPagefindIndex() {
const source = path.join(process.cwd(), '_site', 'pagefind')
if (!fs.existsSync(source)) {
return
}
const destination = path.join(getDocsPublicFsPath(), 'pagefind')
fs.mkdirSync(destination, { recursive: true })
fs.cpSync(source, destination, { recursive: true })
}
// Copy the `dist` folder from the root of the repo containing the latest version of Bootstrap to make it available from
// the `/docs/${docs_version}/dist` URL.
function copyBootstrap() {
const source = path.join(process.cwd(), 'dist')
const destination = path.join(getDocsPublicFsPath(), 'docs', getConfig().docs_version, 'dist')
fs.mkdirSync(destination, { recursive: true })
fs.cpSync(source, destination, { recursive: true })
}
// Copy the content as-is of the `static` folder to make it available from the `/` URL.
// A folder named `[version]` will automatically be renamed to the current version of the docs extracted from the
// `config.yml` file.
function copyStatic() {
const source = getDocsStaticFsPath()
const destination = path.join(getDocsPublicFsPath())
copyStaticRecursively(source, destination)
}
// Alias (copy) some static files to different paths.
function aliasStatic() {
const source = getDocsStaticFsPath()
const destination = path.join(getDocsPublicFsPath())
for (const [aliasSource, aliasDestination] of Object.entries(staticFileAliases)) {
fs.cpSync(path.join(source, aliasSource), path.join(destination, aliasDestination))
}
}
// See `copyStatic()` for more details.
function copyStaticRecursively(source: string, destination: string) {
const entries = fs.readdirSync(source, { withFileTypes: true })
for (const entry of entries) {
if (entry.isFile()) {
fs.cpSync(path.join(source, entry.name), replacePathVersionPlaceholder(path.join(destination, entry.name)))
} else if (entry.isDirectory()) {
fs.mkdirSync(replacePathVersionPlaceholder(path.join(destination, entry.name)), { recursive: true })
copyStaticRecursively(path.join(source, entry.name), path.join(destination, entry.name))
}
}
}
function replacePathVersionPlaceholder(name: string) {
return name.replace('[version]', getConfig().docs_version)
}
function sitemapFilter(page: string, excludedUrls: string[]) {
if (excludedUrls.includes(page)) {
return false
}
return true
}