Skip to content
Closed
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
44 changes: 23 additions & 21 deletions src/md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,31 @@ export function transformMarkdown(buf: Buffer | string): string {
for (const line of bufToString(buf).split(/\r?\n/)) {
switch (state) {
case 'root':
// Strip up to 3 leading spaces before checking for code fences (per CommonMark spec)
const stripped = line.replace(/^ {0,3}/, '')
const { fence, js, bash } = stripped.match(codeBlockRe)?.groups || {}
if (fence) {
codeBlockEnd = fence
if (js) {
state = 'js'
output.push('')
} else if (bash) {
state = 'bash'
output.push('await $`')
} else {
state = 'other'
output.push('')
}
break
}
if (tabRe.test(line) && prevLineIsEmpty) {
output.push(line)
state = 'tab'
continue
}
const { fence, js, bash } = line.match(codeBlockRe)?.groups || {}
if (!fence) {
prevLineIsEmpty = line === ''
output.push('// ' + line)
continue
}
codeBlockEnd = fence
if (js) {
state = 'js'
output.push('')
} else if (bash) {
state = 'bash'
output.push('await $`')
} else {
state = 'other'
output.push('')
}
break
prevLineIsEmpty = line === ''
output.push('// ' + line)
continue
case 'tab':
if (line === '') {
output.push('')
Expand All @@ -60,23 +62,23 @@ export function transformMarkdown(buf: Buffer | string): string {
}
break
case 'js':
if (line === codeBlockEnd) {
if (line.replace(/^ {0,3}/, '') === codeBlockEnd) {
output.push('')
state = 'root'
} else {
output.push(line)
}
break
case 'bash':
if (line === codeBlockEnd) {
if (line.replace(/^ {0,3}/, '') === codeBlockEnd) {
output.push('`')
state = 'root'
} else {
output.push(line)
}
break
case 'other':
if (line === codeBlockEnd) {
if (line.replace(/^ {0,3}/, '') === codeBlockEnd) {
output.push('')
state = 'root'
} else {
Expand Down
50 changes: 50 additions & 0 deletions test/md.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,54 @@ echo foo
//
// `)
})

test('transformMarkdown() handles indented code fences in list items (#1389)', () => {
// Code fences indented with up to 3 spaces (e.g. inside list items) should
// be recognized as fenced code blocks, not as tab-indented code.
const input = [
'# h1',
'',
'paragraph',
'',
'## h2',
'',
'### h3',
'',
'```bash',
'echo "1"',
'```',
'',
'### h3',
'',
'- item 1',
'',
'',
' ```bash',
' echo "2"',
' ```',
'',
'',
'### h3',
'',
'```bash',
'echo "4"',
'```',
].join('\n')

const result = transformMarkdown(input)

// The indented code fence must produce a valid $`` invocation,
// not raw backticks that cause "$(...) is not a function".
assert.ok(
!result.includes(' ```bash'),
'indented fence opener should not appear as raw code'
)
assert.ok(
result.includes('await $`'),
'indented bash fence should produce await $`'
)
// Verify all three bash blocks are converted
const bashBlocks = result.match(/await \$`/g)
assert.equal(bashBlocks?.length, 3, 'all three bash blocks should be converted')
})
})