Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaJson.cs
More file actions
Latest commit
86 lines (75 loc) · 3.26 KB
/
Copy pathMetaJson.cs
File metadata and controls
86 lines (75 loc) · 3.26 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
usingSystem.Text.Json;
usingSystem.Text.Json.Serialization;
namespaceShellDocs.Core;
publicclassMetaJson
{
[JsonPropertyName("title")]
publicstring?Title{get;set;}
[JsonPropertyName("pages")]
publicList<MetaJsonEntry>Pages{get;set;}=new();
// Slugs of pages or subfolders that should route (URLs resolve) but not
// appear in the sidebar tree. Useful for landing pages reached only via
// the package selector, private drafts, or archived content.
[JsonPropertyName("hidden")]
publicList<string>Hidden{get;set;}=new();
privatestaticreadonlyJsonSerializerOptionsOptions=new()
{
PropertyNameCaseInsensitive=true,
ReadCommentHandling=JsonCommentHandling.Skip,
AllowTrailingCommas=true,
Converters={newMetaJsonEntryConverter()}
};
publicstaticMetaJson?Parse(stringjson)
{
if(string.IsNullOrWhiteSpace(json))returnnull;
returnJsonSerializer.Deserialize<MetaJson>(json,Options);
}
}
publicabstractclassMetaJsonEntry{}
publicclassMetaJsonPageRef:MetaJsonEntry{publicrequiredstringSlug{get;init;}}
publicclassMetaJsonDivider:MetaJsonEntry{}
publicclassMetaJsonSubsection:MetaJsonEntry
{
publicrequiredstringTitle{get;init;}
publicList<MetaJsonEntry>Pages{get;init;}=new();
}
/*
Each entry in meta.json's "pages" array can be:
- a string slug ("button")
- the literal "---" for a section divider
- an object { "title": "Data Display", "pages": [...] } for a subsection
Custom converter dispatches on the JSON token type.
*/
internalclassMetaJsonEntryConverter:JsonConverter<MetaJsonEntry>
{
publicoverrideMetaJsonEntry?Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions)
{
if(reader.TokenType==JsonTokenType.String)
{
varvalue=reader.GetString()??"";
returnvalue=="---"?newMetaJsonDivider():newMetaJsonPageRef{Slug=value};
}
if(reader.TokenType==JsonTokenType.StartObject)
{
usingvardoc=JsonDocument.ParseValue(refreader);
varroot=doc.RootElement;
vartitle=root.TryGetProperty("title",outvart)?t.GetString()??"":"";
varpages=newList<MetaJsonEntry>();
if(root.TryGetProperty("pages",outvarp)&&p.ValueKind==JsonValueKind.Array)
{
foreach(varelinp.EnumerateArray())
{
varelJson=el.GetRawText();
varchildReader=newUtf8JsonReader(System.Text.Encoding.UTF8.GetBytes(elJson));
childReader.Read();
varchild=Read(refchildReader,typeof(MetaJsonEntry),options);
if(childis not null)pages.Add(child);
}
}
returnnewMetaJsonSubsection{Title=title,Pages=pages};
}
thrownewJsonException($"Unexpected token {reader.TokenType} in meta.json entry.");
}
publicoverridevoidWrite(Utf8JsonWriterwriter,MetaJsonEntryvalue,JsonSerializerOptionsoptions)
=>thrownewNotSupportedException("Writing meta.json entries not supported.");
}