Skip to content

Commit

Permalink
Don't validate commented links (#212)
Browse files Browse the repository at this point in the history
Closes #132
  • Loading branch information
reakaleek authored Jan 15, 2025
1 parent a0c1284 commit b344877
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/source/testing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ The files in this directory are used for testing purposes. Do not edit these fil


###### [#synthetics-config-file]

% [Non Existing Link](./non-existing.md)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.IO;
using Elastic.Markdown.Myst.Comments;
using Markdig;
using Markdig.Helpers;
using Markdig.Parsers;
Expand Down Expand Up @@ -46,12 +47,16 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
if (processor.Inline is not LinkInline link)
return match;

// Links in comments should not be validated
// This works for the current test cases, but we might need to revisit this in case it needs some traversal
if (link.Parent?.ParentBlock is CommentBlock)
return match;

var url = link.Url;
var line = link.Line + 1;
var column = link.Column;
var length = url?.Length ?? 1;


var context = processor.GetContext();
if (processor.GetContext().SkipValidation)
return match;
Expand Down
56 changes: 56 additions & 0 deletions tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,59 @@ public void HasWarnings()
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.");
}
}

public class CommentedNonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
"""
% [Non Existing Link](/non-existing.md)
"""
)
{
[Fact]
public void GeneratesHtml() =>
// language=html
Html.Should().BeNullOrWhiteSpace();

[Fact]
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(0);
}

public class CommentedNonExistingLinks2(ITestOutputHelper output) : LinkTestBase(output,
"""
% Hello, this is a [Non Existing Link](/non-existing.md).
Links:
- [](/testing/req.md)
% - [Non Existing Link](/non-existing.md)
- [](/testing/req.md)
"""
)
{
[Fact]
public void GeneratesHtml() =>
// language=html
Html.TrimEnd().Should().Be("""
<p>Links:</p>
<ul>
<li> <a href="/testing/req.html">Special Requirements</a></li>
</ul>
<ul>
<li> <a href="/testing/req.html">Special Requirements</a></li>
</ul>
""");

[Fact]
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(0);
}

public class NonExistingLinkShouldFail(ITestOutputHelper output) : LinkTestBase(output,
"""
[Non Existing Link](/non-existing.md)
- [Non Existing Link](/non-existing.md)
This is another [Non Existing Link](/non-existing.md)
% This is a commented [Non Existing Link](/non-existing.md)
"""
)
{

[Fact]
public void HasErrors() => Collector.Diagnostics.Should().HaveCount(3);
}

0 comments on commit b344877

Please sign in to comment.