Repository: lukasgeiter/mkdocs-awesome-pages-plugin

Issue:
order_by title does not work with folder
the new order_by feature works great with multible files in one directory. Thank you!

But the new feature does not work with directories:

directory tree:
```tree
│   mkdocs.yml
└───docs
    │   .pages
    │   index.md
    ├───1
    │       .pages (Title: A (1))
    │       index.md
    ├───2
    │       .pages (Title: C (2))
    │       index.md
    ├───3
    │       .pages (Title: B (3))
    │       index.md
    └───4
            .pages (Title: D (4))
            index.md
```
.pages at root level:
```yaml
nav: 
  - Home: index.md
  - ... | flat
order_by: title
```


My expection was that the groups where sorted by name (from the .pages in sub dir: A, B, C, D) but they are sorted by the name of the directory (1, 2, 3, 4):
![image](https://user-images.githubusercontent.com/18051656/235715327-4f0ad874-8028-408b-aa68-e692c68da50a.png)

My minimized demo solution:
[mkdocs-awesome-order-by-folder.zip](https://github.com/lukasgeiter/mkdocs-awesome-pages-plugin/files/11375039/mkdocs-awesome-order-by-folder.zip)

Is this behavior intentional or is it a bug?

Discussion:
lukasgeiter: Thanks for fixing this! The code looks good. That said, I would like to see a test that fails before the fix and passes after it.
kamilkrzyskow: Emotional damage... ok I shall see what I can do.
kamilkrzyskow: @lukasgeiter I think this should do the trick ✌️ 

Patch:
diff --git a/mkdocs_awesome_pages_plugin/navigation.py b/mkdocs_awesome_pages_plugin/navigation.py
index 62b1fd6..812f796 100644
--- a/mkdocs_awesome_pages_plugin/navigation.py
+++ b/mkdocs_awesome_pages_plugin/navigation.py
@@ -93,7 +93,7 @@ class AwesomeNavigation:
             return
 
         if order_by == Meta.ORDER_BY_TITLE:
-            key = lambda i: get_title(i)
+            key = lambda i: self._get_item_title(i)
         else:
             key = lambda i: basename(self._get_item_path(i))
 
@@ -191,6 +191,46 @@ class AwesomeNavigation:
         elif isinstance(item, Page):
             return item.file.abs_src_path
 
+    def _get_item_title(self, item: NavigationItem) -> str:
+        # Handle custom section titles in the ".pages" file
+        if isinstance(item, Section):
+            title = self.meta.sections[item].title
+            if title is not None:
+                return title
+
+        if item.title is not None:
+            return item.title
+
+        if not isinstance(item, Page):
+            return str(item.title)
+
+        # Copy of mkdocs.structure.pages.Page._set_title and Page.read_source
+        try:
+            with open(item.file.abs_src_path, encoding="utf-8-sig", errors="strict") as f:
+                source = f.read()
+        except OSError:
+            raise OSError(f"File not found: {item.file.src_path}")
+        except ValueError:
+            raise ValueError(f"Encoding error reading file: {item.file.src_path}")
+
+        page_markdown, page_meta = mkdocs.utils.meta.get_data(source)
+
+        if "title" in page_meta:
+            return page_meta["title"]
+
+        title = mkdocs.utils.get_markdown_title(page_markdown)
+
+        if title is None:
+            if item.is_homepage:
+                title = "Home"
+            else:
+                title = item.file.name.replace("-", " ").replace("_", " ")
+                # Capitalize if the filename was all lowercase, otherwise leave it as-is.
+                if title.lower() == title:
+                    title = title.capitalize()
+
+        return title
+
     @staticmethod
     def _set_title(section: Section, meta: Meta):
         if meta.title is not None:
@@ -265,38 +305,3 @@ def get_by_type(nav, T):
         if item.children:
             ret.extend(get_by_type(item.children, T))
     return ret
-
-
-# Copy of mkdocs.structure.pages.Page._set_title and Page.read_source
-def get_title(item: NavigationItem) -> str:
-    if item.title is not None:
-        return item.title
-
-    if not isinstance(item, Page):
-        return str(item.title)
-
-    try:
-        with open(item.file.abs_src_path, encoding="utf-8-sig", errors="strict") as f:
-            source = f.read()
-    except OSError:
-        raise OSError(f"File not found: {item.file.src_path}")
-    except ValueError:
-        raise ValueError(f"Encoding error reading file: {item.file.src_path}")
-
-    page_markdown, page_meta = mkdocs.utils.meta.get_data(source)
-
-    if "title" in page_meta:
-        return page_meta["title"]
-
-    title = mkdocs.utils.get_markdown_title(page_markdown)
-
-    if title is None:
-        if item.is_homepage:
-            title = "Home"
-        else:
-            title = item.file.name.replace("-", " ").replace("_", " ")
-            # Capitalize if the filename was all lowercase, otherwise leave it as-is.
-            if title.lower() == title:
-                title = title.capitalize()
-
-    return title


Test patch:
diff --git a/mkdocs_awesome_pages_plugin/tests/e2e/test_order_and_sort.py b/mkdocs_awesome_pages_plugin/tests/e2e/test_order_and_sort.py
index a7573d2..88b850f 100644
--- a/mkdocs_awesome_pages_plugin/tests/e2e/test_order_and_sort.py
+++ b/mkdocs_awesome_pages_plugin/tests/e2e/test_order_and_sort.py
@@ -424,3 +424,19 @@ class TestOrderAndSort(E2ETestCase):
             navigation,
             [("A", "/3"), ("B", [("C", "/B/1"), ("B", "/B/2")]), ("C", "/1")],
         )
+
+    def test_order_by_title_section_with_custom_title(self):
+        navigation = self.mkdocs(
+            self.createConfig(),
+            [
+                ("1.md", "# C"),
+                ("B", [("1.md", "# C"), ("2.md", "# B"), self.pagesFile(title="D")]),
+                ("3.md", "# A"),
+                self.pagesFile(order_by="title"),
+            ],
+        )
+
+        self.assertEqual(
+            navigation,
+            [("A", "/3"), ("C", "/1"), ("D", [("C", "/B/1"), ("B", "/B/2")])],
+        )