Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ private static void ProcessLinkText(InlineProcessor processor, LinkInline link,

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

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

var title = markdown.Title;
var title = markdown?.Title;

if (!string.IsNullOrEmpty(anchor))
{
ValidateAnchor(processor, markdown, anchor, link);
if (link.FirstChild == null && markdown.TableOfContents.TryGetValue(anchor, out var heading))
if (markdown is not null)
ValidateAnchor(processor, markdown, anchor, link);
if (link.FirstChild == null && (markdown?.TableOfContents.TryGetValue(anchor, out var heading) ?? false))
title += " > " + heading.Heading;
}

Expand Down
20 changes: 19 additions & 1 deletion tests/Elastic.Markdown.Tests/Inline/InlineLinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System.IO.Abstractions.TestingHelpers;
using Elastic.Markdown.Diagnostics;
using FluentAssertions;
using JetBrains.Annotations;
using Markdig.Syntax.Inlines;
Expand Down Expand Up @@ -177,11 +178,28 @@ public void GeneratesHtml() =>
public void HasWarnings()
{
Collector.Diagnostics.Should().HaveCount(1);
Collector.Diagnostics.First().Severity.Should().Be(Diagnostics.Severity.Warning);
Collector.Diagnostics.First().Severity.Should().Be(Severity.Warning);
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 NonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
"""
[Non Existing Link](/non-existing.md)
"""
)
{
[Fact]
public void HasErrors() => Collector.Diagnostics
.Where(d => d.Severity == Severity.Error)
.Should().HaveCount(1);

[Fact]
public void HasNoWarning() => Collector.Diagnostics
.Where(d => d.Severity == Severity.Warning)
.Should().HaveCount(0);
}

public class CommentedNonExistingLinks(ITestOutputHelper output) : LinkTestBase(output,
"""
% [Non Existing Link](/non-existing.md)
Expand Down