diff --git a/generate-llms.ts b/generate-llms.ts index 6d55767..ca1a9bc 100644 --- a/generate-llms.ts +++ b/generate-llms.ts @@ -547,6 +547,29 @@ const buildSitemap = (): void => { console.log(`${PUBLIC_DIR}/sitemap.xml создан`); }; +/** + * Преобразовать абсолютные ссылки `index.md` в рабочие при открытии + * README в репозитории: + * - `/docs/foo` → относительный путь `docs/docs/foo.md`; + * - корневые ресурсы (`/llms.txt`, `/llms-full.txt`, `*.zip`, `/manifest.json`, + * `/sitemap.xml`, `/robots.txt`) — генерируемые, в репозитории отсутствуют, + * поэтому ссылки переписываются на абсолютный `SITE_URL`. + */ +const transformReadmeLinks = (content: string): string => { + const linkRe = /\]\((\/[^)\s]*)\)/g; + return content.replace(linkRe, (match, href: string) => { + const [pathPart, hash = ''] = href.split('#'); + const hashPart = hash ? `#${hash}` : ''; + + if (pathPart.startsWith(DOC_PREFIX)) { + const rel = linkToArchiveRel(pathPart); + return `](docs/docs/${rel}${hashPart})`; + } + + return `](${SITE_URL}${pathPart}${hashPart})`; + }); +}; + /** Скопировать `index.md` документации в корневой README без frontmatter. */ const buildReadme = (): void => { const indexPath = 'docs/docs/index.md'; @@ -556,7 +579,15 @@ const buildReadme = (): void => { } const raw = fs.readFileSync(indexPath, 'utf8'); const { body } = parseFrontmatter(raw); - fs.writeFileSync('README.md', body.trimStart(), 'utf8'); + const transformed = transformReadmeLinks(body.trimStart()); + + // Порядок: H1 → описание (первый абзац) → ссылка на сайт. + const withSiteLink = transformed.replace( + /^(#\s[^\n]*\n\n[^\n]+\n)/, + `$1\nСайт: ${SITE_URL}\n`, + ); + + fs.writeFileSync('README.md', withSiteLink, 'utf8'); console.log(`README.md обновлён из ${indexPath}`); };