-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds comment syntax to Markdoc (#198)
* Adds native support for comments * Prettier * Update src/tokenizer/plugins/comments.ts Co-authored-by: Mike Fix <62121649+mfix-stripe@users.noreply.github.com> * Fixes bug in the comment plugin Co-authored-by: Mike Fix <62121649+mfix-stripe@users.noreply.github.com>
- Loading branch information
1 parent
7a2e3f8
commit a18ffcc
Showing
8 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import Tokenizer from '..'; | ||
|
||
describe('MarkdownIt Comments plugin', function () { | ||
const tokenizer = new Tokenizer({ allowComments: true }); | ||
|
||
function parse(example) { | ||
const content = example.replace(/\n\s+/gm, '\n').trim(); | ||
return tokenizer.tokenize(content); | ||
} | ||
|
||
describe('inline comments', function () { | ||
const output = [ | ||
{ type: 'paragraph_open' }, | ||
{ | ||
type: 'inline', | ||
children: [ | ||
{ type: 'text', content: 'this is a test ' }, | ||
{ type: 'comment', content: 'example comment' }, | ||
{ type: 'text', content: ' foo' }, | ||
], | ||
}, | ||
{ type: 'paragraph_close' }, | ||
]; | ||
|
||
it('simple inline comment', function () { | ||
const example = parse(` | ||
this is a test <!-- example comment --> foo | ||
`); | ||
|
||
expect(example).toDeepEqualSubset(output); | ||
}); | ||
|
||
it('inline comment with a newline', function () { | ||
const example = parse(` | ||
this is a test <!-- | ||
example comment | ||
--> foo | ||
`); | ||
|
||
expect(example).toDeepEqualSubset(output); | ||
}); | ||
}); | ||
|
||
describe('block comments', function () { | ||
const output = [ | ||
{ type: 'paragraph_open' }, | ||
{ type: 'inline' }, | ||
{ type: 'paragraph_close' }, | ||
{ type: 'comment', content: 'example comment' }, | ||
{ type: 'paragraph_open' }, | ||
{ type: 'inline', content: 'foo' }, | ||
{ type: 'paragraph_close' }, | ||
]; | ||
|
||
it('simple block comment after a paragraph', function () { | ||
const example = parse(` | ||
this is a test | ||
<!-- | ||
example comment | ||
--> | ||
foo | ||
`); | ||
|
||
expect(example).toDeepEqualSubset(output); | ||
}); | ||
|
||
it('block comment with ending on same line as content', function () { | ||
const example = parse(` | ||
this is a test | ||
<!-- | ||
example comment --> | ||
foo | ||
`); | ||
|
||
expect(example).toDeepEqualSubset(output); | ||
}); | ||
|
||
it('block comment one one line', function () { | ||
const example = parse(` | ||
this is a test | ||
<!-- example comment --> | ||
foo | ||
`); | ||
|
||
expect(example).toDeepEqualSubset(output); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type MarkdownIt from 'markdown-it/lib'; | ||
import type StateBlock from 'markdown-it/lib/rules_block/state_block'; | ||
import type StateInline from 'markdown-it/lib/rules_inline/state_inline'; | ||
|
||
const OPEN = '<!--'; | ||
const CLOSE = '-->'; | ||
|
||
function block( | ||
state: StateBlock, | ||
startLine: number, | ||
endLine: number, | ||
silent: boolean | ||
): boolean { | ||
const start = state.bMarks[startLine] + state.tShift[startLine]; | ||
if (!state.src.startsWith(OPEN, start)) return false; | ||
|
||
const close = state.src.indexOf(CLOSE, start); | ||
|
||
if (!close) return false; | ||
if (silent) return true; | ||
|
||
const content = state.src.slice(start + OPEN.length, close); | ||
const lines = content.split('\n').length; | ||
const token = state.push('comment', '', 0); | ||
token.content = content.trim(); | ||
token.map = [startLine, startLine + lines]; | ||
state.line += lines; | ||
|
||
return true; | ||
} | ||
|
||
function inline(state: StateInline, silent: boolean): boolean { | ||
if (!state.src.startsWith(OPEN, state.pos)) return false; | ||
|
||
const close = state.src.indexOf(CLOSE, state.pos); | ||
|
||
if (!close) return false; | ||
if (silent) return true; | ||
|
||
const content = state.src.slice(state.pos + OPEN.length, close); | ||
const token = state.push('comment', '', 0); | ||
token.content = content.trim(); | ||
state.pos = close + CLOSE.length; | ||
|
||
return true; | ||
} | ||
|
||
export default function plugin(md: MarkdownIt) { | ||
md.block.ruler.before('table', 'comment', block, { alt: ['paragraph'] }); | ||
md.inline.ruler.push('comment', inline); | ||
} |