Skip to content

Commit

Permalink
separated metadata in sidebars into yaml files see docsifyjs/docsify#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikApption committed Mar 14, 2023
1 parent 74660f0 commit 866746a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
23 changes: 22 additions & 1 deletion Portal/utils/SyncDocs/MarkdownDocumentationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal static bool CheckIfDraft(string outputFilePath)
return false;
}

internal static bool CheckIfAutogenerate(string outputFilePath)
internal static bool CheckIfAutogenerateFrontMatter(string outputFilePath)
{
if (!File.Exists(outputFilePath))
return true;
Expand All @@ -113,6 +113,27 @@ internal static bool CheckIfAutogenerate(string outputFilePath)
return false;
}

internal static bool CheckIfAutogenerateYaml(string outputFilePath)
{
if (!File.Exists(outputFilePath))
return true;
var content = File.ReadAllText(outputFilePath);
// deserialize the yaml block into a custom type
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();

var metadata = deserializer.Deserialize<Dictionary<string, string>>(content);
if (metadata.TryGetValue("autogenerate", out var draftString))
{
var isDraft = Boolean.Parse(draftString);
//Console.WriteLine($"File {outputFilePath} - Draft={isDraft}");
return isDraft;
}
return false;
}


private void AddFileMapping(string engPath, string frePath)
{
var id = Guid.NewGuid().ToString();
Expand Down
23 changes: 15 additions & 8 deletions Portal/utils/SyncDocs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using CommandLine;
using Microsoft.Extensions.Options;

const string SIDEBAR = "_sidebar.md";
const string SIDEBAR_META = "_sidebar.md.yaml";

var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: false)
Expand Down Expand Up @@ -69,16 +72,18 @@
var topLevelfolders = new List<string>();
var topLevelfiles = new List<string>();
// iterate the provided source folder
Func<string,bool> excluder = n => Path.GetFileName(n) == "_sidebar.md" || BuildExcluder(configParams)(n);
Func<string,bool> excluder = n => Path.GetFileName(n) == SIDEBAR || BuildExcluder(configParams)(n);
await IteratePath(options.Path, excluder, AddAsync(topLevelfolders!), AddAsync(topLevelfiles), options.TopLevelDepth);
var gen = new SidebarGenerator();
var topSidebar = gen.GenerateTopLevel(options.Path, topLevelfiles, topLevelfolders, options.Profile);
Console.WriteLine($"Processing top level directory {options.Path}");
var metadata = "---\nautogenerate: true\n---\n";
var topSidebarPath = Path.Combine(options.Path, "_sidebar.md");
if (MarkdownDocumentationService.CheckIfAutogenerate(topSidebarPath))
var metadata = "autogenerate: true\n";
var topSidebarPath = Path.Combine(options.Path, SIDEBAR);
var topSidebarPathMeta = Path.Combine(options.Path, SIDEBAR_META);
if (MarkdownDocumentationService.CheckIfAutogenerateYaml(topSidebarPathMeta))
{
await File.WriteAllTextAsync(topSidebarPath, metadata + topSidebar);
await File.WriteAllTextAsync(topSidebarPath, topSidebar);
await File.WriteAllTextAsync(topSidebarPathMeta, metadata);
Console.WriteLine($"Generated top level sidebar");
}
var topFolders1 = topLevelfolders.Where(s => FolderDepth(Path.GetRelativePath(options.Path, s)) < options.TopLevelDepth).ToList();
Expand All @@ -91,10 +96,12 @@
await IteratePath(folder, excluder, AddAsync(folders), AddAsync(files));
Console.WriteLine($"Found {files.Count} files for sidebar");
var sidebar = gen.GenerateSidebar(new DirectoryInfo(folder).Name, folder, files, folders, options.Profile);
var sidebarPath = Path.Combine(folder, "_sidebar.md");
if (MarkdownDocumentationService.CheckIfAutogenerate(sidebarPath))
var sidebarPath = Path.Combine(folder, SIDEBAR);
var sidebarPathMeta = Path.Combine(folder, SIDEBAR_META);
if (MarkdownDocumentationService.CheckIfAutogenerateYaml(sidebarPathMeta))
{
await File.WriteAllTextAsync(sidebarPath, metadata + sidebar);
await File.WriteAllTextAsync(sidebarPath, sidebar);
await File.WriteAllTextAsync(sidebarPathMeta, metadata);
Console.WriteLine($"Generated sidebar {sidebarPath}");
}
}
Expand Down
16 changes: 10 additions & 6 deletions Portal/utils/SyncDocs/TranslationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ public async Task TranslateMarkupFile(string sourcePath, string outputPath, bool
return;

var sourceFileUrl = GetSourceFilePathUrl(sourcePath);
var metadata = $"---\nremarks: Automatically translated with DeepL\nsource: {sourceFileUrl}\ndraft: true\n---";
var metadata = $"remarks: Automatically translated with DeepL\nsource: {sourceFileUrl}\ndraft: true\n";
//var remarks = $"[_metadata_: remarks]:- \"Automatically translated with DeepL. From: {sourceFileUrl}\"";
//var note = $"[_(draft documentation, please review)_]({sourceFileUrl})";
//var note = $"_(draft documentation, please review)_";

using var writer = new StreamWriter(outputPath);
//if (!isSidebar)
//{
writer.WriteLine(metadata);
writer.WriteLine();
//}
if (!isSidebar)
{
writer.WriteLine($"---\n{metadata}---");
writer.WriteLine();
}
else
{
File.WriteAllText($"{outputPath}.yaml", metadata);
}

//writer.WriteLine(note);
//writer.WriteLine();
Expand Down

0 comments on commit 866746a

Please sign in to comment.