Replies: 2 comments 10 replies
-
In theory, yes - this line in the self.blog = self._resolve(files, config)
self.blog.posts = sorted(
self._resolve_posts(files, config),
key = lambda post: post.config.date.created, # blogs are sorted by creation date
reverse = True
) This could probably be adjusted, but it's not trivial, and I can't do the code-digging needed right now. Maybe this might make for a good feature proposal, @squidfunk? Allowing to specify a custom sorting of blog posts based on passed/available metadata in the plugin's config, and falling back to creation date as default? |
Beta Was this translation helpful? Give feedback.
-
Hi @Pageboy, So I made a small example, hopefully with "best practices" ✌️ The Hook codeimport logging
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.plugins import PrefixedLogger, event_priority
from mkdocs.structure.nav import Navigation
from mkdocs.structure.pages import Page
from mkdocs.utils.templates import TemplateContext
def on_config(config: MkDocsConfig):
# Reset state for mkdocs serve changes
StateHandler.is_sorted = False
# Run after the Blog plugin
@event_priority(-105)
def on_page_context(
context: TemplateContext, *, page: Page, config: MkDocsConfig, nav: Navigation
):
# Quit early if already sorted
if StateHandler.is_sorted:
return
posts = context.get("posts")
# Ignore pages without posts
if not posts:
return
# Sort in-place, this affects the posts globally for all pages that use them
# Most recently updated at the top
posts.sort(key=lambda post: post.config.date.updated, reverse=True)
StateHandler.is_sorted = True
LOG.info("Blog posts sorted by updated")
class StateHandler:
is_sorted: bool = False
HOOK_NAME: str = "sort_blog_updated"
"""Name of this hook. Used in logging."""
LOG: PrefixedLogger = PrefixedLogger(
HOOK_NAME, logging.getLogger(f"mkdocs.hooks.{HOOK_NAME}")
)
"""Logger instance for this hook.""" |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to order the blog by updated date?
Beta Was this translation helpful? Give feedback.
All reactions