Skip to content

Commit 5eddd6a

Browse files
fix(mobile): a <details> in an artifact template label is not a fold tag
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f7168d0 commit 5eddd6a

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎apps/mobile/src/lib/markdownDocument.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ export type MarkdownDetailsSegment =
298298
const DETAILS_TAG = /^<(\/?)details(?=[\s/>])/i;
299299
const SUMMARY_TAG = /^<summary(?=[\s/>])/i;
300300
const SUMMARY_CLOSE_TAG = /^<\/summary\s*>$/i;
301+
// Matches the directive `splitCodexArtifactTemplateMarkdown` renders as a card.
302+
const ARTIFACT_TEMPLATE_DIRECTIVE = /::artifact-template\{[^\n]*/g;
301303

302304
interface HtmlToken {
303305
readonly at: number;
@@ -386,7 +388,7 @@ export function splitMarkdownDetails(markdown: string): MarkdownDetailsSplit {
386388
definitions: [],
387389
};
388390
if (!/<details/i.test(markdown)) return whole;
389-
const tokens: HtmlToken[] = [];
391+
let tokens: HtmlToken[] = [];
390392
const collect = (node: MarkdownNode) => {
391393
if (!("children" in node)) return;
392394
for (const child of node.children) {
@@ -403,6 +405,22 @@ export function splitMarkdownDetails(markdown: string): MarkdownDetailsSplit {
403405
}
404406
};
405407
collect(parser.parse(markdown));
408+
// An artifact template directive's attributes are literal text, so a `<details>` in its label
409+
// is not a tag. Tokens arrive in source order, so one pass drops those inside a directive line.
410+
const directives = [...markdown.matchAll(ARTIFACT_TEMPLATE_DIRECTIVE)];
411+
if (directives.length > 0) {
412+
let next = 0;
413+
const literal = (at: number) => {
414+
while (next < directives.length) {
415+
const directive = directives[next]!;
416+
if (at < directive.index) return false;
417+
if (at < directive.index + directive[0].length) return true;
418+
next += 1;
419+
}
420+
return false;
421+
};
422+
tokens = tokens.filter((token) => !literal(token.at));
423+
}
406424
// Read definitions after flattening, which exposes ones a raw HTML block hides from this parse.
407425
const definitions: MarkdownReferenceDefinition[] = [];
408426
if (markdown.includes("]:")) {

‎apps/mobile/src/lib/markdownHtml.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,20 @@ describe("details folds in chat Markdown", () => {
204204
);
205205
});
206206

207+
it.each([
208+
["on its own line", "\n\n"],
209+
["right after the summary", ""],
210+
])("does not count a <details> in an artifact template label %s", (_, gap) => {
211+
const directive =
212+
'::artifact-template{skill_name="artifact-template-x" skill_directory="/tmp/x" display_name="<details> demo" artifact_kind="document"}';
213+
expect(
214+
split(`<details>\n<summary>A</summary>${gap}${directive}\n\n</details>\n\nOutside.`),
215+
).toMatchObject([
216+
{ kind: "details", summary: "A", body: `${gap}${directive}\n\n` },
217+
{ kind: "markdown", markdown: "\n\nOutside." },
218+
]);
219+
});
220+
207221
it("finds definitions inside a compact HTML block and labels that wrap", () => {
208222
const markdown =
209223
"See [the\n doc].\n\n<details><summary>A</summary>a</details>\n[The Doc]: https://example.com";

0 commit comments

Comments
 (0)