From d54e99d422a4770747894443ba6ece7a53c9351b Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Sun, 22 Jan 2023 11:50:34 +0100
Subject: [PATCH] fix: filesort

---
 application/source/document/files.go | 15 ++++++++++++---
 application/source/document/html.go  |  7 +++----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/application/source/document/files.go b/application/source/document/files.go
index e2a9edd..f773d14 100644
--- a/application/source/document/files.go
+++ b/application/source/document/files.go
@@ -5,6 +5,7 @@ package document
 
 import (
 	"errors"
+	"fmt"
 	"os"
 	"path"
 	"path/filepath"
@@ -40,13 +41,21 @@ func (m SourceFileMap) findByHash(hash string) *SourceFile {
 func buildFileMap(files []*SourceFile) (SourceFileMap, []string) {
 
 	sort.Slice(files, func(i, j int) bool {
-		if files[i].level == files[j].level {
-			return files[i].relSourcePath < files[j].relSourcePath
+		if files[i].relSourcePath == files[j].relSourcePath {
+			if files[i].level < files[j].level {
+				return true
+			}
+
+			return files[i].level < files[j].level
 		}
 
-		return files[i].level < files[j].level
+		return files[i].relSourcePath < files[j].relSourcePath
 	})
 
+	for _, f := range files {
+		fmt.Println(f.relSourcePath, f.level)
+	}
+
 	keys := make([]string, 0, len(files))
 	mapFiles := make(map[string]*SourceFile)
 	for _, file := range files {
diff --git a/application/source/document/html.go b/application/source/document/html.go
index 0eea67d..a85104d 100644
--- a/application/source/document/html.go
+++ b/application/source/document/html.go
@@ -239,12 +239,11 @@ func buildTree(body string) *Tree[DocumentNode] {
 
 			if title, err := s.Html(); err == nil {
 				aID = slug.Make(title)
-			} else {
-				aID = "" + e.DataAtom.String() + "-" + randomID()
 			}
 
-			s.SetAttr("id", aID)
-
+			if aID == "" {
+				aID = "" + e.DataAtom.String() + "-" + randomID()
+			}
 		}
 
 		payload := DocumentNode{
-- 
GitLab