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
58 changes: 45 additions & 13 deletions scripts/sync-agent-variant-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,51 @@ function upsertFrontmatterLine(frontmatter: string, key: string, value: string):
}

function stripAgentOnlyBlocksForVariant(body: string, activeVariant: AgentVariant): string {
return body.replace(
/\n?<AgentOnly variant="([^"]+)">\n([\s\S]*?)\n<\/AgentOnly>\n?/g,
(_match, variant: string, content: string) => {
if (
!variant
.split(",")
.map((item) => item.trim())
.includes(activeVariant)
)
return "\n";
return `\n${content.trim()}\n`;
},
);
type OpenBlock = {
include: boolean;
lines: string[];
openLine: string;
};

const renderedLines: string[] = [];
let openBlock: OpenBlock | undefined;

for (const line of body.split("\n")) {
if (openBlock) {
if (line.match(/^<\/AgentOnly>\s*$/)) {
if (openBlock.include) renderedLines.push(...openBlock.lines);
openBlock = undefined;
continue;
}
openBlock.lines.push(line);
continue;
}

const openMatch = line.match(/^<AgentOnly variant="([^"]+)">\s*$/);
if (openMatch) {
openBlock = {
include: agentOnlyVariantMatches(openMatch[1], activeVariant),
lines: [],
openLine: line,
};
continue;
}

renderedLines.push(line);
}

if (openBlock) {
renderedLines.push(openBlock.openLine, ...openBlock.lines);
}

return renderedLines.join("\n");
}
Comment thread
miyoungc marked this conversation as resolved.

function agentOnlyVariantMatches(variant: string, activeVariant: AgentVariant): boolean {
return variant
.split(",")
.map((item) => item.trim())
.includes(activeVariant);
}

export function renderAgentVariantPage(
Expand Down
23 changes: 23 additions & 0 deletions test/agent-variant-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ describe("agent variant docs", () => {
expect(rendered).not.toContain("<AgentOnly");
});

it("keeps adjacent list items together after variant filtering", () => {
const rendered = renderAgentVariantPage(
`---
title: "Example"
---
## Prerequisites

<AgentOnly variant="openclaw">
- NemoClaw installed.
</AgentOnly>
<AgentOnly variant="hermes">
- NemoHermes installed.
</AgentOnly>
- A local model server running.
`,
"openclaw",
);

expect(rendered).toContain("- NemoClaw installed.\n- A local model server running.");
expect(rendered).not.toContain("- NemoClaw installed.\n\n- A local model server running.");
expect(rendered).not.toContain("NemoHermes installed.");
});

it("rewrites relative imports but preserves Fern route links for generated build output", () => {
const rendered = renderAgentVariantPage(
`${source}\nSee [Commands](../reference/commands#$$nemoclaw-list).\nSee [Backup](backup-restore).\n![Diagram](images/diagram.png)\n`,
Expand Down
Loading