From 895dbba3fe5e39dde237d173bcd00dbe44f235df Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 4 Apr 2024 19:49:46 +0200 Subject: Purge CSS on a per-template basis --- src/templates.ts | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/templates.ts b/src/templates.ts index b5043c0..4f8ab9f 100644 --- a/src/templates.ts +++ b/src/templates.ts @@ -2,16 +2,21 @@ import * as fs from "node:fs/promises"; import * as cheerio from "cheerio"; import { matter } from "toml-matter"; import { Marked } from "marked"; +import { PurgeCSS } from "purgecss"; import log from "loglevel"; import config from "./config"; import { getPost, readPosts, type Post } from "./posts"; +const css = await Bun.file("templates/style.css").text(); + const marked = new Marked(); marked.use({ gfm: true, }); +const purgeCSS = new PurgeCSS(); + function addMenu( parent: cheerio.Cheerio, child: cheerio.Cheerio, @@ -23,24 +28,34 @@ function addMenu( } export async function layout( - $: cheerio.CheerioAPI, + html: string, pageTitle: string, ): Promise { + const ccss = ( + await purgeCSS.purge({ + content: [ + { + raw: html, + extension: ".html", + }, + ], + css: [{ raw: css }], + }) + )[0].css; + const $ = cheerio.load(html); $("html").attr("lang", config.default_language); $("head > link[rel=alternate]").attr("title", config.title); addMenu($("nav"), $("nav a")); $(".title").text(config.title); $("title").text(pageTitle); $(".p-name").text(pageTitle); - $("head") - .children("style") - .text(await fs.readFile("templates/style.css", "utf-8")); + $("head").children("style").text(ccss); return $; } async function render404Page(): Promise { const $ = await layout( - cheerio.load(await fs.readFile("templates/404.html", "utf-8")), + await fs.readFile("templates/404.html", "utf-8"), "404 Not Found", ); return $.html(); @@ -49,7 +64,7 @@ async function render404Page(): Promise { async function renderHomepage(posts: Array): Promise { const file = matter(await fs.readFile("content/_index.md", "utf-8")); const $ = await layout( - cheerio.load(await fs.readFile("templates/homepage.html", "utf-8")), + await fs.readFile("templates/homepage.html", "utf-8"), config.title, ); @@ -84,7 +99,7 @@ async function renderHomepage(posts: Array): Promise { async function renderPost(file: Post, content: string) { const $ = await layout( - cheerio.load(await fs.readFile("templates/post.html", "utf-8")), + await fs.readFile("templates/post.html", "utf-8"), file.title, ); @@ -112,7 +127,7 @@ async function renderPost(file: Post, content: string) { async function renderListPage(tag: string, posts: Post[]) { const $ = await layout( - cheerio.load(await fs.readFile("templates/list.html", "utf-8")), + await fs.readFile("templates/list.html", "utf-8"), tag || config.title, ); const $feed = $(".h-feed"); @@ -139,7 +154,7 @@ async function renderListPage(tag: string, posts: Post[]) { async function renderTags(tags: string[]) { const $ = await layout( - cheerio.load(await fs.readFile("templates/tags.html", "utf-8")), + await fs.readFile("templates/tags.html", "utf-8"), config.title, ); const $tags = $(".tags"); @@ -192,7 +207,7 @@ async function renderFeedStyles() { xml: true, }, ); - $("style").text(await fs.readFile("templates/style.css", "utf-8")); + $("style").text(css); return $.xml(); } -- cgit 1.4.1