Skip to content

Commit

Permalink
fix(#74): headings are not picked up after a one-line code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
cqroot committed Jan 19, 2023
1 parent 9db9df1 commit 7442fe4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
52 changes: 52 additions & 0 deletions src/markdownHeaders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable no-undef */
import markdownHeaders from './markdownHeaders';

test('get markdown headers with backticks', () => {
const headersWithBackticks = markdownHeaders(`# heading 1
\`plaintext
\`\`\`one line 3ticks\`\`\`
plaintext
# heading 1
plaintext
\`inline one-tick\`
\`\`\`one line 3ticks\`\`\`
# heading 1
\`\`\`bash
# heading 1
\`\`\`
# heading 1
\`\`\`\`\`\`
# heading 1
\`\`\`\`\`\`
# heading 1
\`\`\`
# heading 1
\`\`\`\`\`\`
# heading 1
\`\`\`\`\`\`
# heading 1
\`\`\`
# heading 1
plaintext`);

expect(headersWithBackticks.length).toBe(7);
expect(headersWithBackticks[0].lineno).toBe(0);
expect(headersWithBackticks[1].lineno).toBe(6);
expect(headersWithBackticks[2].lineno).toBe(11);
expect(headersWithBackticks[3].lineno).toBe(17);
expect(headersWithBackticks[4].lineno).toBe(23);
expect(headersWithBackticks[5].lineno).toBe(29);
expect(headersWithBackticks[6].lineno).toBe(35);
});
8 changes: 5 additions & 3 deletions src/markdownHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const markdownit = require('markdown-it')({ html: true });

function isHeader(line: string, context: any) {
// check code block
if (line.match(/(?:```)/)) {
context.flagBlock = !context.flagBlock;
return false;
if (!line.match(/(?:```)(?:.+?)(?:```)/)) {
if (line.match(/(?:```)/)) {
context.flagBlock = !context.flagBlock;
return false;
}
}
// check comment block
if (line.match(/(?:<!--)/) && !line.match(/(?:-->)/)) {
Expand Down

0 comments on commit 7442fe4

Please sign in to comment.