Files
slm-design/concat-md.js
S.Gromov 5553ece16d
All checks were successful
CI/CD Pipeline / docs (push) Successful in 13s
CI/CD Pipeline / docker (push) Successful in 36s
CI/CD Pipeline / deploy (push) Successful in 7s
chore: переместить br в README при генерации
2026-04-19 07:56:01 +03:00

108 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import path from "path";
import fs from "fs";
// Явный порядок файлов внутри каждого языка
const fileOrder = [
"index.md",
// reference
"reference/layers.md",
"reference/modules.md",
"reference/segments.md",
];
// Удалить frontmatter из содержимого md-файла
const stripFrontmatter = (content) =>
content.replace(/^---[\s\S]*?---\n*/m, "");
// Удалить блоки между маркерами <!-- rules-link --> и <!-- /rules-link -->
const stripRulesLink = (content) =>
content.replace(/<!-- rules-link -->[\s\S]*?<!-- \/rules-link -->\n*/g, "");
// Сдвинуть уровень заголовков на 1 вниз (h1→h2, h2→h3, ...)
// Не трогает заголовки внутри блоков кода
const shiftHeadings = (content) => {
const lines = content.split("\n");
let inCodeBlock = false;
return lines
.map((line) => {
if (line.startsWith("```")) inCodeBlock = !inCodeBlock;
if (inCodeBlock) return line;
if (/^#{1,5}\s/.test(line)) return "#" + line;
return line;
})
.join("\n");
};
// Собрать ARCHITECTURE.md с мета-якорями для каждого файла
const buildRules = (lang) => {
const srcDir = `./docs/${lang}`;
const outDir = `./generated/${lang}`;
const outFile = path.join(outDir, "ARCHITECTURE.md");
if (!fs.existsSync(srcDir)) {
console.log(`Пропуск ${lang}: папка ${srcDir} не найдена`);
return;
}
fs.mkdirSync(outDir, { recursive: true });
const parts = [];
for (const file of fileOrder) {
const filePath = path.join(srcDir, file);
if (!fs.existsSync(filePath)) continue;
const raw = fs.readFileSync(filePath, "utf8");
const content = stripRulesLink(stripFrontmatter(raw)).trim();
if (!content) continue;
// Мета-якорь: путь VitePress без расширения
const route = "/" + file.replace(/\.md$/, "");
// index.md остаётся без сдвига (его h1 — главный заголовок документа)
const processed = file === "index.md" ? content : shiftHeadings(content);
parts.push(`<!-- ${route} -->\n${processed}`);
}
fs.writeFileSync(outFile, parts.join("\n\n"), "utf8");
console.log(`ARCHITECTURE.md (${lang}) создан: ${outFile}`);
};
// Собираем ARCHITECTURE.md для обоих языков
buildRules("ru");
buildRules("en");
// Версия из package.json
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8"));
const version = `v${pkg.version}`;
// Подставить версию в ссылки
const replaceVersion = (content) =>
content.replace(/raw\/branch\/main/g, `raw/tag/${version}`);
// Генерируем README из index.md
const buildReadme = (lang, outFile) => {
const indexPath = `./docs/${lang}/index.md`;
if (!fs.existsSync(indexPath)) {
console.log(`Пропуск README (${lang}): ${indexPath} не найден`);
return;
}
let content = replaceVersion(stripFrontmatter(fs.readFileSync(indexPath, "utf8")));
// В README <br> нужен вверху блока, а не внизу
content = content.replace(
/<!-- rules-link -->\n(> .+\n> .+)\n\n<br>\n<!-- \/rules-link -->/,
"<!-- rules-link -->\n<br>\n\n$1\n<!-- /rules-link -->"
);
fs.writeFileSync(outFile, content, "utf8");
console.log(`${outFile} создан из ${indexPath} (${version})`);
};
buildReadme("ru", "./README_RU.md");