Skip to content

Commit afca623

Browse files
authored
Fix markdown indented codeblock (#370)
1 parent eede52a commit afca623

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,33 +56,33 @@
5656
},
5757
"dependencies": {
5858
"hightable": "0.26.0",
59-
"hyparquet": "1.24.0",
59+
"hyparquet": "1.24.1",
6060
"hyparquet-compressors": "1.1.1",
6161
"icebird": "0.3.1",
6262
"squirreling": "0.7.9"
6363
},
6464
"devDependencies": {
65-
"@storybook/react-vite": "10.2.0",
65+
"@storybook/react-vite": "10.2.2",
6666
"@testing-library/react": "16.3.2",
67-
"@types/node": "25.0.10",
68-
"@types/react": "19.2.9",
67+
"@types/node": "25.1.0",
68+
"@types/react": "19.2.10",
6969
"@types/react-dom": "19.2.3",
7070
"@vitejs/plugin-react": "5.1.2",
7171
"@vitest/coverage-v8": "4.0.18",
7272
"eslint": "9.39.2",
7373
"eslint-plugin-react": "7.37.5",
7474
"eslint-plugin-react-hooks": "7.0.1",
7575
"eslint-plugin-react-refresh": "0.4.26",
76-
"eslint-plugin-storybook": "10.2.0",
77-
"globals": "17.1.0",
76+
"eslint-plugin-storybook": "10.2.2",
77+
"globals": "17.2.0",
7878
"jsdom": "27.4.0",
7979
"nodemon": "3.1.11",
8080
"npm-run-all": "4.1.5",
81-
"react": "19.2.3",
82-
"react-dom": "19.2.3",
83-
"storybook": "10.2.0",
81+
"react": "19.2.4",
82+
"react-dom": "19.2.4",
83+
"storybook": "10.2.2",
8484
"typescript": "5.9.3",
85-
"typescript-eslint": "8.53.1",
85+
"typescript-eslint": "8.54.0",
8686
"vite": "7.3.1",
8787
"vitest": "4.0.18"
8888
},

src/components/Markdown/Markdown.test.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,25 @@ describe('Markdown lists', () => {
255255
})
256256

257257
it('nested code block within a list item', () => {
258-
const text = `- List item with code:
259-
\`\`\`js
260-
console.log("Nested code")
261-
\`\`\``
258+
const text = `- Item 1
259+
- Item 2 code:
260+
261+
\`\`\`bash
262+
./doStuff
263+
\`\`\`
264+
265+
- Item 3`
262266
const { container, getByText } = render(<Markdown text={text} />)
263-
getByText('List item with code:')
264-
getByText('console.log("Nested code")')
267+
getByText('Item 1')
268+
getByText('Item 2 code:')
269+
getByText('Item 3')
265270
const codeBlock = container.querySelector('pre')
266271
expect(codeBlock).toBeTruthy()
267-
expect(codeBlock?.textContent).toContain('console.log("Nested code")')
272+
// Leading spaces should be stripped
273+
expect(codeBlock?.textContent).toBe('./doStuff')
274+
// All items should be in the same list
275+
expect(container.querySelectorAll('ul').length).toBe(1)
276+
expect(container.querySelectorAll('li').length).toBe(3)
268277
})
269278

270279
it('list with unicode dash –', () => {

src/components/Markdown/Markdown.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,18 @@ function parseList(lines: string[], start: number, baseIndent: number): [Token[]
212212
if (subIndent > baseIndent) {
213213
const trimmed = subline.trimStart()
214214
if (trimmed.startsWith('```')) {
215-
// If it’s a fenced code block, parse until closing fence
215+
// Fenced code block, parse until closing fence
216216
const language = trimmed.slice(3).trim() || undefined
217+
const indentRegex = new RegExp(`^ {0,${subIndent}}`)
217218
i++
218219
const codeLines: string[] = []
219220
while (i < lines.length && !lines[i]?.trimStart().startsWith('```')) {
220221
const line = lines[i]
221222
if (line === undefined) {
222223
throw new Error(`Line is undefined at index ${i}`)
223224
}
224-
codeLines.push(line)
225+
// Strip spaces to match the fence indent
226+
codeLines.push(line.replace(indentRegex, ''))
225227
i++
226228
}
227229
i++ // skip the closing ```

0 commit comments

Comments
 (0)