-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathRedirectFile.cs
More file actions
169 lines (147 loc) · 4.96 KB
/
RedirectFile.cs
File metadata and controls
169 lines (147 loc) · 4.96 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
using System.IO.Abstractions;
using Elastic.Documentation.Links;
using YamlDotNet.RepresentationModel;
using static Elastic.Documentation.SymlinkValidator;
namespace Elastic.Documentation.Configuration.Builder;
public record RedirectFile
{
public Dictionary<string, LinkRedirect>? Redirects { get; }
public IFileInfo Source { get; }
private IDocumentationSetContext Context { get; }
public RedirectFile(IDocumentationSetContext context, IFileInfo? source = null)
{
var docsetConfigurationPath = context.ConfigurationPath;
var redirectFileName = docsetConfigurationPath.Name.StartsWith('_') ? "_redirects.yml" : "redirects.yml";
var redirectFileInfo = docsetConfigurationPath.FileSystem.FileInfo.New(Path.Join(docsetConfigurationPath.Directory!.FullName, redirectFileName));
Source = source ?? redirectFileInfo;
Context = context;
if (!Source.Exists)
return;
// Validate that the redirects.yml is not a symlink (security: prevents path traversal attacks)
EnsureNotSymlink(Source);
var reader = new YamlStreamReader(Source, Context.Collector);
try
{
foreach (var entry in reader.Read())
{
switch (entry.Key)
{
case "redirects":
Redirects = ReadRedirects(reader, entry.Entry);
break;
default:
reader.EmitWarning($"{entry.Key} is not a known configuration", entry.Key);
break;
}
}
}
catch (Exception e)
{
reader.EmitError("Could not load docset.yml", e);
throw;
}
}
private static Dictionary<string, LinkRedirect>? ReadRedirects(YamlStreamReader reader, KeyValuePair<YamlNode, YamlNode> entry)
{
if (!reader.ReadObjectDictionary(entry, out var mapping))
return null;
var dictionary = new Dictionary<string, LinkRedirect>(StringComparer.OrdinalIgnoreCase);
foreach (var entryValue in mapping.Children)
{
if (entryValue.Key is not YamlScalarNode scalar || scalar.Value is null)
continue;
var key = scalar.Value;
if (entryValue.Value is YamlScalarNode)
{
var to = reader.ReadString(entryValue);
dictionary.Add(key,
!string.IsNullOrEmpty(to)
? to.StartsWith('!')
? new LinkRedirect { To = to.TrimStart('!'), Anchors = LinkRedirect.CatchAllAnchors }
: new LinkRedirect { To = to }
: new LinkRedirect { To = "index.md", Anchors = LinkRedirect.CatchAllAnchors }
);
continue;
}
if (!reader.ReadObjectDictionary(entryValue, out var valueMapping))
continue;
var linkRedirect = ReadLinkRedirect(reader, key, valueMapping);
if (linkRedirect is not null)
dictionary.Add(key, linkRedirect);
}
return dictionary;
}
private static LinkRedirect? ReadLinkRedirect(YamlStreamReader reader, string file, YamlMappingNode mapping)
{
var redirect = new LinkRedirect();
foreach (var entryValue in mapping.Children)
{
if (entryValue.Key is not YamlScalarNode scalar || scalar.Value is null)
continue;
var key = scalar.Value;
switch (key)
{
case "anchors":
redirect = redirect with { Anchors = reader.ReadDictionaryNullValue(entryValue) };
continue;
case "to":
var to = reader.ReadString(entryValue);
if (to is not null)
redirect = redirect with { To = to };
continue;
case "many":
var many = ReadManyRedirects(reader, file, entryValue.Value);
redirect = redirect with { Many = many };
continue;
}
}
if (redirect.To is null && redirect.Anchors is null && redirect.Many is null)
return null;
if (redirect.To is null && redirect.Many is null or { Length: 0 })
return redirect with { To = file };
return string.IsNullOrEmpty(redirect.To) && redirect.Many is null or { Length: 0 }
? null : redirect;
}
private static LinkSingleRedirect[]? ReadManyRedirects(YamlStreamReader reader, string file, YamlNode node)
{
if (node is not YamlSequenceNode sequence)
return null;
var redirects = new List<LinkSingleRedirect>();
foreach (var entryValue in sequence.Children)
{
if (entryValue is not YamlMappingNode mapping)
continue;
var redirect = new LinkRedirect();
foreach (var keyValue in mapping.Children)
{
if (keyValue.Key is not YamlScalarNode scalar || scalar.Value is null)
continue;
var key = scalar.Value;
switch (key)
{
case "anchors":
redirect = redirect with { Anchors = reader.ReadDictionaryNullValue(keyValue) };
continue;
case "to":
var to = reader.ReadString(keyValue);
if (to is not null)
redirect = redirect with { To = to };
continue;
}
}
if (redirect.To is null && redirect.Anchors is not null && redirect.Anchors.Count >= 0)
redirect = redirect with { To = file };
redirects.Add(redirect);
}
if (redirects.Count == 0)
return null;
return
[
..redirects
.Where(r => r.To is not null && r.Anchors is not null && r.Anchors.Count >= 0)
];
}
}