From a9cf3970e5c8a5e56d7aed1beb519e92376f583d Mon Sep 17 00:00:00 2001
From: Bruno Massa <brmassa@gmail.com>
Date: Thu, 6 Jul 2023 14:36:26 -0300
Subject: [PATCH 1/3] fix: section permalink

---
 source/Models/Frontmatter.cs | 50 ++++++++++++++++++------------------
 source/Models/Site.cs        |  4 +--
 2 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/source/Models/Frontmatter.cs b/source/Models/Frontmatter.cs
index 378e378..57b434f 100644
--- a/source/Models/Frontmatter.cs
+++ b/source/Models/Frontmatter.cs
@@ -93,7 +93,7 @@ public class Frontmatter : IBaseContent, IParams
     /// The source directory of the file.
     /// </summary>
     [YamlIgnore]
-    public string? SourcePathLastDirectory => Path.GetDirectoryName(SourcePathDirectory ?? string.Empty);
+    public string? SourcePathLastDirectory => new DirectoryInfo(SourcePathDirectory ?? string.Empty).Name;
 
     /// <summary>
     /// Point to the site configuration.
@@ -273,6 +273,29 @@ public class Frontmatter : IBaseContent, IParams
     /// </summary>
     private DateTime? contentCacheTime { get; set; }
 
+    private const string urlForIndex = @"{%- liquid 
+if page.Parent
+echo page.Parent.Permalink
+echo '/'
+endif
+if page.Title != ''
+echo page.Title
+else
+echo page.SourcePathLastDirectory
+endif
+-%}";
+    private const string urlForNonIndex = @"{%- liquid 
+if page.Parent
+echo page.Parent.Permalink
+echo '/'
+endif
+if page.Title != ''
+echo page.Title
+else
+echo page.SourceFileNameWithoutExtension
+endif
+-%}";
+
     private List<Frontmatter>? regularPagesCache;
 
     private List<Frontmatter>? pagesCached { get; set; }
@@ -326,30 +349,7 @@ public class Frontmatter : IBaseContent, IParams
 
         var permaLink = string.Empty;
 
-        URLforce ??= URL
-            ?? (isIndex 
-            ? @"{%- liquid 
-if page.Parent
-echo page.Parent.Permalink
-echo '/'
-endif
-if page.Title != ''
-echo page.Title
-else
-echo page.SourcePathLastDirectory
-endif
--%}" 
-            : @"{%- liquid 
-if page.Parent
-echo page.Parent.Permalink
-echo '/'
-endif
-if page.Title != ''
-echo page.Title
-else
-echo page.SourceFileNameWithoutExtension
-endif
--%}");
+        URLforce ??= URL ?? (isIndex ? urlForIndex : urlForNonIndex);
 
         try
         {
diff --git a/source/Models/Site.cs b/source/Models/Site.cs
index 81e9c24..bf5cc12 100644
--- a/source/Models/Site.cs
+++ b/source/Models/Site.cs
@@ -267,7 +267,7 @@ public class Site : IParams
         }
         else if (level == 1)
         {
-            var section = directory;
+            var section = new DirectoryInfo(directory).Name;
             var contentTemplate = new BasicContent(
                 title: section,
                 section: "section",
@@ -344,7 +344,6 @@ public class Site : IParams
                     URL = baseContent.URL,
                     PagesReferences = new()
                 };
-
                 automaticContentCache.Add(id, frontmatter);
                 PostProcessFrontMatter(frontmatter);
             }
@@ -355,7 +354,6 @@ public class Site : IParams
             frontmatter.PagesReferences!.Add(originalFrontmatter.Permalink!);
         }
 
