Skip to content

Commit 5b2d6e8

Browse files
authored
Add auto text link generation for markdown files (#197)
1 parent bef1b03 commit 5b2d6e8

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

docs/source/syntax/applies.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ applies:
4848
Are equivalent, note `all` just means we won't be rendering the version portion in the html.
4949

5050

51-
## This section has its own applies annotations
51+
## This section has its own applies annotations [#sections]
5252

5353
:::{applies}
5454
:stack: unavailable

docs/source/syntax/links.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ Cross links are links that point to a different docset.
4242

4343
The syntax is `<scheme>://<path>`, where <scheme> is the repository name and <path> is the path to the file.
4444

45+
## Auto Text Links
46+
47+
If you link to a local markdown file but omit any link text `docs-builder` will use the target's [title](titles.md).
48+
49+
```markdown
50+
[](applies.md)
51+
```
52+
will output: [](applies.md)
53+
54+
You can go one step further to auto generate link text for headings within files:
55+
56+
```markdown
57+
[](applies.md#sections)
58+
```
59+
60+
will output: [](applies.md#sections)
61+
62+
This also applies to local anchors
63+
64+
65+
```markdown
66+
[](#anchor-link)
67+
```
68+
69+
will output: [](#anchor-link)
70+
4571
## Heading anchors
4672

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

docs/source/testing/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
title: Testing
33
---
44

5-
The files in this directory are used for testing purposes. Do not edit these files unless you are working on tests.
5+
The files in this directory are used for testing purposes. Do not edit these files unless you are working on tests.
6+
7+
8+
###### [#synthetics-config-file]

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,20 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
108108

109109
if (link.FirstChild == null || !string.IsNullOrEmpty(anchor))
110110
{
111-
var file = string.IsNullOrWhiteSpace(url) ? context.Path
112-
: context.Build.ReadFileSystem.FileInfo.New(Path.Combine(context.Build.SourcePath.FullName, url.TrimStart('/')));
111+
var file = string.IsNullOrWhiteSpace(url)
112+
? context.Path
113+
: url.StartsWith('/')
114+
? context.Build.ReadFileSystem.FileInfo.New(Path.Combine(context.Build.SourcePath.FullName, url.TrimStart('/')))
115+
: context.Build.ReadFileSystem.FileInfo.New(Path.Combine(context.Path.Directory!.FullName, url));
113116
var markdown = context.GetDocumentationFile?.Invoke(file) as MarkdownFile;
117+
if (markdown == null)
118+
{
119+
processor.EmitWarning(line,
120+
column,
121+
length,
122+
$"'{url}' could not be resolved to a markdown file while creating an auto text link, '{file.FullName}' does not exist.");
123+
}
124+
114125
var title = markdown?.Title;
115126

116127
if (!string.IsNullOrEmpty(anchor))

0 commit comments

Comments
 (0)