From 255e5f7835423d4f41fef35df192288be32d6340 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Mon, 9 Aug 2021 16:55:53 +0200 Subject: [PATCH] Implemented sitemaps. --- assets/css/sitemap.css | 22 ++++++++++++ content/pages/sitemap.njk | 16 +++++++++ index.js | 28 ++++------------ libs/nunjucksConfig.js | 31 +++++++++++++++++ libs/utils/siteMap.js | 70 +++++++++++++++++++++++++++++++++++++++ libs/utils/utils.js | 14 ++++++++ 6 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 assets/css/sitemap.css create mode 100644 content/pages/sitemap.njk create mode 100644 libs/nunjucksConfig.js create mode 100644 libs/utils/siteMap.js create mode 100644 libs/utils/utils.js diff --git a/assets/css/sitemap.css b/assets/css/sitemap.css new file mode 100644 index 0000000..0ef2bd5 --- /dev/null +++ b/assets/css/sitemap.css @@ -0,0 +1,22 @@ +body { + display: flex; + flex-direction: column; +} + +.sitemap-container { + display: grid; + place-items: center; + + width: fit-content; + + margin: 2rem auto; + padding: 1rem 4rem; + background-color: var(--grey3); + border-radius: .5rem; + box-shadow: 0 1rem 2rem var(--grey1); +} + +.sitemap-dir > .sitemap-dir { + margin: 0 auto; + padding: 0 0 .5rem 1rem; +} \ No newline at end of file diff --git a/content/pages/sitemap.njk b/content/pages/sitemap.njk new file mode 100644 index 0000000..9bdf417 --- /dev/null +++ b/content/pages/sitemap.njk @@ -0,0 +1,16 @@ + + + + {% include "templates/defaultTags.njk" %} + + Site Map + + +
+ Sitemap - "/" +
+
+ {{ siteMap() | safe }} +
+ + \ No newline at end of file diff --git a/index.js b/index.js index f928620..b49297a 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,10 @@ import fs from 'fs' import express from 'express' import njk from 'nunjucks' -import njkMarkdown from 'nunjucks-markdown' -import marked from 'marked' import { requestHandler } from './libs/requestHandler.js' -import { markedRenderer } from './libs/markedRenderer.js' +import { utils } from './libs/utils/utils.js' +import { nunjacksConfig } from './libs/nunjucksConfig.js' // Load in config @@ -23,26 +22,11 @@ const Server = express() // Configure the assets directory Server.use('/assets', express.static(Config.assetsDir)) -// Configure the nunjucks environment (HTML templating) -const njkEnv = njk.configure( - Config.contentDir, - { - autoescape: true, // Automatically escape HTML aka a '<' will become '<' - watch: true, // Automatically reload all templates if they change on disk. - trimBlocks: true, // Remove trailing newlines from tags - lstripBlocks: true, // Automatically remove leading whitespace from tags - express: Server // Use the express server. (Currently obsolete, I think.) - } -) +// Configure nunjacks +nunjacksConfig(njk, Server, Config) -// Configure marked (markdown parser) -marked.use({ - gfm: true, // Use GitHub formatting? - renderer: markedRenderer // Use my own custom rendering for som tags -}) - -// Let nunjucks use markdown. -njkMarkdown.register(njkEnv, marked) +// Generate utils +utils.generate() diff --git a/libs/nunjucksConfig.js b/libs/nunjucksConfig.js new file mode 100644 index 0000000..d313d63 --- /dev/null +++ b/libs/nunjucksConfig.js @@ -0,0 +1,31 @@ +import njkMarkdown from 'nunjucks-markdown' +import marked from 'marked' + +import { markedRenderer } from './markedRenderer.js' +import { utils } from './utils/utils.js' + + +export function nunjacksConfig(njk, express, Config) { + // Configure the nunjucks environment (HTML templating) + const njkEnv = njk.configure( + Config.contentDir, + { + autoescape: true, // Automatically escape HTML aka a '<' will become '<' + watch: true, // Automatically reload all templates if they change on disk. + trimBlocks: true, // Remove trailing newlines from tags + lstripBlocks: true, // Automatically remove leading whitespace from tags + express: express // Use the express server. (Currently obsolete, I think.) + } + ) + + njkEnv.addGlobal('siteMap', entrypoint => utils.values.siteMap.render(entrypoint ? entrypoint : "/")) + + // Configure marked (markdown parser) + marked.use({ + gfm: true, // Use GitHub formatting? + renderer: markedRenderer // Use my own custom rendering for som tags + }) + + // Let nunjucks use markdown. + njkMarkdown.register(njkEnv, marked) +} \ No newline at end of file diff --git a/libs/utils/siteMap.js b/libs/utils/siteMap.js new file mode 100644 index 0000000..270a80e --- /dev/null +++ b/libs/utils/siteMap.js @@ -0,0 +1,70 @@ +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/, "")] = { + path: fullPath, + address: fullPath.replace(/^[\s\S]+\/pages/, ""), + type: "directory", + children: await handlePath(fullPath, {}) + } + else + siteMap[fullPath.replace(/^[\s\S]+\/pages/, "").replace(/\.[a-z]+$/, "")] = { + path: fullPath, + 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 ` +
+ ${(() => { + let result = [] + + for (const dirIndex in siteMap) { + const dir = siteMap[dirIndex] + if (!dir.address.startsWith(entrypoint)) + continue + + if (dir.type === "directory") { + result.push(`${dir.address}`) + result.push(renderSiteMap(dir.children, entrypoint)) + result.push(``) + } else { + result.push(`${dir.address}`) + } + } + + return result.join("") + })()} +
+ ` +} + +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) + } +} \ No newline at end of file diff --git a/libs/utils/utils.js b/libs/utils/utils.js new file mode 100644 index 0000000..1f0dfc9 --- /dev/null +++ b/libs/utils/utils.js @@ -0,0 +1,14 @@ +import fs from 'fs' +import { generateSiteMap } from './siteMap.js' + +const ConfigFile = fs.readFileSync('./config.json') +const Config = JSON.parse(ConfigFile) + +// Generate some utilities +export let utils = { + generate: async () => + utils.values = { + siteMap: await generateSiteMap(Config) + }, + values: {} +} \ No newline at end of file