Skip to content
Draft
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
10 changes: 10 additions & 0 deletions webview-ui/src/utils/__tests__/markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ describe("markdown heading helpers", () => {
expect(countMarkdownHeadings("")).toBe(0)
})

it("returns 0 for non-string values", () => {
// These simulate runtime scenarios where a non-string truthy value
// is passed due to unexpected API data (see issue #11605)
expect(countMarkdownHeadings(42 as unknown as string)).toBe(0)
expect(countMarkdownHeadings({ key: "value" } as unknown as string)).toBe(0)
expect(countMarkdownHeadings(["# heading"] as unknown as string)).toBe(0)
expect(countMarkdownHeadings(true as unknown as string)).toBe(0)
expect(countMarkdownHeadings(null as unknown as string)).toBe(0)
})

it("counts single and multiple headings", () => {
expect(countMarkdownHeadings("# One")).toBe(1)
expect(countMarkdownHeadings("# One\nContent")).toBe(1)
Expand Down
2 changes: 1 addition & 1 deletion webview-ui/src/utils/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Code fences are stripped before matching to avoid false positives.
*/
export function countMarkdownHeadings(text: string | undefined): number {
if (!text) return 0
if (typeof text !== "string" || !text) return 0

// Remove fenced code blocks to avoid counting headings inside code
const withoutCodeBlocks = text.replace(/```[\s\S]*?```/g, "")
Expand Down
Loading