Skip to content

Commit 72cca15

Browse files
committed
added tests linking to inline anchors
1 parent 8a702d1 commit 72cca15

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/Elastic.Markdown/IO/MarkdownFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void ReadDocumentInstructions(MarkdownDocument document)
170170
.Select(s => s.Slugify())
171171
.Concat(document.Descendants<InlineAnchor>().Select(a=>a.Anchor))
172172
.ToArray();
173-
173+
174174
foreach (var label in labels)
175175
{
176176
if (!string.IsNullOrEmpty(label))

src/Elastic.Markdown/Myst/MarkdownParser.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class MarkdownParser(
3131
public static MarkdownPipeline MinimalPipeline { get; } =
3232
new MarkdownPipelineBuilder()
3333
.UseYamlFrontMatter()
34+
.UseInlineAnchors()
3435
.UseHeadingsWithSlugs()
3536
.UseDirectives()
3637
.Build();

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.IO.Abstractions.TestingHelpers;
56
using Elastic.Markdown.Myst.InlineParsers;
67
using FluentAssertions;
8+
using JetBrains.Annotations;
79
using Markdig.Syntax;
10+
using Markdig.Syntax.Inlines;
811
using Xunit.Abstractions;
912

1013
namespace Elastic.Markdown.Tests.Inline;
@@ -135,3 +138,74 @@ public void GeneratesAttributesInHtml() =>
135138
""".TrimEnd()
136139
);
137140
}
141+
142+
143+
public abstract class InlineAnchorLinkTestBase(ITestOutputHelper output, [LanguageInjection("markdown")] string content)
144+
: InlineTest<LinkInline>(output,
145+
$"""
146+
## Hello world
147+
148+
A paragraph
149+
150+
{content}
151+
152+
$$$same-page-anchor$$$
153+
154+
""")
155+
{
156+
protected override void AddToFileSystem(MockFileSystem fileSystem)
157+
{
158+
// language=markdown
159+
var inclusion =
160+
"""
161+
# Special Requirements
162+
163+
## Sub Requirements
164+
165+
To follow this tutorial you will need to install the following components:
166+
167+
## New Requirements [#new-reqs]
168+
169+
These are new requirements
170+
171+
With a custom anchor that exists temporarily. $$$custom-anchor$$$
172+
""";
173+
fileSystem.AddFile(@"docs/testing/req.md", inclusion);
174+
fileSystem.AddFile(@"docs/_static/img/observability.png", new MockFileData(""));
175+
}
176+
177+
}
178+
179+
public class InlineAnchorCanBeLinkedToo(ITestOutputHelper output) : InlineAnchorLinkTestBase(output,
180+
"""
181+
[Hello](#same-page-anchor)
182+
"""
183+
)
184+
{
185+
[Fact]
186+
public void GeneratesHtml() =>
187+
// language=html
188+
Html.Should().Contain(
189+
"""<p><a href="#same-page-anchor">Hello</a></p>"""
190+
);
191+
192+
[Fact]
193+
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
194+
}
195+
196+
public class ExternalPageInlineAnchorCanBeLinkedToo(ITestOutputHelper output) : InlineAnchorLinkTestBase(output,
197+
"""
198+
[Sub Requirements](testing/req.md#custom-anchor)
199+
"""
200+
)
201+
{
202+
[Fact]
203+
public void GeneratesHtml() =>
204+
// language=html
205+
Html.Should().Contain(
206+
"""<p><a href="testing/req.html#custom-anchor">Sub Requirements</a></p>"""
207+
);
208+
209+
[Fact]
210+
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
211+
}

0 commit comments

Comments
 (0)