Skip to content

Commit c655966

Browse files
committed
Add Pagefind search
Dammit
1 parent 1100ce5 commit c655966

File tree

24 files changed

+1240
-32
lines changed

24 files changed

+1240
-32
lines changed

eleventy.config.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { load as yamlLoad } from 'js-yaml';
22
import pluginRss from '@11ty/eleventy-plugin-rss';
33
import { bundle as lightningcssBundle, browserslistToTargets, Features } from 'lightningcss';
4+
import * as esbuild from 'esbuild';
45
import Image from '@11ty/eleventy-img';
56
import { glob } from 'glob';
6-
import { cpSync } from 'node:fs';
7+
import { cpSync, rmSync, mkdirSync, writeFileSync } from 'node:fs';
78
import os from 'node:os';
9+
import path from 'node:path';
10+
import * as pagefind from 'pagefind';
811
import MarkdownIt from 'markdown-it';
912

1013
import packageJson from './package.json' with { type: 'json' };
@@ -141,6 +144,32 @@ export default (config) => {
141144
return code;
142145
});
143146

147+
// JavaScript
148+
149+
config.addTemplateFormats('js');
150+
151+
config.addExtension('js', {
152+
outputFileExtension: 'js',
153+
compile: async (content, path) => {
154+
if (path !== './src/scripts/index.js') {
155+
return;
156+
}
157+
158+
return async () => {
159+
let { outputFiles } = await esbuild.build({
160+
target: 'es2020',
161+
entryPoints: [path],
162+
minify: true,
163+
bundle: true,
164+
external: ['/pagefind/pagefind.js'],
165+
write: false,
166+
});
167+
168+
return outputFiles[0].text;
169+
};
170+
},
171+
});
172+
144173
// Covers
145174

146175
const imagesCache = '.cache/@11ty/_images';
@@ -187,8 +216,31 @@ export default (config) => {
187216
);
188217
});
189218

219+
// Search
220+
190221
config.on('eleventy.after', async () => {
191222
cpSync(imagesCache, 'dist', { recursive: true });
223+
224+
const { index } = await pagefind.createIndex();
225+
await index.addDirectory({ path: 'dist' });
226+
rmSync('dist/pagefind', { recursive: true, force: true });
227+
228+
const { files } = await index.getFiles();
229+
const ignoredPaths = [
230+
'pagefind-ui',
231+
'pagefind-modular-ui',
232+
'translations/',
233+
];
234+
235+
const filteredFiles = files.filter((file) => {
236+
return !ignoredPaths.some((pattern) => file.path.includes(pattern));
237+
});
238+
239+
filteredFiles.forEach((file) => {
240+
const outputPath = path.join('dist/pagefind', file.path);
241+
mkdirSync(path.dirname(outputPath), { recursive: true });
242+
writeFileSync(outputPath, file.content);
243+
});
192244
});
193245

194246
// Dates

netlify.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
default-src 'self';
2727
base-uri 'self';
2828
object-src 'none';
29-
script-src 'none';
29+
script-src 'self' 'wasm-unsafe-eval';
3030
style-src 'self';
3131
img-src 'self' data:;
3232
font-src 'self';
3333
connect-src 'self';
34+
worker-src 'self';
3435
form-action 'self';
3536
frame-ancestors 'self';
3637
upgrade-insecure-requests;

0 commit comments

Comments
 (0)