Integrate knowledge docs and admin updates

This commit is contained in:
dcconstructions
2026-07-06 00:27:04 +03:00
parent 5e26b9b80f
commit bb7c54e9ca
43 changed files with 7038 additions and 72 deletions
+134 -5
View File
@@ -4,6 +4,7 @@ import { fileURLToPath } from "node:url";
const repoRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
const pagePath = join(repoRoot, "content", "pages", "home.json");
const knowledgePath = join(repoRoot, "content", "knowledge.json");
const robotsPath = join(repoRoot, "robots.txt");
const sitemapPath = join(repoRoot, "sitemap.xml");
const templateRoot = join(repoRoot, "templates", "pages", "home");
@@ -193,6 +194,115 @@ function escapeXml(value) {
.replaceAll("'", "'");
}
function stripHtml(value) {
return String(value ?? "")
.replace(/<[^>]*>/g, " ")
.replace(/\s+/g, " ")
.trim();
}
function normalizeLandingTarget(value) {
const raw = String(value || "").trim();
if (!raw || /^javascript:/i.test(raw) || isExternalHref(raw)) return "";
const hashValue = raw.includes("#") ? raw.slice(raw.indexOf("#") + 1) : raw;
return hashValue
.replace(/^\/+/, "")
.replace(/^index\.html#?/i, "")
.replace(/^#+/, "")
.trim();
}
function validateLandingTargets(model) {
const targets = new Map();
for (const block of allBlocks(model)) {
if (block.enabled === false) continue;
const target = normalizeLandingTarget(block.anchor);
if (!target) continue;
if (targets.has(target)) {
const first = targets.get(target);
throw new Error(
`Duplicate landing target "${target}" in blocks "${first.adminLabel || first.id}" and "${block.adminLabel || block.id}"`,
);
}
targets.set(target, block);
}
}
function slugify(value, fallback = "section") {
const map = {
а: "a",
б: "b",
в: "v",
г: "g",
д: "d",
е: "e",
ё: "e",
ж: "zh",
з: "z",
и: "i",
й: "y",
к: "k",
л: "l",
м: "m",
н: "n",
о: "o",
п: "p",
р: "r",
с: "s",
т: "t",
у: "u",
ф: "f",
х: "h",
ц: "c",
ч: "ch",
ш: "sh",
щ: "sch",
ъ: "",
ы: "y",
ь: "",
э: "e",
ю: "yu",
я: "ya",
};
const safe = String(value ?? "")
.toLowerCase()
.replace(/[а-яё]/g, (char) => map[char] ?? "")
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
return safe || fallback;
}
function collectKnowledgeUrls(source, canonical) {
let model;
try {
model = JSON.parse(source);
} catch {
return [];
}
const basePath = String(model.basePath || "knowledge").replace(/^\/+|\/+$/g, "") || "knowledge";
const urls = [new URL(`${basePath}/`, canonical).href];
function walk(nodes = [], parentSegments = []) {
nodes.forEach((node, index) => {
if (node.enabled === false) return;
const segment = slugify(node.slug || node.title || node.id, `page-${index + 1}`);
const segments = [...parentSegments, segment];
if (node.useContent !== false && stripHtml(node.bodyHtml).length > 0) {
urls.push(new URL(`${basePath}/${segments.join("/")}/`, canonical).href);
}
if (Array.isArray(node.children)) walk(node.children, segments);
});
}
walk(model.tree || []);
return urls;
}
function canonicalUrl(model) {
const fallback = "https://nodedc.dctouch.ru/";
const value = String(model.seo?.canonicalUrl || fallback).trim() || fallback;
@@ -208,6 +318,8 @@ function canonicalUrl(model) {
async function writeSeoFiles(model) {
const canonical = canonicalUrl(model);
const sitemapUrl = new URL("sitemap.xml", canonical).href;
const knowledgeSource = await readFile(knowledgePath, "utf8").catch(() => "");
const knowledgeUrls = collectKnowledgeUrls(knowledgeSource, canonical);
const robots = ["User-agent: *", "Allow: /", `Sitemap: ${sitemapUrl}`, ""].join("\n");
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
@@ -216,6 +328,15 @@ async function writeSeoFiles(model) {
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
${knowledgeUrls
.map(
(url) => ` <url>
<loc>${escapeXml(url)}</loc>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>`,
)
.join("\n")}
</urlset>
`;
@@ -306,6 +427,16 @@ function scopeDuplicateIds(html, scope) {
return scoped;
}
function htmlHasId(html, id) {
return new RegExp(`\\sid="${escapeRegExp(id)}"`).test(html);
}
function withLandingTarget(html, block) {
const target = normalizeLandingTarget(block.anchor);
if (!target || htmlHasId(html, target)) return html;
return `<span id="${escapeAttr(target)}" class="landing-target" aria-hidden="true"></span>${html}`;
}
async function renderBlock(block, renderCounts) {
if (block.enabled === false) {
return "";
@@ -316,12 +447,9 @@ async function renderBlock(block, renderCounts) {
renderCounts.set(templateName, renderCount + 1);
const html = renderTemplate(await readTemplate(templateName), block);
const rendered = renderCount === 0 && block.scopeIds !== true ? html : scopeDuplicateIds(html, block.id);
if (renderCount === 0 && block.scopeIds !== true) {
return html;
}
return scopeDuplicateIds(html, block.id);
return withLandingTarget(rendered, block);
}
async function renderRegion(region) {
@@ -367,6 +495,7 @@ async function renderSections() {
applySeoDefaults(page);
applySharedDemoRequest(page);
applyRenderableLinks(page);
validateLandingTargets(page);
let output = renderTemplate(shell, { id: page.slug, ...page });