Skip to content

Commit 1b61f91

Browse files
committed
Merge branch 'main' into fix/directive-syntax
2 parents ed3988a + bef1b03 commit 1b61f91

File tree

14 files changed

+160
-18
lines changed

14 files changed

+160
-18
lines changed

docs/source/contribute/on-the-web.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
title: Contribute on the web
33
---
44

5-
:::{warning}
6-
This feature is not supported yet. See [#171](https://github.com/elastic/docs-builder/issues/171) for more information.
7-
:::
8-
95
1. On the right side of the page you want to edit, select **Edit this page**.
106
1. Do something on GitHub.
117
1. Take the dog for a walk.
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This snippet is not included anywhere
2+
3+
```{warning}
4+
This snippet is not included anywhere
5+
```

docs/source/syntax/file_inclusion.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ If there are portions of content that are relevant to multiple pages, you can in
1212
### Syntax
1313

1414
```markdown
15-
:::{include} _snippets/my_snippet.md
15+
:::{include} _snippets/reusable-snippet.md
1616
:::
1717
```
1818

19-
:::{include} _snippets/my_snippet.md
19+
:::{include} _snippets/reusable-snippet.md
2020
:::
2121

2222
### Asciidoc syntax

src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ namespace Elastic.Markdown.IO.Discovery;
1010

1111
public record GitCheckoutInformation
1212
{
13+
private string? _repositoryName;
14+
1315
private static GitCheckoutInformation Unavailable { get; } = new()
1416
{
1517
Branch = "unavailable",
1618
Remote = "unavailable",
17-
Ref = "unavailable"
19+
Ref = "unavailable",
20+
RepositoryName = "unavailable"
1821
};
1922

2023
[JsonPropertyName("branch")]
@@ -26,14 +29,27 @@ public record GitCheckoutInformation
2629
[JsonPropertyName("ref")]
2730
public required string Ref { get; init; }
2831

32+
[JsonIgnore]
33+
public string? RepositoryName
34+
{
35+
get => _repositoryName ??= Remote.Split('/').Last();
36+
set => _repositoryName = value;
37+
}
38+
2939
// manual read because libgit2sharp is not yet AOT ready
3040
public static GitCheckoutInformation Create(IFileSystem fileSystem)
3141
{
3242
// filesystem is not real so return a dummy
3343
if (fileSystem is not FileSystem)
3444
{
3545
var fakeRef = Guid.NewGuid().ToString().Substring(0, 16);
36-
return new GitCheckoutInformation { Branch = $"test-{fakeRef}", Remote = "elastic/docs-builder", Ref = fakeRef, };
46+
return new GitCheckoutInformation
47+
{
48+
Branch = $"test-{fakeRef}",
49+
Remote = "elastic/docs-builder",
50+
Ref = fakeRef,
51+
RepositoryName = "docs-builder"
52+
};
3753
}
3854

3955
var gitConfig = Git(".git/config");
@@ -66,7 +82,15 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem)
6682
if (string.IsNullOrEmpty(remote))
6783
remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY") ?? "elastic/docs-builder-unknown";
6884

69-
return new GitCheckoutInformation { Ref = gitRef, Branch = branch, Remote = remote };
85+
remote = remote.AsSpan().TrimEnd(".git").ToString();
86+
87+
return new GitCheckoutInformation
88+
{
89+
Ref = gitRef,
90+
Branch = branch,
91+
Remote = remote,
92+
RepositoryName = remote.Split('/').Last()
93+
};
7094

7195
IFileInfo Git(string path) => fileSystem.FileInfo.New(Path.Combine(Paths.Root.FullName, path));
7296

@@ -80,7 +104,7 @@ string BranchTrackingRemote(string b, IniFile c)
80104
if (!sections.Contains(branchSection))
81105
return string.Empty;
82106

83-
var remoteName = ini.GetSetting(branchSection, "remote");
107+
var remoteName = ini.GetSetting(branchSection, "remote")?.Trim();
84108

85109
var remoteSection = $"remote \"{remoteName}\"";
86110

src/Elastic.Markdown/IO/DocumentationFile.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ public record StaticFile(IFileInfo SourceFile, IDirectoryInfo RootPath)
1919

2020
public record ExcludedFile(IFileInfo SourceFile, IDirectoryInfo RootPath)
2121
: DocumentationFile(SourceFile, RootPath);
22+
23+
public record SnippetFile(IFileInfo SourceFile, IDirectoryInfo RootPath)
24+
: DocumentationFile(SourceFile, RootPath);

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class DocumentationSet
2020
public IDirectoryInfo SourcePath { get; }
2121
public IDirectoryInfo OutputPath { get; }
2222

23+
public string RelativeSourcePath { get; }
24+
2325
public DateTimeOffset LastWrite { get; }
2426

2527
public ConfigurationFile Configuration { get; }
@@ -31,6 +33,7 @@ public DocumentationSet(BuildContext context)
3133
Context = context;
3234
SourcePath = context.SourcePath;
3335
OutputPath = context.OutputPath;
36+
RelativeSourcePath = Path.GetRelativePath(Paths.Root.FullName, SourcePath.FullName);
3437
Configuration = new ConfigurationFile(context.ConfigurationPath, SourcePath, context);
3538

3639
MarkdownParser = new MarkdownParser(SourcePath, context, GetMarkdownFile, Configuration);
@@ -87,6 +90,11 @@ private DocumentationFile CreateMarkDownFile(IFileInfo file, BuildContext contex
8790
if (Configuration.Globs.Any(g => g.IsMatch(relativePath)))
8891
return new MarkdownFile(file, SourcePath, MarkdownParser, context);
8992

93+
// we ignore files in folders that start with an underscore
94+
if (relativePath.IndexOf("_snippets", StringComparison.Ordinal) >= 0)
95+
return new SnippetFile(file, SourcePath);
96+
97+
// we ignore files in folders that start with an underscore
9098
if (relativePath.IndexOf("/_", StringComparison.Ordinal) > 0 || relativePath.StartsWith("_"))
9199
return new ExcludedFile(file, SourcePath);
92100

src/Elastic.Markdown/Myst/Directives/IncludeBlock.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,24 @@ private void ExtractInclusionPath(ParserContext context)
7171
Found = true;
7272
else
7373
this.EmitError($"`{IncludePath}` does not exist.");
74+
75+
// literal includes may point to locations other than `_snippets` since they do not
76+
// participate in emitting links
77+
if (Literal)
78+
return;
79+
80+
var file = FileSystem.FileInfo.New(IncludePath);
81+
82+
if (file.Directory != null && file.Directory.FullName.IndexOf("_snippets", StringComparison.Ordinal) < 0)
83+
{
84+
this.EmitError($"{{include}} only supports including snippets from `_snippet` folders. `{IncludePath}` is not a snippet");
85+
Found = false;
86+
}
87+
88+
if (file.FullName == context.Path.FullName)
89+
{
90+
this.EmitError($"{{include}} cyclical include detected `{IncludePath}` points to itself");
91+
Found = false;
92+
}
7493
}
7594
}

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public async Task<string> RenderLayout(MarkdownFile markdown, Cancel ctx = defau
4747

4848
var previous = DocumentationSet;
4949

50+
var remote = DocumentationSet.Context.Git.RepositoryName;
51+
var branch = DocumentationSet.Context.Git.Branch;
52+
var path = Path.Combine(DocumentationSet.RelativeSourcePath, markdown.RelativePath);
53+
var editUrl = $"https://github.com/elastic/{remote}/edit/{branch}/{path}";
54+
5055
var slice = Index.Create(new IndexViewModel
5156
{
5257
Title = markdown.Title ?? "[TITLE NOT SET]",
@@ -56,7 +61,8 @@ public async Task<string> RenderLayout(MarkdownFile markdown, Cancel ctx = defau
5661
CurrentDocument = markdown,
5762
NavigationHtml = navigationHtml,
5863
UrlPathPrefix = markdown.UrlPathPrefix,
59-
Applies = markdown.YamlFrontMatter?.AppliesTo
64+
Applies = markdown.YamlFrontMatter?.AppliesTo,
65+
GithubEditUrl = editUrl
6066
});
6167
return await slice.RenderAsync(cancellationToken: ctx);
6268
}

src/Elastic.Markdown/Slices/Index.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
CurrentDocument = Model.CurrentDocument,
1010
NavigationHtml = Model.NavigationHtml,
1111
UrlPathPrefix = Model.UrlPathPrefix,
12+
GithubEditUrl = Model.GithubEditUrl
1213
};
1314
}
1415
<section id="elastic-docs-v3">

0 commit comments

Comments
 (0)