Skip to content

Commit

Permalink
fix: spaces before code block
Browse files Browse the repository at this point in the history
  • Loading branch information
cqroot committed Nov 11, 2023
1 parent 1439c25 commit 56e8840
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/markdownHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ test('headers after code highlighting', () => {
slug: 'like-this',
});
});

test('spaces before code block', () => {
const headers = markdownHeaders(
readFileSync('./test/86-spaces_before_code_block.md', 'utf-8'),
);
expect(headers.length).toBe(2);
expect(headers[0]).toEqual({
html: 'spaces before code blocks',
level: 1,
lineno: 0,
number: '1',
slug: 'spaces-before-code-blocks',
});
expect(headers[1]).toEqual({
html: 'Comment',
level: 1,
lineno: 13,
number: '2',
slug: 'comment',
});
});
4 changes: 3 additions & 1 deletion src/markdownHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const markdownit = require('markdown-it')({ html: true })
function isHeader(line: string, context: any) {
// check code block
if (!line.match(/(?:```)(?:.+?)(?:```)/)) {
if (line.match(/(?:^```)/)) {
if (line.match(/(?:^\s{0,3}```)/)) {
context.flagBlock = !context.flagBlock;
return false;
}
}

// check comment block
if (line.match(/(?:<!--)/) && !line.match(/(?:-->)/)) {
context.flagComment = true;
Expand All @@ -21,6 +22,7 @@ function isHeader(line: string, context: any) {
context.flagComment = false;
return false;
}

if (context.flagBlock || context.flagComment) return false;

if (!line.match(/^ {0,3}#/)) return false;
Expand Down
17 changes: 17 additions & 0 deletions test/86-spaces_before_code_block.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# spaces before code block

```bash
# Comment
echo 1
```

```bash
# Comment
echo 1
```

```bash
# Comment
echo 1
```

0 comments on commit 56e8840

Please sign in to comment.