diff --git a/docs/source/testing/index.md b/docs/source/testing/index.md index 0f334219..7535aa86 100644 --- a/docs/source/testing/index.md +++ b/docs/source/testing/index.md @@ -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) \ No newline at end of file diff --git a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs index f40e7cfb..b6d8472a 100644 --- a/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs +++ b/src/Elastic.Markdown/Myst/InlineParsers/DiagnosticLinkInlineParser.cs @@ -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; @@ -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; diff --git a/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs b/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs index a4ff9f53..3a90cf82 100644 --- a/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs +++ b/tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs @@ -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(""" +

Links:

+ + + """); + + [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); +}