Skip to content

Commit

Permalink
Handle Pagefind Intermediate Files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jieiku committed Aug 9, 2024
1 parent d5acabc commit 63e4ecd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ webmanifest = "manifest.min.json" # Required for PWAs
###############################################################################

pwa = true # true to load the service worker
pwa_VER = '3.12.0' # Service Worker cache version. (increment if you need to force a new cache)
pwa_VER = '3.12.1' # Service Worker cache version. (increment if you need to force a new cache)

### 3600=1hour, 28800=8hours, 86400=1day, 604800=1week, 1209600=2weeks
pwa_NORM_TTL = 0 # 86400 is reasonable. html, json, xml, anything else undefined
Expand Down
59 changes: 49 additions & 10 deletions package_abridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ const pwa_BASE_CACHE_FILES = data.extra.pwa_BASE_CACHE_FILES;
// npm run abridge -- "--base-url https://abridge.pages.dev"
var args = process.argv[2] ? ' '+process.argv[2] : '';

// check if abridge is used directly or as a theme.
bpath = '';
if (fs.existsSync('./themes')) {
bpath = 'themes/abridge/';
}

async function execWrapper(cmd) {
const { stdout, stderr } = await execPromise(cmd);
if (stdout) {
Expand Down Expand Up @@ -73,12 +79,6 @@ async function abridge() {
if (e.code != 'EEXIST') throw e;
}

// check if abridge is used directly or as a theme.
bpath = '';
if (fs.existsSync('./themes')) {
bpath = 'themes/abridge/';
}

base_url = data.base_url;
if (base_url.slice(-1) == "/") {
base_url = base_url.slice(0, -1);
Expand All @@ -89,8 +89,20 @@ async function abridge() {
// Has to happen at start otherwise, it happens too late asyncronously.
const createIndex = require('./static/js/pagefind.index.cjs'); // run the pagefind index.js script
await createIndex(); // makes program wait for pagefind build execution
} else {
_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/);
_rmRecursive(path.join(bpath, "static/js/index"));
_rmRecursive(path.join(bpath, "static/js/fragment"));
}

// cleanup
_rmRegex(path.join(bpath, "static/js/"),/^pagefind\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.js$/);//pagefind temporary intermediate files
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-.*\.css$/);//pagefind temporary intermediate files


if (pwa) {// Update pwa settings, file list, and hashes.
if (typeof pwa_VER !== 'undefined' && typeof pwa_NORM_TTL !== 'undefined' && typeof pwa_LONG_TTL !== 'undefined' && typeof pwa_TTL_NORM !== 'undefined' && typeof pwa_TTL_LONG !== 'undefined' && typeof pwa_TTL_EXEMPT !== 'undefined') {
// update from abridge theme.
Expand Down Expand Up @@ -196,6 +208,9 @@ async function abridge() {
abridge_bundle = bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa);
minify(abridge_bundle,'static/js/abridge.min.js');

// cleanup
_rmRegex(path.join(bpath, "static/js/"),/^pagefind_search\.js$/);//pagefind intermediate file that is now in bundle.

console.log('Zola Build to generate new integrity hashes for the previously minified files:');
await execWrapper('zola build'+args);
}
Expand All @@ -204,8 +219,6 @@ async function _headersWASM() {
// running WASM in the browser requires wasm-unsafe-eval if using Content-Security-Policy:
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_webassembly_execution
// This function adds wasm-unsafe-eval to the pagefind and tinysearch demos without adding it to the elasticlunr demo.
// I keep hoping they will add something like: wasm-src 'self' but as far as I know nothing like that exists.

const { replaceInFileSync } = await import('replace-in-file');
if (search_library === 'pagefind') {
replaceInFileSync({files: 'static/_headers', from: /script-src 'self'/g, to: "script-src 'wasm-unsafe-eval' 'self'"});
Expand All @@ -216,6 +229,26 @@ async function _headersWASM() {
}
}

function _rmRecursive(targetFiles) {
try {
fs.rmSync(targetFiles, { recursive: true });
} catch (error) {
if (error.code !== 'ENOENT') {// Ignore if does not exist, that is the desired result.
console.error("An error occurred:", error);
}
}
}

function _rmRegex(path,regex) {
try {
fs.readdirSync(path).filter(f => regex.test(f)).forEach(f => fs.unlinkSync(path + f));
} catch (error) {
if (error.code !== 'ENOENT') {// Ignore if does not exist, that is the desired result.
console.error("An error occurred:", error);
}
}
}

function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search_library,index_format,uglyurls,pwa) {
minify_files = [];

Expand Down Expand Up @@ -244,7 +277,7 @@ function bundle(bpath,js_prestyle,js_switcher,js_email_encode,js_copycode,search
minify_files.push(bpath+'static/js/elasticlunr.min.js');
minify_files.push(bpath+'static/js/search.js');
} else if (search_library === 'pagefind') {
minify_files.push(bpath+'static/js/pagefind-search.js');
minify_files.push(bpath+'static/js/pagefind_search.js');
} else if (search_library === 'tinysearch') {
minify_files.push(bpath+'static/js/tinysearch.js');
}
Expand Down Expand Up @@ -295,7 +328,13 @@ if (args === ' offline') {
searchChange('offline');
} else if (args === ' elasticlunrjava') {
searchChange('elasticlunrjava');
} else if (args === ' elasticlunr') {
} else if (args === ' elasticlunr') {// Zola default search_library
// I should not need to do cleanup here as well... but this is what works for now to keep the repo clean.
_rmRegex(path.join(bpath, "static/js/"),/^wasm.*pagefind$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind.*pf_meta$/);
_rmRegex(path.join(bpath, "static/js/"),/^pagefind-entry\.json$/);
_rmRecursive(path.join(bpath, "static/js/index"));
_rmRecursive(path.join(bpath, "static/js/fragment"));
searchChange('elasticlunr');
} else if (args === ' pagefind') {
searchChange('pagefind');
Expand Down
2 changes: 1 addition & 1 deletion static/js/pagefind.index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function createIndex() {

// now insert the CJS into the anonymous function within pagefind.search.js
const pagefind_searchPath = path.join(__dirname, "pagefind.search.js");//file to insert into
const search_pagefindPath = path.join(__dirname, "pagefind-search.js");//output
const search_pagefindPath = path.join(__dirname, "pagefind_search.js");//output
let pagefind_searchContent = fs.readFileSync(pagefind_searchPath, "utf8");
// Now insert into pagefind.search.js at this location: //insertHere
pagefind_searchContent = pagefind_searchContent.replace(/\/\/insertHere/g, pagefindContent);
Expand Down

0 comments on commit 63e4ecd

Please sign in to comment.