Skip to content

Commit efe42b7

Browse files
authored
Add caching support for illustrations in the about page (#919)
1 parent 57588a9 commit efe42b7

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

convert/convertAbout.ts

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

6+
const base = process.env.BUILD_BASE_PATH || '';
7+
58
export interface AboutTaskOutput extends TaskOutput {
69
taskName: 'ConvertAbout';
710
}
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+
834
/**
9-
* Copies about.partial.html to static folder
35+
* Copies about.partial.html to static folder with image tag processing
1036
*/
1137
export function convertAbout(dataDir: string, verbose: number) {
1238
const srcFile = path.join(dataDir, 'about.partial.html');
1339
const dstFile = path.join('src/gen-assets', 'about.partial.html');
14-
copyFile(srcFile, dstFile, function (err: any) {
15-
if (err) throw err;
16-
if (verbose) console.log(`copied ${srcFile} to ${dstFile}`);
17-
});
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}`);
1851
}
1952
export class ConvertAbout extends Task {
2053
public triggerFiles: string[] = ['about.partial.html'];

0 commit comments

Comments
 (0)