Skip to content

Commit 506b451

Browse files
committed
Add support for llms.txt
1 parent c926195 commit 506b451

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

MyApp/Configure.Ssg.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void Configure(IWebHostBuilder builder) => builder
5252
pages.LoadFrom("_pages");
5353
videos.LoadFrom("_videos");
5454
AppConfig.Instance.GitPagesBaseUrl ??= ResolveGitBlobBaseUrl(appHost.ContentRootDirectory);
55+
AppConfig.Instance.GitPagesRawBaseUrl ??= ResolveGitRawBlobBaseUrl(appHost.ContentRootDirectory);
5556
},
5657
afterAppHostInit: appHost =>
5758
{
@@ -95,14 +96,34 @@ public void Configure(IWebHostBuilder builder) => builder
9596
}
9697
return null;
9798
}
99+
100+
private string? ResolveGitRawBlobBaseUrl(IVirtualDirectory contentDir)
101+
{
102+
var srcDir = new DirectoryInfo(contentDir.RealPath);
103+
var gitConfig = new FileInfo(Path.Combine(srcDir.Parent!.FullName, ".git", "config"));
104+
if (gitConfig.Exists)
105+
{
106+
var txt = gitConfig.ReadAllText();
107+
var pos = txt.IndexOf("url = ", StringComparison.Ordinal);
108+
if (pos >= 0)
109+
{
110+
var url = txt[(pos + "url = ".Length)..].LeftPart(".git").LeftPart('\n').Trim();
111+
var gitBaseUrl = url.Replace("github.com","raw.githubusercontent.com").CombineWith($"refs/heads/main/{srcDir.Name}");
112+
return gitBaseUrl;
113+
}
114+
}
115+
return null;
116+
}
98117
}
99118

100119
public class AppConfig
101120
{
102121
public static AppConfig Instance { get; } = new();
122+
public string Title { get; set; }
103123
public string LocalBaseUrl { get; set; }
104124
public string PublicBaseUrl { get; set; }
105125
public string? GitPagesBaseUrl { get; set; }
126+
public string? GitPagesRawBaseUrl { get; set; }
106127
}
107128

108129
// Add additional frontmatter info to include

MyApp/Pages/Llms.cshtml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@page "/llms.txt"
2+
@inject MarkdownPages Markdown
3+
@inject AppConfig Config
4+
@attribute [RenderStatic]
5+
@{
6+
Layout = "";
7+
Response.ContentType = "text/plain";
8+
}
9+
# @Config.Title
10+
11+
## Docs
12+
@foreach (var entry in Markdown.Sidebars.OrderBy(x => x.Key))
13+
{
14+
@foreach (var sidebar in entry.Value)
15+
{
16+
<text>
17+
@(sidebar.Text)
18+
</text>
19+
foreach (var item in sidebar.Children.Safe().Where(x => x.Text != null && x.Link != null))
20+
{
21+
var page = Markdown!.GetBySlug(item.Link);
22+
var suffix = !string.IsNullOrEmpty(page?.Title) && item.Text != page.Title ? $": {page.Title}" : "";
23+
<text> - [@item.Text](@(@Config.GitPagesRawBaseUrl.CombineWith($"_pages/").CombineWith(item.Link.LeftPart('#') + ".md")))@Html.Raw(suffix)
24+
</text>
25+
}
26+
}
27+
}

MyApp/appsettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"AllowedHosts": "*",
99
"AppConfig": {
10+
"Title": "ServiceStack",
1011
"LocalBaseUrl": "https://localhost:5002",
1112
"PublicBaseUrl": "https://docs.servicestack.net"
1213
}

0 commit comments

Comments
 (0)