| 1 | import { languages, type CageLocale } from './languages'; |
| 2 | export { languages, type CageLocale } from './languages'; |
| 3 | export const locales = languages.map(language => language.code); |
| 4 | const readmes = import.meta.glob<string>('../../../README*.md', { query: '?raw', import: 'default', eager: true }); |
| 5 | |
| 6 | function plain(text: string) { |
| 7 | return text.replace(/<img\b[^>]*>/g, '').replace(/!\[[^\]]*\]\([^)]*\)/g, '') |
| 8 | .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1').replace(/<[^>]+>/g, '') |
| 9 | .replace(/\*\*|`/g, '').trim(); |
| 10 | } |
| 11 | function paragraphs(text: string) { |
| 12 | return text.replace(/```[\s\S]*?```/g, '').split(/\n\s*\n/) |
| 13 | .filter(block => !/^\s*(?:[-#|]|\d+\.)/.test(block)) |
| 14 | .map(plain).filter(Boolean); |
| 15 | } |
| 16 | function bullets(text: string) { |
| 17 | return [...text.matchAll(/^- (.+)$/gm)].map(match => match[1]); |
| 18 | } |
| 19 | function pair(text: string): [string, string] { |
| 20 | const bold = text.match(/^\*\*(.+?)\*\*\s*(.*)$/); |
| 21 | return bold ? [plain(bold[1]), plain(bold[2])] : ['', plain(text)]; |
| 22 | } |
| 23 | function readCopy(language: typeof languages[number]) { |
| 24 | const file = language.code === 'zh' ? 'README.md' : `README.${language.code}.md`; |
| 25 | const source = readmes[`../../../${file}`]; |
| 26 | if (!source) throw new Error(`Missing website language source: ${file}`); |
| 27 | const allHeadings = [...source.matchAll(/^## (.+)$/gm)]; |
| 28 | const headings = allHeadings.filter(heading => !/^Star (?:History|增长曲线|成長曲線)$/.test(heading[1])); |
| 29 | if (headings.length !== 9) throw new Error(`${file}: expected the nine shared README sections; update the website mapping when the document structure changes`); |
| 30 | const sections = headings.map(heading => ({ |
| 31 | title: heading[1], body: source.slice(heading.index! + heading[0].length, allHeadings[allHeadings.indexOf(heading) + 1]?.index ?? source.length), |
| 32 | })); |
| 33 | const [reason, uses, quick, installation, steps, boundaries, contribution, license] = sections; |
| 34 | const reasonParagraphs = paragraphs(reason.body); |
| 35 | const installParagraphs = paragraphs(installation.body); |
| 36 | const captions = [...reason.body.matchAll(/^\*\*(.+?)\*\*\s*$/gm)].map(match => plain(match[1])); |
| 37 | const stepHeadings = [...steps.body.matchAll(/^### \d+\. (.+)$/gm)]; |
| 38 | if (stepHeadings.length !== 3 || bullets(reason.body).length !== 4 || bullets(boundaries.body).length !== 4) { |
| 39 | throw new Error(`${file}: README feature, setup or boundary structure has changed`); |
| 40 | } |
| 41 | const setup = stepHeadings.map((heading, i): [string,string] => [heading[1], plain(steps.body.slice(heading.index! + heading[0].length, stepHeadings[i+1]?.index ?? steps.body.length))]); |
| 42 | const docs = [...contribution.body.matchAll(/^\|\s*([^|]+)\s*\|\s*\[([^\]]+)\]\(([^)]+)\)\s*\|/gm)].map(match => ({ |
| 43 | title: plain(match[1]), label: match[2], href: match[3].startsWith('https://') ? match[3] : `https://github.com/Lee-xeo/Claude-Cage/blob/main/${match[3]}`, |
| 44 | })); |
| 45 | const slogan = source.slice(0, headings[0].index).match(/^\*\*(.+?)\*\*$/m)?.[1] || 'Claude Cage'; |
| 46 | const isChinese = language.code === 'zh' || language.code === 'zh-TW'; |
| 47 | const limit = plain(bullets(boundaries.body).at(-1)!); |
| 48 | return { |
| 49 | ...language, title: `Claude Cage — ${plain(slogan)}`, description: reasonParagraphs[1], |
| 50 | home: 'Claude Cage', start: quick.title, boundary: boundaries.title, |
| 51 | hero: isChinese ? '把 Claude 大小姐' : 'Claude Cage', |
| 52 | heroAccent: language.code === 'zh' ? '关起来。' : language.code === 'zh-TW' ? '關起來。' : '', |
| 53 | tagline: isChinese ? plain(slogan).split(/[,,]/).slice(1).join(',') : plain(slogan), |
| 54 | intro: reasonParagraphs[1], requirements: 'macOS · Windows · Linux · Android · iOS', artAlt: 'Claude Cage', |
| 55 | why: reason.title, whyText: reasonParagraphs[0], whyNote: limit, |
| 56 | keyFeatures: bullets(reason.body).map(plain), |
| 57 | featureTitle: uses.title, features: bullets(uses.body).map(pair), |
| 58 | workbench: captions[1] || 'Claude Cage', |
| 59 | evidenceTitle: captions[0] || language.checksLabel, evidenceNote: limit, |
| 60 | quickTitle: quick.title, quickText: paragraphs(quick.body)[0], |
| 61 | prompt: quick.body.match(/```[^\n]*\n([\s\S]*?)```/)?.[1]?.trim() || '', |
| 62 | quickNote: paragraphs(quick.body).slice(1).join('\n'), |
| 63 | stepsTitle: steps.title, steps: setup, |
| 64 | boundaryTitle: boundaries.title, boundaries: bullets(boundaries.body).map(pair), |
| 65 | downloadTitle: installation.title, downloadText: installParagraphs[1], |
| 66 | downloadNote: installParagraphs[0], updateNote: installParagraphs.at(-1) || '', |
| 67 | communityTitle: contribution.title, communityText: paragraphs(contribution.body).at(-1) || '', |
| 68 | group: 'QQ · 1134250870', docsTitle: contribution.title, docs, |
| 69 | licenseTitle: license.title, licenseText: paragraphs(license.body).slice(0,-1).join('\n\n'), |
| 70 | independent: paragraphs(license.body).at(-1) || '', |
| 71 | }; |
| 72 | } |
| 73 | export const cageCopy = Object.fromEntries(languages.map(language => [language.code, readCopy(language)])) as Record<CageLocale, ReturnType<typeof readCopy>>; |