Skip to content

Commit 9f7256d

Browse files
authored
Remove erroneous warning for link text generation on missing page (#592)
* Remove erroneous warning for link text generation on missing page * dotnet format
1 parent 29e6435 commit 9f7256d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,20 @@ private static void ProcessLinkText(InlineProcessor processor, LinkInline link,
202202

203203
var markdown = context.GetDocumentationFile?.Invoke(file) as MarkdownFile;
204204

205-
if (markdown == null)
205+
if (markdown == null && link.FirstChild == null)
206206
{
207207
processor.EmitWarning(link,
208208
$"'{url}' could not be resolved to a markdown file while creating an auto text link, '{file.FullName}' does not exist.");
209209
return;
210210
}
211211

212-
var title = markdown.Title;
212+
var title = markdown?.Title;
213213

214214
if (!string.IsNullOrEmpty(anchor))
215215
{
216-
ValidateAnchor(processor, markdown, anchor, link);
217-
if (link.FirstChild == null && markdown.TableOfContents.TryGetValue(anchor, out var heading))
216+
if (markdown is not null)
217+
ValidateAnchor(processor, markdown, anchor, link);
218+
if (link.FirstChild == null && (markdown?.TableOfContents.TryGetValue(anchor, out var heading) ?? false))
218219
title += " > " + heading.Heading;
219220
}
220221

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.IO.Abstractions.TestingHelpers;
6+
using Elastic.Markdown.Diagnostics;
67
using FluentAssertions;
78
using JetBrains.Annotations;
89
using Markdig.Syntax.Inlines;
@@ -177,11 +178,28 @@ public void GeneratesHtml() =>
177178
public void HasWarnings()
178179
{
179180
Collector.Diagnostics.Should().HaveCount(1);
180-
Collector.Diagnostics.First().Severity.Should().Be(Diagnostics.Severity.Warning);
181+
Collector.Diagnostics.First().Severity.Should().Be(Severity.Warning);
181182
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.");
182183
}
183184
}
184185

186+
public class NonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
187+
"""
188+
[Non Existing Link](/non-existing.md)
189+
"""
190+
)
191+
{
192+
[Fact]
193+
public void HasErrors() => Collector.Diagnostics
194+
.Where(d => d.Severity == Severity.Error)
195+
.Should().HaveCount(1);
196+
197+
[Fact]
198+
public void HasNoWarning() => Collector.Diagnostics
199+
.Where(d => d.Severity == Severity.Warning)
200+
.Should().HaveCount(0);
201+
}
202+
185203
public class CommentedNonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
186204
"""
187205
% [Non Existing Link](/non-existing.md)

0 commit comments

Comments
 (0)