-
         // TODO: still too hardcoded
         if (frontmatter.Type != "tags" || originalFrontmatter is null)
         {
-- 
GitLab


From 3a036998ff939dd14dcee6cde51db2693873d4b1 Mon Sep 17 00:00:00 2001
From: Bruno Massa <brmassa@gmail.com>
Date: Thu, 6 Jul 2023 18:25:40 -0300
Subject: [PATCH 2/3] fix: tags and tagsreferences

---
 source/BuildCommand.cs          |  2 +-
 source/Models/Frontmatter.cs    |  9 ++++--
 source/Models/Site.cs           | 51 +++++++++++++++++++++++----------
 source/Parser/YAMLParser.cs     | 12 --------
 source/ServeCommand.cs          |  2 +-
 test/Models/FrontmatterTests.cs |  4 +--
 test/Models/SiteTests.cs        |  4 +--
 test/Parser/YAMLParserTests.cs  |  5 +++-
 8 files changed, 53 insertions(+), 36 deletions(-)

diff --git a/source/BuildCommand.cs b/source/BuildCommand.cs
index 8f6c16e..beffae0 100644
--- a/source/BuildCommand.cs
+++ b/source/BuildCommand.cs
@@ -45,7 +45,7 @@ public class BuildCommand : BaseGeneratorCommand
 
         // Print each page
         var pagesCreated = 0; // counter to keep track of the number of pages created
-        _ = Parallel.ForEach(site.PagesDict, pair =>
+        _ = Parallel.ForEach(site.PagesReferences, pair =>
         {
             var (url, frontmatter) = pair;
             var result = frontmatter.CreateOutputFile();
diff --git a/source/Models/Frontmatter.cs b/source/Models/Frontmatter.cs
index 57b434f..9937f5d 100644
--- a/source/Models/Frontmatter.cs
+++ b/source/Models/Frontmatter.cs
@@ -133,11 +133,16 @@ public class Frontmatter : IBaseContent, IParams
     [YamlIgnore]
     public Frontmatter? Parent { get; set; }
 
+    /// <summary>
+    /// A list of tags, if any.
+    /// </summary>
+    public List<string>? Tags { get; set; }
+
     /// <summary>
     /// A list of tags, if any.
     /// </summary>
     [YamlIgnore]
-    public List<Frontmatter>? Tags { get; set; }
+    public List<Frontmatter>? TagsReference { get; set; }
 
     /// <summary>
     /// Check if the page is expired
@@ -215,7 +220,7 @@ public class Frontmatter : IBaseContent, IParams
             pagesCached ??= new();
             foreach (var permalink in PagesReferences)
             {
-                pagesCached.Add(Site.PagesDict[permalink]);
+                pagesCached.Add(Site.PagesReferences[permalink]);
             }
             return pagesCached;
         }
diff --git a/source/Models/Site.cs b/source/Models/Site.cs
index bf5cc12..2aa6cfd 100644
--- a/source/Models/Site.cs
+++ b/source/Models/Site.cs
@@ -1,6 +1,5 @@
 using System;
 using System.Collections.Generic;
-using System.Globalization;
 using System.IO;
 using System.Linq;
 using System.Threading;
@@ -8,6 +7,7 @@ using System.Threading.Tasks;
 using Fluid;
 using Markdig;
 using Serilog;
+using SuCoS.Helpers;
 using SuCoS.Parser;
 using YamlDotNet.Serialization;
 
@@ -79,7 +79,7 @@ public class Site : IParams
     {
         get
         {
-            pagesCache ??= PagesDict.Values.ToList();
+            pagesCache ??= PagesReferences.Values.ToList();
             return pagesCache!;
         }
     }
@@ -91,13 +91,13 @@ public class Site : IParams
     /// <returns></returns>
     public Frontmatter? GetPage(string permalink)
     {
-        return PagesDict.TryGetValue(permalink, out var page) ? page : null;
+        return PagesReferences.TryGetValue(permalink, out var page) ? page : null;
     }
 
     /// <summary>
     /// List of all pages, including generated, by their permalink.
     /// </summary>
-    public Dictionary<string, Frontmatter> PagesDict { get; } = new();
+    public Dictionary<string, Frontmatter> PagesReferences { get; } = new();
 
     /// <summary>
     /// List of pages from the content folder.
@@ -106,7 +106,7 @@ public class Site : IParams
     {
         get
         {
-            regularPagesCache ??= PagesDict
+            regularPagesCache ??= PagesReferences
                     .Where(pair => pair.Value.Kind == Kind.single)
                     .Select(pair => pair.Value)
                     .ToList();
@@ -224,7 +224,7 @@ public class Site : IParams
         baseTemplateCache.Clear();
         contentTemplateCache.Clear();
         automaticContentCache.Clear();
-        PagesDict.Clear();
+        PagesReferences.Clear();
         IgnoreCacheBefore = DateTime.Now;
     }
 
@@ -252,8 +252,8 @@ public class Site : IParams
                 Home = frontmatter;
                 frontmatter!.Permalink = "/";
                 frontmatter.Kind = Kind.index;
-                PagesDict.Remove(frontmatter.Permalink);
-                PagesDict.Add(frontmatter.Permalink, frontmatter);
+                PagesReferences.Remove(frontmatter.Permalink);
+                PagesReferences.Add(frontmatter.Permalink, frontmatter);
             }
             else
             {
@@ -361,8 +361,8 @@ public class Site : IParams
         }
         lock (originalFrontmatter)
         {
-            originalFrontmatter.Tags ??= new();
-            originalFrontmatter.Tags!.Add(frontmatter);
+            originalFrontmatter.TagsReference ??= new();
+            originalFrontmatter.TagsReference!.Add(frontmatter);
         }
         return frontmatter;
     }
@@ -394,20 +394,20 @@ public class Site : IParams
     /// Extra calculation and automatic data for each frontmatter.
     /// </summary>
     /// <param name="frontmatter">The given page to be processed</param>
-    /// <param name="pageParent">The parent page, if any</param>
+    /// <param name="parent">The parent page, if any</param>
     /// <param name="overwrite"></param>
-    public void PostProcessFrontMatter(Frontmatter frontmatter, Frontmatter? pageParent = null, bool overwrite = false)
+    public void PostProcessFrontMatter(Frontmatter frontmatter, Frontmatter? parent = null, bool overwrite = false)
     {
         if (frontmatter is null)
         {
             throw new ArgumentNullException(nameof(frontmatter));
         }
 
-        frontmatter.Parent = pageParent;
+        frontmatter.Parent = parent;
         frontmatter.Permalink = frontmatter.CreatePermalink();
         lock (syncLockPostProcess)
         {
-            if (!PagesDict.TryGetValue(frontmatter.Permalink, out var old) || overwrite)
+            if (!PagesReferences.TryGetValue(frontmatter.Permalink, out var old) || overwrite)
             {
                 if (old?.PagesReferences is not null)
                 {
@@ -430,9 +430,30 @@ public class Site : IParams
                 // Register the page for all urls
                 foreach (var url in frontmatter.Urls)
                 {
-                    PagesDict[url] = frontmatter;
+                    PagesReferences[url] = frontmatter;
                 }
             }
         }
+
+        if (frontmatter.Tags is not null)
+        {
+            foreach (var tagName in frontmatter.Tags)
+            {
+                var contentTemplate = new BasicContent(
+                    title: tagName,
+                    section: "tags",
+                    type: "tags",
+                    url: "tags/" + Urlizer.Urlize(tagName)
+                );
+                _ = CreateAutomaticFrontmatter(contentTemplate, frontmatter);
+            }
+        }
+
+        if (!string.IsNullOrEmpty(frontmatter.Section)
+            && PagesReferences.TryGetValue('/' + frontmatter.Section!, out var section))
+        {
+            section.PagesReferences ??= new();
+            section.PagesReferences.Add(frontmatter.Permalink!);
+        }
     }
 }
\ No newline at end of file
diff --git a/source/Parser/YAMLParser.cs b/source/Parser/YAMLParser.cs
index ae35946..f166971 100644
--- a/source/Parser/YAMLParser.cs
+++ b/source/Parser/YAMLParser.cs
@@ -106,18 +106,6 @@ public class YAMLParser : IFrontmatterParser
         {
             return page;
         }
-
-        foreach (var tagObj in tagsValues)
-        {
-            var tagName = (string)tagObj;
-            var contentTemplate = new BasicContent(
-                title: tagName,
-                section: "tags",
-                type: "tags",
-                url: "tags/" + Urlizer.Urlize(tagName)
-            );
-            _ = site.CreateAutomaticFrontmatter(contentTemplate, page);
-        }
         ParseParams(page, typeof(Frontmatter), yaml, yamlDictionary);
 
         return page;
diff --git a/source/ServeCommand.cs b/source/ServeCommand.cs
index f78381d..f34d763 100644
--- a/source/ServeCommand.cs
+++ b/source/ServeCommand.cs
@@ -231,7 +231,7 @@ public class ServeCommand : BaseGeneratorCommand, IDisposable
         }
 
         // Check if the requested file path corresponds to a registered page
-        else if (site.PagesDict.TryGetValue(requestPath, out var frontmatter))
+        else if (site.PagesReferences.TryGetValue(requestPath, out var frontmatter))
         {
             resultType = "dict";
             await HandleRegisteredPageRequest(context, frontmatter);
diff --git a/test/Models/FrontmatterTests.cs b/test/Models/FrontmatterTests.cs
index b6dcb78..5140a02 100644
--- a/test/Models/FrontmatterTests.cs
+++ b/test/Models/FrontmatterTests.cs
@@ -56,7 +56,7 @@ public class FrontmatterTests
         Assert.Null(frontmatter.Permalink);
         Assert.Empty(frontmatter.Urls);
         Assert.Equal(string.Empty, frontmatter.RawContent);
-        Assert.Null(frontmatter.Tags);
+        Assert.Null(frontmatter.TagsReference);
         Assert.Null(frontmatter.PagesReferences);
         Assert.Empty(frontmatter.RegularPages);
         Assert.False(frontmatter.IsDateExpired);
@@ -78,7 +78,7 @@ public class FrontmatterTests
         // Assert
         foreach (var url in new[] { "/v123", "/title" })
         {
-            site.PagesDict.TryGetValue(url, out var frontmatter1);
+            site.PagesReferences.TryGetValue(url, out var frontmatter1);
             Assert.NotNull(frontmatter1);
             Assert.Same(frontmatter, frontmatter1);
         }
diff --git a/test/Models/SiteTests.cs b/test/Models/SiteTests.cs
index f5364b8..5cb16d1 100644
--- a/test/Models/SiteTests.cs
+++ b/test/Models/SiteTests.cs
@@ -50,12 +50,12 @@ public class SiteTests
         var key = (firstKeyPart, secondKeyPart, thirdKeyPart);
         site.baseTemplateCache.Add(key, value);
         site.contentTemplateCache.Add(key, value);
-        site.PagesDict.Add("test", new Frontmatter("Test Title", "sourcePath", site));
+        site.PagesReferences.Add("test", new Frontmatter("Test Title", "sourcePath", site));
 
         site.ResetCache();
 
         Assert.Empty(site.baseTemplateCache);
         Assert.Empty(site.contentTemplateCache);
-        Assert.Empty(site.PagesDict);
+        Assert.Empty(site.PagesReferences);
     }
 }
