Skip to content

Commit

Permalink
fix: don't split codeblocks with blank lines (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 22, 2021
1 parent 1fd2b35 commit 85feb2f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/loader/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,19 @@ export default function babelPluginUntyped () {
}
}

function containsIncompleteCodeblock (line = '') {
const codeDelimiters = line.split('\n').filter(line => line.startsWith('```')).length
return !!(codeDelimiters % 2)
}

function clumpLines (lines: string[], delimiters = [], separator = ' ') {
const clumps: string[] = []
while (lines.length) {
const line = lines.shift()
if (line && !delimiters.includes(line[0]) && clumps.length && clumps[clumps.length - 1]) {
if (
(line && !delimiters.includes(line[0]) && clumps[clumps.length - 1]) ||
containsIncompleteCodeblock(clumps[clumps.length - 1])
) {
clumps[clumps.length - 1] += separator + line
} else {
clumps.push(line)
Expand Down
28 changes: 28 additions & 0 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,34 @@ describe('transform (jsdoc)', () => {
}
})
})

it('does not split within a codeblock', () => {
const result = transform(`
export default {
/**
* @example
* \`\`\`js
* export default secretNumber = 42
*
* export default nothing = null
* \`\`\`
*/
srcDir: 'src'
}
`)
expectCodeToMatch(result, /export default ([\s\S]*)$/, {
srcDir: {
$default: 'src',
$schema: {
title: '',
description: '',
tags: [
'@example\n```js\nexport default secretNumber = 42\n\nexport default nothing = null\n```'
]
}
}
})
})
})

function expectCodeToMatch (code: string, pattern: RegExp, expected: any) {
Expand Down

0 comments on commit 85feb2f

Please sign in to comment.