mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
const CONTENT_DIR = 'content';
|
|
|
|
function walk(dir, callback) {
|
|
fs.readdirSync(dir).forEach((file) => {
|
|
const filepath = path.join(dir, file);
|
|
const stats = fs.statSync(filepath);
|
|
if (stats.isDirectory()) {
|
|
walk(filepath, callback);
|
|
} else if (stats.isFile() && (filepath.endsWith('.mdx') || filepath.endsWith('.md'))) {
|
|
callback(filepath);
|
|
}
|
|
});
|
|
}
|
|
|
|
walk(CONTENT_DIR, (filepath) => {
|
|
let content = fs.readFileSync(filepath, 'utf8');
|
|
let changed = false;
|
|
|
|
const segments = filepath.split(path.sep);
|
|
const depth = segments.length - 1;
|
|
const correctPrefix = '../'.repeat(depth) + 'public/';
|
|
|
|
// Match any sequence of ../ followed by public/
|
|
const relativeLinkRegex = /(\.\.\/)+public\//g;
|
|
|
|
if (relativeLinkRegex.test(content)) {
|
|
const newContent = content.replace(relativeLinkRegex, correctPrefix);
|
|
if (newContent !== content) {
|
|
content = newContent;
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
// Also catch any remaining absolute ones if they exist
|
|
// Regex to match Markdown images: 
|
|
// and HTML images: src="/docs/path..."
|
|
// Capturing group 1: '(/docs/(?:img|v\d+\.\d+)/.*?)(\)|["'])
|
|
const pattern = /(!\[.*?\]\(|src=["'])\/docs\/((?:img|v\d+\.\d+)\/.*?)(\)|["'])/g;
|
|
|
|
if (pattern.test(content)) {
|
|
const newContent = content.replace(pattern, (match, prefix, pathPart, suffix) => {
|
|
// prefix e.g. '' or '"'
|
|
return `${prefix}${correctPrefix}${pathPart}${suffix}`;
|
|
});
|
|
|
|
if (newContent !== content) {
|
|
content = newContent;
|
|
changed = true;
|
|
}
|
|
}
|
|
|
|
if (changed) {
|
|
fs.writeFileSync(filepath, content);
|
|
console.log(`Updated: ${filepath}`);
|
|
}
|
|
});
|