Skip to content

Commit

Permalink
format comments (markdoc#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfix-stripe authored Sep 26, 2022
1 parent bd3eb9a commit aadfd25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ break
Markdoc uses…
`;

const tokenizer = new Markdoc.Tokenizer({ allowComments: true });

function check(source, expected, options = {}) {
const a = expected?.trimStart();
const b = format(Markdoc.parse(source), options);
const b = format(Markdoc.parse(tokenizer.tokenize(source)), options);
// console.log(a, b);
const d = diff(a, b);
if (d && d.includes('Compared values have no visual difference.')) return;
Expand All @@ -103,6 +105,25 @@ describe('Formatter', () => {
stable(expected);
});

it('comments', () => {
const source = `<!--
comment -->
<!-- comment
with more
than one
line -->
`;
const expected = `<!-- comment -->
<!-- comment
with more
than one
line -->
`;

check(source, expected);
});

it('frontmatter', () => {
const source = `---
title: Title
Expand Down
4 changes: 4 additions & 0 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ function* formatNode(n: Node, o: Options = {}) {
yield* formatChildren(n, no);
break;
}
case 'comment': {
yield '<!-- ' + n.attributes.content + ' -->\n';
break;
}
case 'error':
case 'node':
break;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type LocationEdge = {
export type NodeType =
| 'blockquote'
| 'code'
| 'comment'
| 'document'
| 'em'
| 'error'
Expand Down

0 comments on commit aadfd25

Please sign in to comment.