Repository navigation
Expand file tree
/
Copy pathMarkdown.Pages.cs
More file actions
137 lines (119 loc) · 5.36 KB
/
Copy pathMarkdown.Pages.cs
File metadata and controls
137 lines (119 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// run node postinstall.js to update to latest version
using ServiceStack.IO;
namespace MyApp;
public class MarkdownPages(ILogger<MarkdownPages> log, IWebHostEnvironment env, IVirtualFiles fs)
: MarkdownPagesBase<MarkdownFileInfo>(log, env, fs)
{
public override string Id => "pages";
public virtual string? DefaultMenuIcon { get; set; } =
"<svg class='h-6 w-6 shrink-0 text-sky-500' fill='none' viewBox='0 0 24 24' stroke-width='1.5' stroke='currentColor' aria-hidden='true'><path stroke-linecap='round' stroke-linejoin='round' d='M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25'></path></svg>";
List<MarkdownFileInfo> Pages { get; set; } = new();
public List<MarkdownFileInfo> GetVisiblePages(string? prefix=null, bool allDirectories=false) => prefix == null
? Pages.Where(x => IsVisible(x) && !x.Slug!.Contains('/')).OrderBy(x => x.Order).ThenBy(x => x.Path).ToList()
: Pages.Where(x => IsVisible(x) && x.Slug!.StartsWith(prefix.WithTrailingSlash()))
.Where(x => allDirectories || (x.Slug.CountOccurrencesOf('/') == prefix.CountOccurrencesOf('/') + 1))
.OrderBy(x => x.Order).ThenBy(x => x.Path).ToList();
public MarkdownFileInfo? GetBySlug(string slug)
{
slug = slug.Trim('/');
return Fresh(Pages.Where(IsVisible).FirstOrDefault(x => x.Slug == slug));
}
public Dictionary<string, List<MarkdownMenu>> Sidebars { get; set; } = new();
public void LoadFrom(string fromDirectory)
{
Sidebars.Clear();
Pages.Clear();
var files = VirtualFiles.GetDirectory(fromDirectory).GetAllFiles()
.OrderBy(x => x.VirtualPath)
.ToList();
log.LogInformation("Found {Count} pages", files.Count);
var pipeline = CreatePipeline(string.Empty);
foreach (var file in files)
{
try
{
if (file.Extension == "md")
{
var doc = Load(file.VirtualPath, pipeline);
if (doc == null)
continue;
var relativePath = file.VirtualPath[(fromDirectory.Length + 1)..];
if (relativePath.IndexOf('/') >= 0)
{
doc.Slug = relativePath.LastLeftPart('/') + '/' + doc.Slug;
}
Pages.Add(doc);
}
else if (file.Name == "sidebar.json")
{
var virtualPath = file.VirtualPath.Substring(fromDirectory.Length);
var folder = virtualPath[..^"sidebar.json".Length].Trim('/');
var sidebarJson = file.ReadAllText();
var sidebar = sidebarJson.FromJson<List<MarkdownMenu>>();
// If first entry is home and icon is not provided or '' use DefaultMenuIcon
var defaultMenu = sidebar.FirstOrDefault();
if (defaultMenu?.Link?.Trim('/') == folder && defaultMenu.Icon == null)
{
defaultMenu.Icon = DefaultMenuIcon;
}
Sidebars[folder] = sidebar;
}
}
catch (Exception e)
{
log.LogError(e, "Couldn't load {VirtualPath}: {Message}", file.VirtualPath, e.Message);
}
}
if (Sidebars.Count > 0)
{
log.LogInformation("Loaded {Count} sidebars: {Sidebars}", Sidebars.Count, Sidebars.Keys.Join(", "));
}
}
public override List<MarkdownFileBase> GetAll() => Pages.Where(IsVisible).Map(doc => ToMetaDoc(doc, x => x.Url = $"/{x.Slug}"));
public virtual List<MarkdownMenu> GetSidebar(string folder, MarkdownMenu? defaultMenu=null)
{
if (Sidebars.TryGetValue(folder, out var sidebar))
return sidebar;
var allPages = GetVisiblePages(folder);
var allGroups = allPages.Select(x => x.Group)
.Distinct()
.OrderBy(x => x)
.ToList();
sidebar = new List<MarkdownMenu>();
foreach (var group in allGroups)
{
MarkdownMenu? menuItem;
if (group == null)
{
menuItem = defaultMenu ?? new MarkdownMenu {
Children = [],
};
}
else
{
menuItem = new() {
Text = group
};
}
sidebar.Add(menuItem);
foreach (var page in allPages.Where(x => x.Group == group).OrderBy(x => x.Order))
{
menuItem.Children ??= [];
var link = page.Slug!;
if (link.EndsWith("/index"))
{
link = link.Substring(0, link.Length - "index".Length);
// Hide /index from auto Sidebar as it's included in Docs Page Sidebar Header by default
if (link.Trim('/') == folder)
continue;
}
menuItem.Children.Add(new()
{
Text = page.Title!,
Link = link,
});
}
}
return sidebar;
}
}