From c988131d09fe490103bfc2822d8411e22479fb0d Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 16 Feb 2024 16:13:07 +0000 Subject: [PATCH] Add notes list and optimise getting notes --- docs/notes/index.md | 6 ++++++ hooks/notes.py | 28 ++++++++++++++++++++++++++++ mkdocs.yml | 3 ++- theme/all.html | 16 ++++++++++++++++ theme/home.html | 7 +++++-- 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 docs/notes/index.md create mode 100644 theme/all.html diff --git a/docs/notes/index.md b/docs/notes/index.md new file mode 100644 index 0000000..77a71c8 --- /dev/null +++ b/docs/notes/index.md @@ -0,0 +1,6 @@ +--- +template: all.html +hide: [toc] +--- + +# Notes diff --git a/hooks/notes.py b/hooks/notes.py index b31d251..2e33233 100644 --- a/hooks/notes.py +++ b/hooks/notes.py @@ -1,2 +1,30 @@ +import jinja2 + +@jinja2.pass_context +def get_page(context, slug): + nav = context["nav"] + for page in nav.pages: + if page.file.src_uri == slug: + return page + + raise ValueError("Unable to find page for '%s'", slug) + + +@jinja2.pass_context +def get_notes(context): + notes = [] + for page in context["nav"].pages: + if not page.file.src_uri.startswith("notes/"): + continue + + if page.file.src_uri == "notes/index.md": + continue + + notes.append(page) + return sorted(notes, key=lambda p: p.meta["git_creation_date_localized_raw_iso_date"], reverse=True) + + def on_env(env, config, files): env.tests["startswith"] = str.startswith + env.globals["get_page"] = get_page + env.globals["get_notes"] = get_notes diff --git a/mkdocs.yml b/mkdocs.yml index 7abb76d..8056434 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -26,7 +26,7 @@ theme: features: - navigation.top - navigation.sections - - navigation.index + - navigation.indexes - toc.follow - search.suggest - navigation.top @@ -102,6 +102,7 @@ plugins: exclude: - tags.md - README.md + - notes/index.md - minify_html - social - rss: diff --git a/theme/all.html b/theme/all.html new file mode 100644 index 0000000..f4c316c --- /dev/null +++ b/theme/all.html @@ -0,0 +1,16 @@ +{% extends "base.html" %} + +{% block content %} + {{ super() }} + + +{% endblock %} diff --git a/theme/home.html b/theme/home.html index 208639d..8bdc5f2 100644 --- a/theme/home.html +++ b/theme/home.html @@ -5,8 +5,11 @@

Recent notes

+ {% set notes_index = get_page('notes/index.md') %} + {% set notes = get_notes() %} + -

View all →

+

View all {{ notes|length }} →

{% endblock %}