diff --git a/test/Parser/YAMLParserTests.cs b/test/Parser/YAMLParserTests.cs
index 62dfc24..ecfefc4 100644
--- a/test/Parser/YAMLParserTests.cs
+++ b/test/Parser/YAMLParserTests.cs
@@ -4,6 +4,7 @@ using SuCoS.Parser;
 using System.Globalization;
 using SuCoS.Helpers;
 using SuCoS.Models;
+using Microsoft.VisualBasic;
 
 namespace Test.Parser;
 
@@ -186,10 +187,12 @@ Title
     [Fact]
     public void ParseFrontmatter_ShouldCreateTags()
     {
+        // Act
         var frontmatter = parser.ParseFrontmatterAndMarkdown(siteDefault.Object, "", pageContent);
+        siteDefault.Object.PostProcessFrontMatter(frontmatter);
 
         // Asset
-        Assert.Equal(2, frontmatter.Tags?.Count);
+        Assert.Equal(2, frontmatter.TagsReference?.Count);
     }
 
     [Fact]
-- 
GitLab


From 0377e5b4aeffa6610674b7d9450e50c531891a18 Mon Sep 17 00:00:00 2001
From: Bruno Massa <brmassa@gmail.com>
Date: Thu, 6 Jul 2023 18:26:20 -0300
Subject: [PATCH 3/3] fix: RegularPages now discriminate one entry per page,
 even with aliases

---
 source/Models/Site.cs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source/Models/Site.cs b/source/Models/Site.cs
index 2aa6cfd..9f05f01 100644
--- a/source/Models/Site.cs
+++ b/source/Models/Site.cs
@@ -107,7 +107,7 @@ public class Site : IParams
         get
         {
             regularPagesCache ??= PagesReferences
-                    .Where(pair => pair.Value.Kind == Kind.single)
+                    .Where(pair => pair.Value.Kind == Kind.single && pair.Key == pair.Value.Permalink)
                     .Select(pair => pair.Value)
                     .ToList();
             return regularPagesCache;
-- 
GitLab