Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const pattNextBarOrTicks = pattern("[^`|\\r\\n]*(?:[|]|`+)");
const pattCaptionStart = pattern("\\^[ \\t]+");
const pattFootnoteStart = pattern("\\[\\^([^\\]]+)\\]:[ \\t\\r\\n]");
const pattThematicBreak = pattern("[-*][ \t]*[-*][ \\t]*[-*][-* \\t]*\\r?\\n");
const pattCommentFence = pattern("(%%%%*)[ \\t]*\\r?\\n");
const pattDivFence = pattern("(::::*)[ \\t]*\\r?\\n");
const pattDivFenceStart = pattern("(::::*)[ \\t]*");
const pattDivFenceEnd = pattern("([\\w_-]*)[ \\t]*\\r?\\n");
Expand Down Expand Up @@ -626,6 +627,50 @@ class EventParser {
}
},

{
name: "fenced_comment",
type: ContentType.Block,
content: ContentType.Text,
continue: (container) => {
const m = this.find(container.extra.closePattern);
if (m) {
container.extra.endFenceStartpos = m.startpos;
container.extra.endFenceEndpos = m.startpos + m.captures[0].length - 1;
this.pos = m.endpos;
this.finishedLine = true;
return false;
} else {
return true;
}
},
open: (spec) => {
const m = this.find(pattCommentFence);
if (m) {
const border = m.captures[0];
const closePattern = pattern("(" + border +
"%*)[ \\t]*[\\r\\n]");
this.addContainer(new Container(spec, { closePattern: closePattern }));
this.addMatch(m.startpos, m.startpos + border.length - 1,
"+comment");
this.pos = m.endpos;
this.finishedLine = true;
return true;
} else {
return false;
}
},
close: () => {
const container = this.containers.pop();
if (!container) return;
const sp = container.extra.endFenceStartpos || this.pos;
const ep = container.extra.endFenceEndpos || this.pos;
this.addMatch(sp, ep, "-comment");
if (sp === ep) {
this.warn(new Warning("Unclosed comment block", this.pos));
}
}
},

{
name: "fenced_div",
type: ContentType.Block,
Expand Down
1 change: 1 addition & 0 deletions src/functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const testfiles = [
"symb.test",
"emphasis.test",
"escapes.test",
"fenced_comments.test",
"fenced_divs.test",
"footnotes.test",
"headings.test",
Expand Down
12 changes: 12 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,18 @@ const parse = function(input: string, options: ParseOptions = {}): Doc {
input.substring(startpos, endpos + 1);
},

["+comment"]: (suffixes, startpos, endpos, pos) => {
pushContainer(pos);
context = Context.Verbatim;
},

["-comment"]: (suffixes, startpos, endpos, pos) => {
popContainer(pos);
// Discard content - comments are not rendered
context = Context.Normal;
accumulatedText = "";
},

["+code_block"]: (suffixes, startpos, endpos, pos) => {
pushContainer(pos);
context = Context.Verbatim;
Expand Down
54 changes: 54 additions & 0 deletions test/fenced_comments.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Basic fenced comment:

```
Before.

%%%
This is a comment.
%%%

After.
.
<p>Before.</p>
<p>After.</p>
```

Fenced comment with blank lines:

```
%%%
First line.

Second line.
%%%
.
```

Fenced comment with longer fence:

```
%%%%
%%% not the end
still inside
%%%%
.
```

Closing fence must match length:

```
%%%%
comment
%%%
still comment
%%%%
.
```

Unclosed fenced comment consumes rest of document:

```
%%%
This comment never closes.
.
```