Files
Explorer-Workbench/src/Explorer.App/DocumentationLoader.cs

56 lines
1.7 KiB
C#

using System.IO;
using System.Reflection;
namespace Explorer.App;
public static class DocumentationLoader
{
public const string EmbeddedName = "Explorer.Documentation.md";
public const string FileName = "Documentation.md";
public static LoadedDocumentation Load()
{
foreach (var path in CandidatePaths())
{
try
{
if (File.Exists(path))
{
return new LoadedDocumentation(File.ReadAllText(path), path);
}
}
catch (IOException)
{
// try the next candidate
}
}
var assembly = typeof(DocumentationLoader).Assembly;
using var stream = assembly.GetManifestResourceStream(EmbeddedName);
if (stream is null)
{
return new LoadedDocumentation(
"# Documentation\n\nThe user guide file was not found. Add `docs/Documentation.md` to the project.",
null);
}
using var reader = new StreamReader(stream);
return new LoadedDocumentation(reader.ReadToEnd(), null);
}
public static IEnumerable<string> CandidatePaths()
{
var baseDir = AppContext.BaseDirectory;
yield return Path.Combine(baseDir, FileName);
yield return Path.Combine(baseDir, "docs", FileName);
var dir = new DirectoryInfo(baseDir);
for (var n = 0; n < 8 && dir is not null; n++, dir = dir.Parent)
{
yield return Path.Combine(dir.FullName, "docs", FileName);
yield return Path.Combine(dir.FullName, FileName);
}
}
}
public sealed record LoadedDocumentation(string Markdown, string? SourcePath);