Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions scripts/docs-site/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { parseFrontmatter } from "../../.openclaw-sync/lib/docs-markdown.mjs";
import { renderPageOgCards, renderOgPng } from "./og-cards.mjs";
import { renderDefaultOgSvg } from "./og-card-template.mjs";
import { activeTabTitle, groupForPage, flattenNavEntries, flattenNav } from "./navigation.mjs";
import { releaseNavigationEntries, patchReleaseSlugs, patchReleaseBadgeHtml } from "./release-navigation.mjs";
import { isVersionedReleasePage, releaseNavigationEntries, patchReleaseSlugs, patchReleaseBadgeHtml } from "./release-navigation.mjs";
import { resolveRedirects } from "../../.openclaw-sync/lib/docs-redirects.mjs";

const root = process.cwd();
Expand Down Expand Up @@ -307,7 +307,7 @@ function writePage(page) {
const article = renderCache ? renderCache.render(page.raw, options) : renderArticle(page.raw, options);
const composed = page.slug === "index" ? homeLayoutHtml(article, icon, announcement ? { ...announcement, href: pageRoute(announcedReleasePage) } : null, page.locale) : article;
const html = rewriteInternalUrls(rewriteOfficialDiscordLinks(composed, page.slug), page.locale);
const toc = tableOfContents(html);
const toc = isVersionedReleasePage(page.slug) ? releaseTableOfContents(html) : tableOfContents(html);
const outPath = path.join(outDir, pageRoute(page).replace(/^\//, ""), "index.html");
fs.mkdirSync(path.dirname(outPath), { recursive: true });
fs.writeFileSync(outPath, layout({ page, nav, activeTab, html, toc, prev, next }), "utf8");
Expand Down Expand Up @@ -382,7 +382,6 @@ function layout({ page, nav, activeTab, html, toc, prev, next }) {
const ogTitle = page.slug === "index" ? `${config.name} Docs` : `${page.title} · ${config.name}`;
const canonicalUrl = canonicalOrigin ? `${canonicalOrigin}${pageRoute(page)}` : "";
const analyticsEligible = process.env.DOCS_SITE_GA4_ENABLED === "1" && !previewMode && !page.hidden && !basePath && canonicalOrigin === "https://docs.openclaw.ai";
// tableOfContents already caps this shared UI/analytics heading list at 24.
const pageOgVersion = page.locale === "en" ? pageOgVersions.get(page.slug) : undefined;
const pageOgPath = pageOgVersion
? `/og/${page.slug}.png`
Expand Down Expand Up @@ -424,7 +423,7 @@ ${canonicalUrl ? `<meta property="og:url" content="${escapeAttr(canonicalUrl)}">
${siteHeader(page)}
<div class="doc-shell">
${sidebar(page, nav, activeTab)}
<div class="main"${analyticsEligible ? ` data-analytics-path="${escapeAttr(pageRoute(page))}" data-analytics-title="${escapeAttr(title)}" data-analytics-sections="${escapeAttr(JSON.stringify(toc.map(item => item.id)))}"` : ""}>
<div class="main"${analyticsEligible ? ` data-analytics-path="${escapeAttr(pageRoute(page))}" data-analytics-title="${escapeAttr(title)}" data-analytics-sections="${escapeAttr(JSON.stringify(toc.slice(0, 24).map(item => item.id)))}"` : ""}>
${homeHeroArt}
<div class="page-intro"></div>
${tocHtml(toc, page.locale)}
Expand Down Expand Up @@ -727,6 +726,29 @@ function navLink(activePage, page) {
return `<a class="nav-link${active}" href="${pageUrl(page)}"${previewLinkAttrs(page)}${active ? ' aria-current="page"' : ""}>${escapeHtml(page.title)}${patchReleases.has(page.slug) ? patchReleaseBadgeHtml(activePage.locale) : ""}${announcement && page.slug === announcedReleasePage.slug ? releaseBadgeHtml(announcement, activePage.locale) : ""}</a>`;
}

// Release topics and sources can contain real headings inside disclosures. Keep
// their deep links in the article, but only index top-level section headings.
function releaseTableOfContents(html) {
const items = [];
let depth = 0;
let heading;
new Parser({
onopentag(tag, attrs) {
if (depth === 0 && tag === "h2" && attrs.id) heading = { level: 2, id: attrs.id, title: "" };
depth++;
},
ontext(text) { if (heading) heading.title += text; },
onclosetag() {
if (--depth === 0 && heading) {
heading.title = heading.title.replace(/^#\s*/, "").replace(/\s+/g, " ").trim();
items.push(heading);
heading = undefined;
}
},
}).end(html);
return items;
}

function tableOfContents(html) {
return [...html.matchAll(/<h([23])\b[^>]*\bid="([^"]+)"[^>]*>([\s\S]*?)<\/h\1>/g)]
.map((m) => ({ level: Number(m[1]), id: m[2], title: decodeHtmlEntities(stripTags(m[3]).replace(/^#\s*/, "")) }))
Expand Down
4 changes: 4 additions & 0 deletions scripts/docs-site/release-navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function releaseMonth(slug) {
return `${match[1]}-${match[2].padStart(2, "0")}`;
}

export function isVersionedReleasePage(slug) {
return Boolean(releaseMonth(slug.split("/").slice(0, 2).join("/")));
}

// Group adjacent release pages without changing source order, links, or groups.
export function releaseNavigationEntries(entries, locale, activeSlug) {
const firstMonth = flattenNavEntries(entries).map(page => releaseMonth(page.slug)).find(Boolean);
Expand Down
23 changes: 23 additions & 0 deletions scripts/docs-site/release-navigation.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,26 @@ test("responsive release disclosures reveal the selected month on direct and PJA
await context.close();
}
});

test("versioned release outlines keep all top-level sections, not nested topics", t => {
const body = ["# Notes", "## First", ...Array.from({ length: 26 }, (_, i) => `### Topic ${i}`),
'<details><summary>Sources</summary>', '## Nested source', '</details>',
...Array.from({ length: 25 }, (_, i) => `## Group ${i}`)].join("\n\n");
const f = fixture(t, [], Object.fromEntries(["releases/2026.11.5", "de/releases/2026.11.5", "releases/2026.8.1/messaging", "releases/index", "guide"].map(route => [`${route}.md`, body])));
const built = f.build({ DOCS_SITE_GA4_ENABLED: "1" });
assert.equal(built.status, 0, built.stderr);
for (const route of ["releases/2026.11.5", "de/releases/2026.11.5", "releases/2026.8.1/messaging", "releases", "guide"]) {
const html = fs.readFileSync(path.join(f.root, "dist/docs-site", route, "index.html"), "utf8");
const toc = html.match(/<details class="toc"[\s\S]*?<nav>([\s\S]*?)<\/nav>/)[1];
const links = [...toc.matchAll(/<a class="toc-l\d" href="#([^"]+)"/g)].map(m => m[1]);
if (route === "guide" || route === "releases") {
assert.equal(links.length, 24);
assert.ok(links.includes("topic-0"));
} else {
assert.deepEqual(links, ["first", ...Array.from({ length: 25 }, (_, i) => `group-${i}`)]);
assert.match(html, /id="nested-source"/);
assert.match(html, /id="topic-0"/);
assert.ok(!html.match(/data-analytics-sections="[^"]*group-24/));
}
}
});
Loading