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 --- bun.lockb | Bin 16165 -> 32114 bytes package.json | 1 + src/templates.ts | 35 +++++++++++++++++++++++++---------- templates/post.html | 25 +++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/bun.lockb b/bun.lockb index bd34a0f..3fd980c 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 56b517a..84bf38c 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "highlight.js": "^11.8.0", "marked": "^9.0.3", "marked-highlight": "^2.0.6", + "purgecss": "^6.0.0", "toml-matter": "^1.0.0" }, "peerDependencies": { 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(); } diff --git a/templates/post.html b/templates/post.html index d08ea86..1262ee0 100644 --- a/templates/post.html +++ b/templates/post.html @@ -33,6 +33,31 @@ Enim lobortis scelerisque fermentum dui faucibus in ornare quam viverra. Eget egestas purus viverra accumsan in nisl nisi, scelerisque eu ultrices vitae, auctor eu augue ut lectus arcu, bibendum at. + + /bin/test + +
+            
+foo=bar
+            
+          
+ + + + + + + + + + + + + + + + +
OneTwoThree
123
    Tags: -- cgit 1.4.1