diff --git a/Helpers/HelperMarkdown.cs b/Helpers/HelperMarkdown.cs index 10bd952..3ce04e8 100644 --- a/Helpers/HelperMarkdown.cs +++ b/Helpers/HelperMarkdown.cs @@ -46,7 +46,7 @@ internal static partial class HelperMarkdown internal static string[] GetFiles(AppSettings appSettings, string directory) { string[] results = Directory.GetFiles(directory, "*.md", SearchOption.AllDirectories). - Where(l => !appSettings.Exclude.Any(m => l.Contains(m))).ToArray(); + Where(l => !appSettings.ExcludeDirectoryNames.Any(m => l.Contains(m))).ToArray(); return results; } @@ -151,6 +151,14 @@ internal static partial class HelperMarkdown return results; } + private static List<(MarkdownFile, string[])> GetCollection(AppSettings appSettings, Record record) + { + List<(MarkdownFile, string[])> results; + string[] files = GetFiles(appSettings, record.Source); + results = GetCollection(appSettings, files); + return results; + } + private static int SetFrontMatterAndH1(AppSettings appSettings, List<(MarkdownFile, string[])> collection) { int result = 0; @@ -342,8 +350,7 @@ internal static partial class HelperMarkdown private static Dictionary> GetKeyValuePairs(AppSettings appSettings, Record record) { Dictionary> results; - string[] files = GetFiles(appSettings, record.Source); - List<(MarkdownFile MarkdownFile, string[] Lines)> collection = GetCollection(appSettings, files); + List<(MarkdownFile MarkdownFile, string[] Lines)> collection = GetCollection(appSettings, record); results = GetKeyValuePairs(collection); return results; } @@ -507,6 +514,50 @@ internal static partial class HelperMarkdown return result; } + private static (string?, string?) GetRelativePath(List<(MarkdownFile MarkdownFile, string[] Lines)> collection, MarkdownFile markdownFile, string file) + { + string? title; + string? relativePath; + List<(string RelativePath, string Title)> results = new(); + foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection) + { + if (item.MarkdownFile.File != file) + continue; + results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1)); + } + if (results.Count == 1) + (relativePath, title) = (results.First().RelativePath, results.First().Title); + else + { + results.Clear(); + string fileLowered = file.ToLower(); + foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection) + { + if (item.MarkdownFile.File.ToLower() != fileLowered) + continue; + results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1)); + } + if (results.Count == 1) + (relativePath, title) = (results.First().RelativePath, results.First().Title); + else + { + results.Clear(); + string fileFullPath = Path.GetFullPath(fileLowered); + foreach ((MarkdownFile MarkdownFile, string[] Lines) item in collection) + { + if (Path.GetFullPath(item.MarkdownFile.File).ToLower() != fileFullPath) + continue; + results.Add((Path.GetRelativePath(markdownFile.Directory, Path.GetFullPath(item.MarkdownFile.File)), item.MarkdownFile.H1)); + } + if (results.Count == 1) + (relativePath, title) = (results.First().RelativePath, results.First().Title); + else + (relativePath, title) = (null, null); + } + } + return (relativePath, title); + } + private static int ConvertFileToSlugName(AppSettings appSettings, ILogger logger, Record record, List<(MarkdownFile MarkdownFile, string[] Lines)> collection) { int result = 0; @@ -526,7 +577,7 @@ internal static partial class HelperMarkdown string segmentsALast; string? relativePath; string segmentsBFirst; - Dictionary> keyValuePairs = record.StartAt is null ? GetKeyValuePairs(collection) : GetKeyValuePairs(appSettings, record); + List<(MarkdownFile MarkdownFile, string[] Lines)> sourceCollection = record.StartAt is null ? collection : GetCollection(appSettings, record); foreach ((MarkdownFile markdownFile, string[] lines) in collection) { if (markdownFile.FileNameWithoutExtension == "index") @@ -540,7 +591,7 @@ internal static partial class HelperMarkdown if (segmentsA.Length != 2) continue; segmentsALast = segmentsA.Last(); - if (segmentsALast.StartsWith("http:") || segmentsALast.StartsWith("https:") || segmentsALast.StartsWith("rdp:") || segmentsALast.StartsWith("onenote:")) + if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l))) continue; segmentsB = segmentsALast.Split(")"); if (segmentsB.Length != 2) @@ -554,7 +605,7 @@ internal static partial class HelperMarkdown checkFileName = fileName.ToLower().Replace("%20", "-").Replace(' ', '-'); checkName = Path.Combine(directory, checkFileName); segmentsC = segmentsA.First().Split('['); - (relativePath, title) = GetRelativePath(keyValuePairs, markdownFile, file); + (relativePath, title) = GetRelativePath(sourceCollection, markdownFile, file); if (relativePath is null) { logger.LogInformation("Didn't find {line} in <{file}>", lines[i], markdownFile.FileNameWithoutExtension); @@ -748,7 +799,7 @@ internal static partial class HelperMarkdown if (segmentsA.Length != 2) continue; segmentsALast = segmentsA.Last(); - if (segmentsALast.StartsWith("http:") || segmentsALast.StartsWith("https:") || segmentsALast.StartsWith("rdp:") || segmentsALast.StartsWith("onenote:")) + if (appSettings.ExcludeSchemes.Any(l => segmentsALast.StartsWith(l))) continue; segmentsB = segmentsALast.Split(")"); if (segmentsB.Length != 2) diff --git a/Models/AppSettings.cs b/Models/AppSettings.cs index 731bc3b..cadc64f 100644 --- a/Models/AppSettings.cs +++ b/Models/AppSettings.cs @@ -5,7 +5,8 @@ namespace File_Folder_Helper.Models; public record AppSettings(string Company, string DefaultNoteType, - string[] Exclude, + string[] ExcludeDirectoryNames, + string[] ExcludeSchemes, string WorkingDirectoryName) { diff --git a/Models/Binder/AppSettings.cs b/Models/Binder/AppSettings.cs index 133c4fc..ba29eef 100644 --- a/Models/Binder/AppSettings.cs +++ b/Models/Binder/AppSettings.cs @@ -9,7 +9,8 @@ public class AppSettings public string? Company { get; set; } public string? DefaultNoteType { get; set; } - public string[]? Exclude { get; set; } + public string[]? ExcludeDirectoryNames { get; set; } + public string[]? ExcludeSchemes { get; set; } public string? WorkingDirectoryName { get; set; } public override string ToString() @@ -25,14 +26,17 @@ public class AppSettings throw new NullReferenceException(nameof(appSettings.Company)); if (appSettings?.DefaultNoteType is null) throw new NullReferenceException(nameof(appSettings.DefaultNoteType)); - if (appSettings?.Exclude is null) - throw new NullReferenceException(nameof(appSettings.Exclude)); + if (appSettings?.ExcludeDirectoryNames is null) + throw new NullReferenceException(nameof(appSettings.ExcludeDirectoryNames)); + if (appSettings?.ExcludeSchemes is null) + throw new NullReferenceException(nameof(appSettings.ExcludeSchemes)); if (appSettings?.WorkingDirectoryName is null) throw new NullReferenceException(nameof(appSettings.WorkingDirectoryName)); result = new( appSettings.Company, appSettings.DefaultNoteType, - appSettings.Exclude, + appSettings.ExcludeDirectoryNames, + appSettings.ExcludeSchemes, appSettings.WorkingDirectoryName ); return result;