Skip to content

Commit c40049c

Browse files
committed
Add GitHub edit link generation for documentation pages
Generate and include GitHub edit URLs for documentation pages based on the repository, branch, and source path. Update the relevant models, views, and tests to support and display this functionality. This enhances the ability for users to easily contribute edits directly from the rendered documentation pages.
1 parent 552b631 commit c40049c

File tree

8 files changed

+42
-10
lines changed

8 files changed

+42
-10
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.

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

Lines changed: 25 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,12 @@ 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, Branch = branch, Remote = remote, RepositoryName = remote.Split('/').Last()
90+
};
7091

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

@@ -80,7 +101,7 @@ string BranchTrackingRemote(string b, IniFile c)
80101
if (!sections.Contains(branchSection))
81102
return string.Empty;
82103

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

85106
var remoteSection = $"remote \"{remoteName}\"";
86107

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 3 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);

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">

src/Elastic.Markdown/Slices/Layout/_TableOfContents.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ul>
1515
</div>
1616
<div class="edit-this-page">
17-
<a href="https://github.com/elastic/markitpy-samples/blob/master/docs/source/index.md">Edit this page</a>
17+
<a href="@Model.GithubEditUrl">Edit this page</a>
1818
</div>
1919
</div>
2020
</aside>

src/Elastic.Markdown/Slices/_ViewModels.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class IndexViewModel
1616
public required MarkdownFile CurrentDocument { get; init; }
1717
public required string NavigationHtml { get; init; }
1818
public required string? UrlPathPrefix { get; init; }
19+
public required string? GithubEditUrl { get; init; }
1920
public required Deployment? Applies { get; init; }
2021
}
2122

@@ -27,6 +28,7 @@ public class LayoutViewModel
2728
public required MarkdownFile CurrentDocument { get; init; }
2829
public required string NavigationHtml { get; set; }
2930
public required string? UrlPathPrefix { get; set; }
31+
public required string? GithubEditUrl { get; set; }
3032

3133
private MarkdownFile[]? _parents;
3234
public MarkdownFile[] Parents

tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ public void Create()
3434
git.Branch.Should().NotContain(git.Ref);
3535
git.Ref.Should().NotBeNullOrWhiteSpace();
3636
git.Remote.Should().NotBeNullOrWhiteSpace();
37+
git.Remote.Should().NotContain("unknown");
38+
git.RepositoryName.Should().NotContain(".git");
39+
git.Remote.Should().NotContain(".git");
3740
}
3841
}

0 commit comments

Comments
 (0)