From 6a5068ad2a0b1b9c350b53abba680a3c6d6d75e0 Mon Sep 17 00:00:00 2001 From: Kenny Daniel Date: Fri, 30 Jan 2026 02:30:19 -0800 Subject: [PATCH] Fix markdown indented codeblock --- package.json | 20 ++++++++++---------- src/components/Markdown/Markdown.test.tsx | 23 ++++++++++++++++------- src/components/Markdown/Markdown.tsx | 6 ++++-- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index bd2c1e2c..937dc70c 100644 --- a/package.json +++ b/package.json @@ -56,16 +56,16 @@ }, "dependencies": { "hightable": "0.26.0", - "hyparquet": "1.24.0", + "hyparquet": "1.24.1", "hyparquet-compressors": "1.1.1", "icebird": "0.3.1", "squirreling": "0.7.9" }, "devDependencies": { - "@storybook/react-vite": "10.2.0", + "@storybook/react-vite": "10.2.2", "@testing-library/react": "16.3.2", - "@types/node": "25.0.10", - "@types/react": "19.2.9", + "@types/node": "25.1.0", + "@types/react": "19.2.10", "@types/react-dom": "19.2.3", "@vitejs/plugin-react": "5.1.2", "@vitest/coverage-v8": "4.0.18", @@ -73,16 +73,16 @@ "eslint-plugin-react": "7.37.5", "eslint-plugin-react-hooks": "7.0.1", "eslint-plugin-react-refresh": "0.4.26", - "eslint-plugin-storybook": "10.2.0", - "globals": "17.1.0", + "eslint-plugin-storybook": "10.2.2", + "globals": "17.2.0", "jsdom": "27.4.0", "nodemon": "3.1.11", "npm-run-all": "4.1.5", - "react": "19.2.3", - "react-dom": "19.2.3", - "storybook": "10.2.0", + "react": "19.2.4", + "react-dom": "19.2.4", + "storybook": "10.2.2", "typescript": "5.9.3", - "typescript-eslint": "8.53.1", + "typescript-eslint": "8.54.0", "vite": "7.3.1", "vitest": "4.0.18" }, diff --git a/src/components/Markdown/Markdown.test.tsx b/src/components/Markdown/Markdown.test.tsx index 880f5779..54d79f18 100644 --- a/src/components/Markdown/Markdown.test.tsx +++ b/src/components/Markdown/Markdown.test.tsx @@ -255,16 +255,25 @@ describe('Markdown lists', () => { }) it('nested code block within a list item', () => { - const text = `- List item with code: - \`\`\`js - console.log("Nested code") - \`\`\`` + const text = `- Item 1 +- Item 2 code: + + \`\`\`bash + ./doStuff + \`\`\` + +- Item 3` const { container, getByText } = render() - getByText('List item with code:') - getByText('console.log("Nested code")') + getByText('Item 1') + getByText('Item 2 code:') + getByText('Item 3') const codeBlock = container.querySelector('pre') expect(codeBlock).toBeTruthy() - expect(codeBlock?.textContent).toContain('console.log("Nested code")') + // Leading spaces should be stripped + expect(codeBlock?.textContent).toBe('./doStuff') + // All items should be in the same list + expect(container.querySelectorAll('ul').length).toBe(1) + expect(container.querySelectorAll('li').length).toBe(3) }) it('list with unicode dash –', () => { diff --git a/src/components/Markdown/Markdown.tsx b/src/components/Markdown/Markdown.tsx index a0a4f40c..99919582 100644 --- a/src/components/Markdown/Markdown.tsx +++ b/src/components/Markdown/Markdown.tsx @@ -212,8 +212,9 @@ function parseList(lines: string[], start: number, baseIndent: number): [Token[] if (subIndent > baseIndent) { const trimmed = subline.trimStart() if (trimmed.startsWith('```')) { - // If it’s a fenced code block, parse until closing fence + // Fenced code block, parse until closing fence const language = trimmed.slice(3).trim() || undefined + const indentRegex = new RegExp(`^ {0,${subIndent}}`) i++ const codeLines: string[] = [] while (i < lines.length && !lines[i]?.trimStart().startsWith('```')) { @@ -221,7 +222,8 @@ function parseList(lines: string[], start: number, baseIndent: number): [Token[] if (line === undefined) { throw new Error(`Line is undefined at index ${i}`) } - codeLines.push(line) + // Strip spaces to match the fence indent + codeLines.push(line.replace(indentRegex, '')) i++ } i++ // skip the closing ```