Skip to content

Commit 34cb5cd

Browse files
authored
Add support for multiple line comments (#940)
* Add support for comment blocks spanning multiple lines * Improve documentation on the feature
1 parent fb0e18f commit 34cb5cd

File tree

4 files changed

+139
-3
lines changed

4 files changed

+139
-3
lines changed

docs/syntax/comments.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Comments
22

3-
Elastic docs V3 currently only supports single-line comments.
4-
53
## Single line comments
64

75
Use `%` to add single-line comments.
@@ -10,4 +8,17 @@ Use `%` to add single-line comments.
108
% This is a comment
119
```
1210

13-
Make sure to add a space after the `%`.
11+
Make sure to add a space after the `%`.
12+
13+
## Multiline comments
14+
15+
Use `<!--` and `-->` to add multiple line comment blocks.
16+
17+
```markdown
18+
- There is a commented section below.
19+
<!--
20+
This section should not appear -
21+
Neither should this line.
22+
-->
23+
- And there is a commented section above.
24+
```

src/Elastic.Markdown/Myst/Comments/CommentMarkdownExtension.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public void Setup(MarkdownPipelineBuilder pipeline)
2222
{
2323
if (!pipeline.BlockParsers.Contains<CommentBlockParser>())
2424
_ = pipeline.BlockParsers.InsertBefore<ThematicBreakParser>(new CommentBlockParser());
25+
if (!pipeline.BlockParsers.Contains<MultipleLineCommentBlockParser>())
26+
_ = pipeline.BlockParsers.InsertBefore<ThematicBreakParser>(new MultipleLineCommentBlockParser());
2527
}
2628

2729
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Diagnostics;
6+
using Markdig.Parsers;
7+
using Markdig.Syntax;
8+
9+
namespace Elastic.Markdown.Myst.Comments;
10+
11+
[DebuggerDisplay("{GetType().Name} Line: {Line}, {Lines}")]
12+
public class MultipleLineCommentBlock(BlockParser parser) : LeafBlock(parser);
13+
14+
public class MultipleLineCommentBlockParser : BlockParser
15+
{
16+
public MultipleLineCommentBlockParser() => OpeningCharacters = ['<'];
17+
18+
private const string BlockStart = "<!--";
19+
private const string BlockEnd = "-->";
20+
21+
public override BlockState TryOpen(BlockProcessor processor)
22+
{
23+
var currentLine = processor.Line;
24+
if (currentLine.Match(BlockStart))
25+
{
26+
var block = new MultipleLineCommentBlock(this)
27+
{
28+
Column = processor.Column,
29+
Span =
30+
{
31+
Start = processor.Start
32+
}
33+
};
34+
processor.NewBlocks.Push(block);
35+
processor.GoToColumn(currentLine.End);
36+
return BlockState.Continue;
37+
}
38+
return BlockState.None;
39+
}
40+
41+
public override BlockState TryContinue(BlockProcessor processor, Block block)
42+
{
43+
var currentLine = processor.Line;
44+
45+
if (!currentLine.Match(BlockEnd))
46+
return BlockState.Continue;
47+
48+
block.UpdateSpanEnd(currentLine.End);
49+
return BlockState.BreakDiscard;
50+
}
51+
}

tests/Elastic.Markdown.Tests/Inline/CommentTest.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,75 @@ public void GeneratesAttributesInHtml() =>
2727
"""
2828
);
2929
}
30+
31+
public class MultipleLineCommentTest(ITestOutputHelper output) : InlineTest(output,
32+
"""
33+
not a comment, and multi line comment below
34+
<!--
35+
multi line comment
36+
Another line inside the commented area
37+
end of comments
38+
-->
39+
40+
also not a comment
41+
"""
42+
)
43+
{
44+
45+
[Fact]
46+
public void GeneratesAttributesInHtml() =>
47+
// language=html
48+
Html.ReplaceLineEndings().Should().NotContainAny(
49+
"<p><!--",
50+
"<p>Multi line comment, first line",
51+
"<p>Another line inside the commented area",
52+
"<p>end of comments",
53+
"<p>-->")
54+
.And.ContainAll(
55+
"<p>not a comment, and multi line comment below</p>",
56+
"<p>also not a comment</p>"
57+
).And.Be(
58+
"""
59+
<p>not a comment, and multi line comment below</p>
60+
<p>also not a comment</p>
61+
""".ReplaceLineEndings()
62+
);
63+
}
64+
65+
public class MultipleLineCommentWithLinkTest(ITestOutputHelper output) : InlineTest(output,
66+
"""
67+
not a comment, and multi line comment below
68+
<!--
69+
multi line comment
70+
[regular link](http://elastic.co/non-existing-link)
71+
[global search field]({{this-variable-does-not-exist}}/introduction.html)
72+
end of comments
73+
-->
74+
75+
also not a comment
76+
"""
77+
)
78+
{
79+
[Fact]
80+
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
81+
82+
[Fact]
83+
public void GeneratesAttributesInHtml() =>
84+
// language=html
85+
Html.ReplaceLineEndings().Should().NotContainAny(
86+
"<p><!--",
87+
"<p>Multi line comment, first line",
88+
"regular link",
89+
"global search field",
90+
"<p>end of comments",
91+
"<p>-->")
92+
.And.ContainAll(
93+
"<p>not a comment, and multi line comment below</p>",
94+
"<p>also not a comment</p>"
95+
).And.Be(
96+
"""
97+
<p>not a comment, and multi line comment below</p>
98+
<p>also not a comment</p>
99+
""".ReplaceLineEndings()
100+
);
101+
}

0 commit comments

Comments
 (0)