import { fetchJson } from './fetchJson.ts'; import type { Translator } from './translator.ts'; let census_groups: Record = {}; let census_comparisons: Record = {}; const replaceQuoteEntity = (string: string): string => string.replace(/"/g, '"'); const mainPlusDetails = (dict: Record, wide: boolean) => (_: string, keys: string, content: string) => { let selectedDict: Record = {}; if (keys === undefined) { selectedDict = dict; } else { for (const key of keys.substring(1).split(',')) { selectedDict[key] = dict[key]; } } return `
${content.replace(/%group%/g, 'general').replace(/
') .replace(/

{graph=([^}]+)}<\/p>/g, '') .replace(/

{set_census_groups=(.+?)}<\/p>/gms, (_, value) => { census_groups = JSON.parse(replaceQuoteEntity(value)); return ''; }) .replace(/

{set_census_comparisons=(.+?)}<\/p>/gms, (_, value) => { census_comparisons = JSON.parse(replaceQuoteEntity(value)); return ''; }) .replace(/

{census_groups(:.+?)?}<\/p>(.+?)

{\/census_groups}<\/p>/gms, mainPlusDetails(census_groups, false)) .replace(/

{census_comparisons(:.+?)?}<\/p>(.+?)

{\/census_comparisons}<\/p>/gms, mainPlusDetails(census_comparisons, true)) .replace(/

{wide_table}<\/p>/g, '
') .replace(/

{\/wide_table}<\/p>/g, '

') .replace(/

{gallery={(.*?)}}<\/p>/gms, generateGallery) .replace(/(?]*?)!!(?![\w!])/g, (_: string, spoilerString: string): string => { return `${spoilerString}`; }) }`; content = await replaceAsync(content, /{json=([^=}]+)=([^=}]+)}/g, async (_, filename, key) => { try { return await fetchJson(translator.config.locale, filename, key); } catch (error) { return `${error}`; } }); content = content.replace(/

{table_of_contents}<\/p>/g, generateToC(content, translator)); content = content.replace(/{optional}(.+?){\/optional}/gms, (_, content) => { if (content.includes('badge bg-danger')) { return ''; } return content; }); const titleMatch = content.match(']*>(.+?)

'); const title = titleMatch ? replaceQuoteEntity(titleMatch[1]) : null; const imgMatch = content.match(']*>'); const img = imgMatch ? imgMatch[1] : null; let intro: string[] = []; for (const introMatch of content.matchAll(/]*>(.+?)<\/p>/gms)) { const p = introMatch[1].replace(/(<([^>]+)>)/ig, '').replace(/\s+/g, ' '); intro = [...intro, ...p.split(' ')]; } return { title, img, intro: intro.length ? intro.slice(0, 24).join(' ') : null, content, }; }