Skip to content

Commit 058b32f

Browse files
committed
Fixed non-deterministic order of categories in blog plugin
1 parent d43626a commit 058b32f

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

material/plugins/blog/plugin.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,17 @@ def on_files(self, files, *, config):
138138

139139
# Generate views for archive
140140
if self.config.archive:
141-
views = self._generate_archive(config, files)
142-
self.blog.views.extend(views)
141+
self.blog.views.extend(
142+
self._generate_archive(config, files)
143+
)
143144

144145
# Generate views for categories
145146
if self.config.categories:
146-
views = self._generate_categories(config, files)
147-
self.blog.views.extend(views)
147+
self.blog.views.extend(sorted(
148+
self._generate_categories(config, files),
149+
key = lambda view: view.name,
150+
reverse = False
151+
))
148152

149153
# Generate pages for views
150154
if self.config.pagination:
@@ -573,10 +577,9 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
573577
self._save_to_file(file.abs_src_path, f"# {name}")
574578
file.inclusion = InclusionLevel.EXCLUDED
575579

576-
# Create and yield view - we don't explicitly set the title of
577-
# the view, so authors can override them in the page's content
580+
# Create and yield view
578581
if not isinstance(file.page, Archive):
579-
yield Archive(None, file, config)
582+
yield Archive(name, file, config)
580583

581584
# Assign post to archive
582585
assert isinstance(file.page, Archive)
@@ -610,10 +613,9 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
610613
self._save_to_file(file.abs_src_path, f"# {name}")
611614
file.inclusion = InclusionLevel.EXCLUDED
612615

613-
# Create and yield view - we don't explicitly set the title of
614-
# the view, so authors can override them in the page's content
616+
# Create and yield view
615617
if not isinstance(file.page, Category):
616-
yield Category(None, file, config)
618+
yield Category(name, file, config)
617619

618620
# Assign post to category and vice versa
619621
assert isinstance(file.page, Category)

material/plugins/blog/structure/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,18 @@ def render(self, page: Page, separator: str):
212212
# View
213213
class View(Page):
214214

215+
# Parent view
216+
parent: View | Section
217+
215218
# Initialize view
216-
def __init__(self, title: str | None, file: File, config: MkDocsConfig):
217-
super().__init__(title, file, config)
218-
self.parent: View | Section
219+
def __init__(self, name: str | None, file: File, config: MkDocsConfig):
220+
super().__init__(None, file, config)
221+
222+
# Initialize name of the view - note that views never pass a title to
223+
# the parent constructor, so the author can always override the title
224+
# that is used for rendering. However, for some purposes, like for
225+
# example sorting, we need something to compare.
226+
self.name = name
219227

220228
# Initialize posts and views
221229
self.posts: list[Post] = []

src/plugins/blog/plugin.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,17 @@ def on_files(self, files, *, config):
138138

139139
# Generate views for archive
140140
if self.config.archive:
141-
views = self._generate_archive(config, files)
142-
self.blog.views.extend(views)
141+
self.blog.views.extend(
142+
self._generate_archive(config, files)
143+
)
143144

144145
# Generate views for categories
145146
if self.config.categories:
146-
views = self._generate_categories(config, files)
147-
self.blog.views.extend(views)
147+
self.blog.views.extend(sorted(
148+
self._generate_categories(config, files),
149+
key = lambda view: view.name,
150+
reverse = False
151+
))
148152

149153
# Generate pages for views
150154
if self.config.pagination:
@@ -573,10 +577,9 @@ def _generate_archive(self, config: MkDocsConfig, files: Files):
573577
self._save_to_file(file.abs_src_path, f"# {name}")
574578
file.inclusion = InclusionLevel.EXCLUDED
575579

576-
# Create and yield view - we don't explicitly set the title of
577-
# the view, so authors can override them in the page's content
580+
# Create and yield view
578581
if not isinstance(file.page, Archive):
579-
yield Archive(None, file, config)
582+
yield Archive(name, file, config)
580583

581584
# Assign post to archive
582585
assert isinstance(file.page, Archive)
@@ -610,10 +613,9 @@ def _generate_categories(self, config: MkDocsConfig, files: Files):
610613
self._save_to_file(file.abs_src_path, f"# {name}")
611614
file.inclusion = InclusionLevel.EXCLUDED
612615

613-
# Create and yield view - we don't explicitly set the title of
614-
# the view, so authors can override them in the page's content
616+
# Create and yield view
615617
if not isinstance(file.page, Category):
616-
yield Category(None, file, config)
618+
yield Category(name, file, config)
617619

618620
# Assign post to category and vice versa
619621
assert isinstance(file.page, Category)

src/plugins/blog/structure/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,18 @@ def render(self, page: Page, separator: str):
212212
# View
213213
class View(Page):
214214

215+
# Parent view
216+
parent: View | Section
217+
215218
# Initialize view
216-
def __init__(self, title: str | None, file: File, config: MkDocsConfig):
217-
super().__init__(title, file, config)
218-
self.parent: View | Section
219+
def __init__(self, name: str | None, file: File, config: MkDocsConfig):
220+
super().__init__(None, file, config)
221+
222+
# Initialize name of the view - note that views never pass a title to
223+
# the parent constructor, so the author can always override the title
224+
# that is used for rendering. However, for some purposes, like for
225+
# example sorting, we need something to compare.
226+
self.name = name
219227

220228
# Initialize posts and views
221229
self.posts: list[Post] = []

0 commit comments

Comments
 (0)