qwik-site/libs/utils/siteMap.js

73 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2021-08-09 17:44:53 +02:00
import fs from 'fs/promises'
import path from 'path'
async function handlePath(dirName, siteMap) {
// Get all directories in the 'dirName'
const dirs = await fs.readdir(dirName)
// For each file in the directory: check if it is a
// file or a folder, if a folder recursively run this
// function in that folder.
for (const dirIndex in dirs) {
const file = dirs[dirIndex]
const fullPath = path.join(dirName, file)
const fileStats = await fs.stat(fullPath)
if (fileStats.isDirectory())
siteMap[fullPath.replace(/^[\s\S]+\/pages/, "")] = {
2021-08-10 20:51:11 +02:00
path: fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\/.+\//, "/"),
2021-08-09 17:44:53 +02:00
address: fullPath.replace(/^[\s\S]+\/pages/, ""),
type: "directory",
children: await handlePath(fullPath, {})
}
else
2021-08-10 20:51:11 +02:00
// Check if it already exists
siteMap[fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, "")] ?
siteMap[fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, "")] :
2021-08-09 17:44:53 +02:00
siteMap[fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, "")] = {
2021-08-10 20:51:11 +02:00
path: fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, "").replace(/\/.+\//, "/"),
2021-08-09 17:44:53 +02:00
address: fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, ""),
type: "file"
}
}
return siteMap
}
function renderSiteMap(siteMap, entrypoint) {
// Make a template for the sitemap and recursively run
// for all directories with children.
return `
<div class="sitemap-dir">
${(() => {
let result = []
for (const dirIndex in siteMap) {
const dir = siteMap[dirIndex]
if (!dir.address.startsWith(entrypoint))
continue
if (dir.type === "directory") {
2021-08-10 20:51:11 +02:00
result.push(`<a href="${dir.address}">${dir.path}`)
2021-08-09 17:44:53 +02:00
result.push(renderSiteMap(dir.children, entrypoint))
result.push(`</a>`)
} else {
2021-08-10 20:51:11 +02:00
result.push(`<a href="${dir.address}">${dir.path}</a>`)
2021-08-09 17:44:53 +02:00
}
}
return result.join("")
})()}
</div>
`
}
export async function generateSiteMap(Config) {
// Generate a site map and return it and a render-function.
const siteMap = await handlePath(`${Config.contentDir}/pages`, {})
return {
siteMap,
render: entrypoint => renderSiteMap(siteMap, entrypoint)
}
}