Skip to content

Commit 552b631

Browse files
authored
Warn about links with template expressions (#191)
Related #182 ## Details Add warning if a link contains `{{` or `}}` thus using a template expression. Because 1) it doesn't work 2) it's discouraged to use template variables in links
1 parent 4d24a61 commit 552b631

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

docs/source/syntax/links.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ I link to the [Inline link](#inline-link) heading above.
3232
I link to the [Notes](tables.md#notes) heading on the [Tables](tables.md) page.
3333
```
3434

35+
## Cross Links
36+
37+
Cross links are links that point to a different docset.
38+
39+
```markdown
40+
[Cross link](kibana://cross-link.md)
41+
```
42+
43+
The syntax is `<scheme>://<path>`, where <scheme> is the repository name and <path> is the path to the file.
44+
3545
## Heading anchors
3646

3747
Headings will automatically create anchor links in the resulting html.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
6262
return match;
6363
}
6464

65+
if (url.Contains("{{") || url.Contains("}}"))
66+
{
67+
processor.EmitWarning(line, column, length, "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.");
68+
return match;
69+
}
70+
6571
var uri = Uri.TryCreate(url, UriKind.Absolute, out var u) ? u : null;
6672

6773
if (IsCrossLink(uri))

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,25 @@ public void EmitsCrossLink()
173173
Collector.CrossLinks.Should().Contain("kibana://index.md");
174174
}
175175
}
176+
177+
public class LinksWithInterpolationWarning(ITestOutputHelper output) : LinkTestBase(output,
178+
"""
179+
[global search field]({{kibana-ref}}/introduction.html#kibana-navigation-search)
180+
"""
181+
)
182+
{
183+
[Fact]
184+
public void GeneratesHtml() =>
185+
// language=html
186+
Html.Should().Contain(
187+
"""<p><a href="%7B%7Bkibana-ref%7D%7D/introduction.html#kibana-navigation-search">global search field</a></p>"""
188+
);
189+
190+
[Fact]
191+
public void HasWarnings()
192+
{
193+
Collector.Diagnostics.Should().HaveCount(1);
194+
Collector.Diagnostics.First().Severity.Should().Be(Diagnostics.Severity.Warning);
195+
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.");
196+
}
197+
}

0 commit comments

Comments
 (0)