the home site for me: also iteration 3 or 4 of my site

feat: add new multi img format

dunkirk.sh 7da2b000 401cc3e4

verified
Changed files
+53 -11
scripts
+53 -11
scripts/preprocess.ts
···
}
function transformImages(content: string): string {
-
return content.replace(
-
/!\[([^\]]*)\]\(([^)]+)\)\{([^}]+)\}/g,
+
// Transform multiple images: !![alt1](url1)[alt2](url2){attrs}
+
content = content.replace(
+
/!!(\[([^\]]*)\]\(([^)]+)\))+(?:\{([^}]+)\})?/g,
+
(match) => {
+
// Extract all [alt](url) pairs
+
const pairs = [...match.matchAll(/\[([^\]]*)\]\(([^)]+)\)/g)];
+
const urls = pairs.map(p => p[2]).join(', ');
+
const alts = pairs.map(p => p[1]).join(', ');
+
+
// Extract attrs if present
+
const attrsMatch = match.match(/\{([^}]+)\}$/);
+
const attrs = attrsMatch ? attrsMatch[1] : '';
+
+
const params: string[] = [`id="${urls}"`];
+
+
if (alts.trim()) {
+
params.push(`alt="${alts}"`);
+
}
+
+
if (attrs) {
+
const classes = attrs.match(/\.([a-zA-Z0-9_-]+)/g)?.map(c => c.slice(1)) || [];
+
if (classes.length) {
+
params.push(`class="${classes.join(' ')}"`);
+
}
+
+
const keyValueMatches = attrs.matchAll(/([a-zA-Z]+)=["']?([^"'\s}]+)["']?/g);
+
for (const [, key, value] of keyValueMatches) {
+
if (key !== 'class') {
+
params.push(`${key}="${value.replace(/["']/g, '')}"`);
+
}
+
}
+
}
+
+
return `{{ imgs(${params.join(', ')}) }}`;
+
}
+
);
+
+
// Transform single images: ![alt](url){attrs}
+
content = content.replace(
+
/!\[([^\]]*)\]\(([^)]+)\)(?:\{([^}]+)\})?/g,
(match, alt, url, attrs) => {
const params: string[] = [`id="${url}"`];
···
params.push(`alt="${alt}"`);
}
-
const classes = attrs.match(/\.([a-zA-Z0-9_-]+)/g)?.map(c => c.slice(1)) || [];
-
if (classes.length) {
-
params.push(`class="${classes.join(' ')}"`);
-
}
-
-
const keyValueMatches = attrs.matchAll(/([a-zA-Z]+)=["']?([^"'\s}]+)["']?/g);
-
for (const [, key, value] of keyValueMatches) {
-
if (key !== 'class') {
-
params.push(`${key}="${value.replace(/["']/g, '')}"`);
+
if (attrs) {
+
const classes = attrs.match(/\.([a-zA-Z0-9_-]+)/g)?.map(c => c.slice(1)) || [];
+
if (classes.length) {
+
params.push(`class="${classes.join(' ')}"`);
+
}
+
+
const keyValueMatches = attrs.matchAll(/([a-zA-Z]+)=["']?([^"'\s}]+)["']?/g);
+
for (const [, key, value] of keyValueMatches) {
+
if (key !== 'class') {
+
params.push(`${key}="${value.replace(/["']/g, '')}"`);
+
}
}
}
return `{{ img(${params.join(', ')}) }}`;
}
);
+
+
return content;
}
function processFile(filePath: string): void {