Skip to content

Commit c07da4b

Browse files
fix(mobile): end an artifact template directive at its closing brace
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b399858 commit c07da4b

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,31 @@ 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;
301+
const ARTIFACT_TEMPLATE_START = "::artifact-template{";
302+
303+
/**
304+
* Spans of the directives `splitCodexArtifactTemplateMarkdown` renders as cards, each through
305+
* the `}` that closes it (quoted values may hold `}`). Resuming where a scan stopped keeps it linear.
306+
*/
307+
function artifactTemplateRanges(markdown: string): Array<{ start: number; end: number }> {
308+
const ranges: Array<{ start: number; end: number }> = [];
309+
let from = 0;
310+
for (;;) {
311+
const start = markdown.indexOf(ARTIFACT_TEMPLATE_START, from);
312+
if (start === -1) return ranges;
313+
let at = start + ARTIFACT_TEMPLATE_START.length;
314+
let quote: string | null = null;
315+
for (; at < markdown.length && markdown[at] !== "\n"; at += 1) {
316+
const char = markdown[at];
317+
if (quote !== null) {
318+
if (char === quote) quote = null;
319+
} else if (char === '"' || char === "'") quote = char;
320+
else if (char === "}") break;
321+
}
322+
if (markdown[at] === "}") ranges.push({ start, end: at + 1 });
323+
from = at + 1;
324+
}
325+
}
303326

304327
interface HtmlToken {
305328
readonly at: number;
@@ -406,15 +429,15 @@ export function splitMarkdownDetails(markdown: string): MarkdownDetailsSplit {
406429
};
407430
collect(parser.parse(markdown));
408431
// 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)];
432+
// is not a tag. Tokens arrive in source order, so one pass drops those inside a directive.
433+
const directives = artifactTemplateRanges(markdown);
411434
if (directives.length > 0) {
412435
let next = 0;
413436
const literal = (at: number) => {
414437
while (next < directives.length) {
415438
const directive = directives[next]!;
416-
if (at < directive.index) return false;
417-
if (at < directive.index + directive[0].length) return true;
439+
if (at < directive.start) return false;
440+
if (at < directive.end) return true;
418441
next += 1;
419442
}
420443
return false;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ describe("details folds in chat Markdown", () => {
218218
]);
219219
});
220220

221+
it("closes a fold at a </details> after an artifact template directive on the same line", () => {
222+
const directive =
223+
'::artifact-template{skill_name="artifact-template-x" skill_directory="/tmp/x" display_name="<details> {demo}" artifact_kind="document"}';
224+
expect(
225+
split(`<details>\n<summary>A</summary>${directive}</details>\n\nOutside.`),
226+
).toMatchObject([
227+
{ kind: "details", summary: "A", body: directive },
228+
{ kind: "markdown", markdown: "\n\nOutside." },
229+
]);
230+
});
231+
221232
it("finds definitions inside a compact HTML block and labels that wrap", () => {
222233
const markdown =
223234
"See [the\n doc].\n\n<details><summary>A</summary>a</details>\n[The Doc]: https://example.com";

0 commit comments

Comments
 (0)