Skip to content

Commit b344877

Browse files
authored
Don't validate commented links (#212)
Closes #132
1 parent a0c1284 commit b344877

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

docs/source/testing/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ The files in this directory are used for testing purposes. Do not edit these fil
66

77

88
###### [#synthetics-config-file]
9+
10+
% [Non Existing Link](./non-existing.md)

src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Immutable;
66
using Elastic.Markdown.Diagnostics;
77
using Elastic.Markdown.IO;
8+
using Elastic.Markdown.Myst.Comments;
89
using Markdig;
910
using Markdig.Helpers;
1011
using Markdig.Parsers;
@@ -46,12 +47,16 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
4647
if (processor.Inline is not LinkInline link)
4748
return match;
4849

50+
// Links in comments should not be validated
51+
// This works for the current test cases, but we might need to revisit this in case it needs some traversal
52+
if (link.Parent?.ParentBlock is CommentBlock)
53+
return match;
54+
4955
var url = link.Url;
5056
var line = link.Line + 1;
5157
var column = link.Column;
5258
var length = url?.Length ?? 1;
5359

54-
5560
var context = processor.GetContext();
5661
if (processor.GetContext().SkipValidation)
5762
return match;

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,59 @@ public void HasWarnings()
195195
Collector.Diagnostics.First().Message.Should().Contain("The url contains a template expression. Please do not use template expressions in links. See https://github.com/elastic/docs-builder/issues/182 for further information.");
196196
}
197197
}
198+
199+
public class CommentedNonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
200+
"""
201+
% [Non Existing Link](/non-existing.md)
202+
"""
203+
)
204+
{
205+
[Fact]
206+
public void GeneratesHtml() =>
207+
// language=html
208+
Html.Should().BeNullOrWhiteSpace();
209+
210+
[Fact]
211+
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(0);
212+
}
213+
214+
public class CommentedNonExistingLinks2(ITestOutputHelper output) : LinkTestBase(output,
215+
"""
216+
% Hello, this is a [Non Existing Link](/non-existing.md).
217+
Links:
218+
- [](/testing/req.md)
219+
% - [Non Existing Link](/non-existing.md)
220+
- [](/testing/req.md)
221+
"""
222+
)
223+
{
224+
[Fact]
225+
public void GeneratesHtml() =>
226+
// language=html
227+
Html.TrimEnd().Should().Be("""
228+
<p>Links:</p>
229+
<ul>
230+
<li> <a href="/testing/req.html">Special Requirements</a></li>
231+
</ul>
232+
<ul>
233+
<li> <a href="/testing/req.html">Special Requirements</a></li>
234+
</ul>
235+
""");
236+
237+
[Fact]
238+
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(0);
239+
}
240+
241+
public class NonExistingLinkShouldFail(ITestOutputHelper output) : LinkTestBase(output,
242+
"""
243+
[Non Existing Link](/non-existing.md)
244+
- [Non Existing Link](/non-existing.md)
245+
This is another [Non Existing Link](/non-existing.md)
246+
% This is a commented [Non Existing Link](/non-existing.md)
247+
"""
248+
)
249+
{
250+
251+
[Fact]
252+
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(3);
253+
}

0 commit comments

Comments
 (0)