93 lines
2.6 KiB
C#
93 lines
2.6 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
var exeDir = AppContext.BaseDirectory;
|
|
|
|
var solutionDir = FindContainingDir(exeDir, "ET.sln")
|
|
?? throw new InvalidOperationException("Cannot find ET.sln");
|
|
|
|
var srcDir = Path.GetFullPath(Path.Combine(solutionDir, "..", "docs", "Notes"));
|
|
var dstDir = Path.GetFullPath(Path.Combine(solutionDir, "Web", "wwwroot", "docs", "notes"));
|
|
|
|
Console.WriteLine($"Source: {srcDir}");
|
|
Console.WriteLine($"Destination: {dstDir}");
|
|
|
|
if (!Directory.Exists(srcDir))
|
|
{
|
|
Console.Error.WriteLine($"Source directory not found: {srcDir}");
|
|
return 1;
|
|
}
|
|
|
|
Directory.CreateDirectory(dstDir);
|
|
|
|
foreach (var file in Directory.GetFiles(dstDir))
|
|
File.Delete(file);
|
|
|
|
var entries = new List<object>();
|
|
|
|
foreach (var file in Directory.EnumerateFiles(srcDir, "*.md"))
|
|
{
|
|
var name = Path.GetFileNameWithoutExtension(file);
|
|
var slug = Slugify(name);
|
|
|
|
File.Copy(file, Path.Combine(dstDir, $"{slug}.md"), overwrite: true);
|
|
|
|
string? category = null;
|
|
var content = File.ReadAllText(file);
|
|
var fmMatch = Regex.Match(content, @"^---\s*\n(.*?)\n---", RegexOptions.Singleline);
|
|
if (fmMatch.Success)
|
|
{
|
|
var catMatch = Regex.Match(fmMatch.Groups[1].Value, @"(?m)^category:\s*(.+)$");
|
|
if (catMatch.Success)
|
|
category = catMatch.Groups[1].Value.Trim().Trim('"');
|
|
}
|
|
|
|
var entry = new Dictionary<string, object?>
|
|
{
|
|
["slug"] = slug,
|
|
["title"] = name
|
|
};
|
|
if (category != null)
|
|
entry["category"] = category;
|
|
|
|
entries.Add(entry);
|
|
}
|
|
|
|
var index = new Dictionary<string, object> { ["notes"] = entries };
|
|
var json = System.Text.Json.JsonSerializer.Serialize(index, new System.Text.Json.JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
|
|
var indexPath = Path.Combine(Path.GetDirectoryName(dstDir)!, "notes-index.json");
|
|
File.WriteAllText(indexPath, json);
|
|
|
|
Console.WriteLine($"Copied {entries.Count} notes.");
|
|
Console.WriteLine($"Index written to: {indexPath}");
|
|
return 0;
|
|
|
|
static string Slugify(string name)
|
|
{
|
|
var slug = name.ToLowerInvariant()
|
|
.Replace("'", "")
|
|
.Replace(".", "")
|
|
.Replace("(", "")
|
|
.Replace(")", "");
|
|
slug = Regex.Replace(slug, @"[^a-z0-9 -]", "");
|
|
slug = Regex.Replace(slug, @"\s+", "-");
|
|
slug = Regex.Replace(slug, @"-+", "-");
|
|
return slug.Trim('-');
|
|
}
|
|
|
|
static string? FindContainingDir(string startDir, string markerFile)
|
|
{
|
|
var dir = new DirectoryInfo(startDir);
|
|
while (dir != null)
|
|
{
|
|
if (File.Exists(Path.Combine(dir.FullName, markerFile)))
|
|
return dir.FullName;
|
|
dir = dir.Parent;
|
|
}
|
|
return null;
|
|
}
|
|
|