Files
slm-design/concat-md.js
S.Gromov 515506e01e chore: версия из package.json для тегов и ссылок
- версия берётся из package.json (0.1.0)
- concat-md.js подставляет версию в README_RU.md
- CI берёт версию из package.json, создаёт тег, не повышает автоматически
- убран husky
2026-04-19 02:04:22 +03:00

101 lines
3.2 KiB
JavaScript
Raw Permalink 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;
}
const content = replaceVersion(stripFrontmatter(fs.readFileSync(indexPath, "utf8")));
fs.writeFileSync(outFile, content, "utf8");
console.log(`${outFile} создан из ${indexPath} (${version})`);
};
buildReadme("ru", "./README_RU.md");