diff --git a/scripts/extract-tools-desc.mjs b/scripts/extract-tools-desc.mjs index 45ef29b9..c4e6f4a2 100644 --- a/scripts/extract-tools-desc.mjs +++ b/scripts/extract-tools-desc.mjs @@ -11,29 +11,37 @@ if (fs.existsSync(localesFile)) { localesData = yaml.parse(fs.readFileSync(localesFile, 'utf-8')) || {}; } +// Single-quoted JS string that may contain escaped quotes. `[^'\\]|\\.` cannot overlap, so it cannot backtrack exponentially. +const quotedValue = (key) => new RegExp(`\\b${key}: '((?:[^'\\\\]|\\\\.)*)'`); +// Decode JS string escapes (\n, \x27, \u00e9, \u{1F600}, \') the way the JS parser would +const JS_ESCAPES = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', v: '\v', 0: '\0' }; +const unescapeValue = (value) => + value.replace(/\\(?:u\{([\da-fA-F]+)\}|u([\da-fA-F]{4})|x([\da-fA-F]{2})|(.))/g, (_, codePoint, u4, x2, char) => + char === undefined ? String.fromCodePoint(Number.parseInt(codePoint ?? u4 ?? x2, 16)) : (JS_ESCAPES[char] ?? char)); + function processFile(filePath) { const parentDir = path.basename(path.dirname(filePath)); // Get parent directory name const originalContent = fs.readFileSync(filePath, 'utf-8'); // Extract existing name and description values - const nameMatch = originalContent.match(/name: '(.*?)'/); - const descriptionMatch = originalContent.match(/description: '(.*?)'/); - const externAccessDescriptionMatch = originalContent.match(/externAccessDescription: '(.*?)'/); + const nameMatch = originalContent.match(quotedValue('name')); + const descriptionMatch = originalContent.match(quotedValue('description')); + const externAccessDescriptionMatch = originalContent.match(quotedValue('externAccessDescription')); if (!nameMatch || !descriptionMatch) { console.warn(`Skipping ${filePath}: Unable to extract name or description.`); return; } - const nameValue = nameMatch[1]; - const descriptionValue = descriptionMatch[1]; - const externAccessDescriptionValue = externAccessDescriptionMatch ? externAccessDescriptionMatch[1] : null; + const nameValue = unescapeValue(nameMatch[1]); + const descriptionValue = unescapeValue(descriptionMatch[1]); + const externAccessDescriptionValue = externAccessDescriptionMatch ? unescapeValue(externAccessDescriptionMatch[1]) : null; // Modify name and description to use the translate function const transformedContent = originalContent - .replace(/name: '((?:[^']+|(?<=\\)')+)'/, `name: t('tools.${parentDir}.title')`) - .replace(/description: '((?:[^']+|(?<=\\)')+)'/, `description: t('tools.${parentDir}.description')`) - .replace(/externAccessDescription: '((?:[^']+|(?<=\\)')+)'/, `externAccessDescription: t('tools.${parentDir}.externalAccess')`); + .replace(quotedValue('name'), `name: t('tools.${parentDir}.title')`) + .replace(quotedValue('description'), `description: t('tools.${parentDir}.description')`) + .replace(quotedValue('externAccessDescription'), `externAccessDescription: t('tools.${parentDir}.externalAccess')`); fs.writeFileSync(filePath, `import { translate as t } from '@/plugins/i18n.plugin';\n${transformedContent}`, 'utf-8'); console.log(`Transformed: ${filePath}`);