generated from NetCoreTemplates/empty
/
Markdown.Meta.cs
60 lines (55 loc) · 2.47 KB
/
Markdown.Meta.cs
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
using ServiceStack.IO;
using ServiceStack.Text;
namespace MyApp;
public class MarkdownMeta
{
public List<IMarkdownPages> Features { get; set; } = [];
public async Task RenderToAsync(string metaDir, string baseUrl)
{
FileSystemVirtualFiles.RecreateDirectory(metaDir);
using var scope = JsConfig.With(new Config { ExcludeTypeInfo = true });
var featureDocs = new Dictionary<string, List<MarkdownFileBase>>();
var allYears = new HashSet<int>();
var index = new Dictionary<string, object>();
foreach (var feature in Features.Safe())
{
var allDocs = feature.GetAll()
.OrderByDescending(x => x.Date!.Value)
.ThenBy(x => x.Order)
.ThenBy(x => x.FileName)
.ToList();
allDocs.ForEach(x => {
if (x.Url?.StartsWith("/") == true)
x.Url = baseUrl.CombineWith(x.Url);
if (x.Image?.StartsWith("/") == true)
x.Image = baseUrl.CombineWith(x.Image);
});
featureDocs[feature.Id] = allDocs;
var featureYears = allDocs.Select(x => x.Date!.Value.Year).Distinct().OrderBy(x => x).ToList();
featureYears.ForEach(x => allYears.Add(x));
index[feature.Id] = featureYears.Map(x => baseUrl.CombineWith($"/meta/{x}/{feature.Id}.json"));
foreach (var year in featureYears)
{
var yearDocs = allDocs
.Where(x => x.Date!.Value.Year == year)
.ToList();
var yearDir = metaDir.CombineWith(year).AssertDir();
var metaPath = yearDir.CombineWith($"{feature.Id}.json");
await File.WriteAllTextAsync(metaPath, yearDocs.ToJson());
}
}
await File.WriteAllTextAsync(metaDir.CombineWith("index.json"), JSON.stringify(index));
await File.WriteAllTextAsync(metaDir.CombineWith("all.json"), JSON.stringify(featureDocs));
foreach (var year in allYears.OrderBy(x => x))
{
var yearDocs = new Dictionary<string, List<MarkdownFileBase>>();
foreach (var entry in featureDocs)
{
yearDocs[entry.Key] = entry.Value
.Where(x => x.Date!.Value.Year == year)
.ToList();
}
await File.WriteAllTextAsync(metaDir.CombineWith($"{year}/all.json"), JSON.stringify(yearDocs));
}
}
}