Skip to content

Commit 9e88ec8

Browse files
authored
Handle about images without creating duplicate files (#922)
* Revert "Add caching support for illustrations in the about page (#919)" This reverts commit efe42b7. * Rewrite about img src at load * Replace only src attribute
1 parent 5420128 commit 9e88ec8

3 files changed

Lines changed: 29 additions & 44 deletions

File tree

convert/convertAbout.ts

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
1-
import * as fs from 'fs';
1+
import { copyFile } from 'fs';
22
import path from 'path';
3-
import { createHashedFile } from './fileUtils';
43
import { Task, TaskOutput } from './Task';
54

6-
const base = process.env.BUILD_BASE_PATH || '';
7-
85
export interface AboutTaskOutput extends TaskOutput {
96
taskName: 'ConvertAbout';
107
}
11-
12-
// Function to check if an image is missing
13-
function isImageMissing(imageSource: string, dataDir: string): boolean {
14-
return !fs.existsSync(path.join(dataDir, 'illustrations', imageSource));
15-
}
16-
17-
function updateImgTags(text: string, dataDir: string, verbose: number): string {
18-
return text.replace(
19-
/<img\b[^>]*\bsrc=["']([^"']*\/)?([^"']*)["'][^>]*>/gi,
20-
(_match, _path, fileName) => {
21-
// If the image is missing in the "illustrations" folder, filter out the entire tag
22-
if (isImageMissing(fileName, dataDir)) {
23-
return '';
24-
} else {
25-
fs.mkdirSync(path.join('static', 'illustrations'), { recursive: true });
26-
const imagePath = createHashedFile(dataDir, `illustrations/${fileName}`, verbose);
27-
28-
return `<img src="${base}/${imagePath}">`;
29-
}
30-
}
31-
);
32-
}
33-
348
/**
35-
* Copies about.partial.html to static folder with image tag processing
9+
* Copies about.partial.html to static folder
3610
*/
3711
export function convertAbout(dataDir: string, verbose: number) {
3812
const srcFile = path.join(dataDir, 'about.partial.html');
3913
const dstFile = path.join('src/gen-assets', 'about.partial.html');
40-
41-
// Read the file content
42-
const content = fs.readFileSync(srcFile, 'utf8');
43-
44-
// Apply the updateImgTags filter
45-
const processedContent = updateImgTags(content, dataDir, verbose);
46-
47-
// Write the processed content to the destination
48-
fs.writeFileSync(dstFile, processedContent, 'utf8');
49-
50-
if (verbose) console.log(`copied and processed ${srcFile} to ${dstFile}`);
14+
copyFile(srcFile, dstFile, function (err: any) {
15+
if (err) throw err;
16+
if (verbose) console.log(`copied ${srcFile} to ${dstFile}`);
17+
});
5118
}
5219
export class ConvertAbout extends Task {
5320
public triggerFiles: string[] = ['about.partial.html'];

convert/convertBooks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function updateImgTags(
239239
): string {
240240
return text.replace(
241241
/<img\b[^>]*\bsrc=["']([^"']*\/)?([^"']*)["'][^>]*>/gi,
242-
(_match, _path, fileName) => {
242+
(match, _path, fileName) => {
243243
// If the image is missing in the "illustrations" folder, filter out the entire tag
244244
if (isImageMissing(fileName)) {
245245
return '';
@@ -250,7 +250,7 @@ function updateImgTags(
250250
context.verbose
251251
);
252252

253-
return `<img src="${base}/${imagePath}">`;
253+
return match.replace(/src=["'][^"']*["']/, `src="${base}/${imagePath}"`);
254254
}
255255
}
256256
);

src/routes/about/+page.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
import { base } from '$app/paths';
1+
const illustrations = import.meta.glob('./*', {
2+
eager: true,
3+
import: 'default',
4+
base: '/src/gen-assets/illustrations'
5+
});
6+
7+
function updateImgTags(/** @type {string} */ text) {
8+
return text.replace(
9+
/<img\b[^>]*\bsrc=["']([^"']*\/)?([^"']*)["'][^>]*>/gi,
10+
(match, _path, fileName) => {
11+
const img = illustrations[`./${fileName}`];
12+
if (!img) {
13+
console.error(`Error loading about illustration: could not find image ${fileName}`);
14+
return match; // keep original tag instead of dropping it
15+
}
16+
return match.replace(/src=["'][^"']*["']/, `src="${img}"`);
17+
}
18+
);
19+
}
220

321
/** @type {import('./$types').PageLoad} */
4-
export async function load({ fetch }) {
22+
export async function load() {
523
const url = await import('$assets/about.partial.html?raw');
6-
return { partial: url.default };
24+
return { partial: updateImgTags(url.default ?? '') };
725
}

0 commit comments

Comments
 (0)