Add notes list and optimise getting notes

This commit is contained in:
Jake Howard 2024-02-16 16:13:07 +00:00 committed by Jake Howard
parent 3108f3a01f
commit c988131d09
5 changed files with 57 additions and 3 deletions

6
docs/notes/index.md Normal file
View File

@ -0,0 +1,6 @@
---
template: all.html
hide: [toc]
---
# Notes

View File

@ -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

View File

@ -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:

16
theme/all.html Normal file
View File

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block content %}
{{ super() }}
<ul>
{% for page in get_notes() %}
<li>
<a href="{{ page.url }}">{{ page.title }}</a> - {{ page.meta.git_creation_date_localized_raw_iso_date }}
{% for tag in page.meta.tags %}
<span class="md-tag">{{ tag }}</span>
{% endfor %}
</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -5,8 +5,11 @@
<h2>Recent notes</h2>
{% set notes_index = get_page('notes/index.md') %}
{% set notes = get_notes() %}
<ul>
{% for page in (nav.pages|selectattr("url", "startswith", "notes/")|sort(attribute="meta.git_creation_date_localized_raw_iso_datetime", reverse=True))[:10] %}
{% for page in notes[:10] %}
<li>
<a href="{{ page.url }}">{{ page.title }}</a> - {{ page.meta.git_creation_date_localized_raw_iso_date }}
{% for tag in page.meta.tags %}
@ -16,5 +19,5 @@
{% endfor %}
</ul>
<p><a href="{{ 'tags/'|url }}">View all &rarr;</a></p>
<p><a href="{{ notes_index.url }}">View all {{ notes|length }} &rarr;</a></p>
{% endblock %}