Skip to content

Commit

Permalink
Add notes list and optimise getting notes
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Feb 16, 2024
1 parent 3108f3a commit c988131
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/notes/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
template: all.html
hide: [toc]
---

# Notes
28 changes: 28 additions & 0 deletions hooks/notes.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ theme:
features:
- navigation.top
- navigation.sections
- navigation.index
- navigation.indexes
- toc.follow
- search.suggest
- navigation.top
Expand Down Expand Up @@ -102,6 +102,7 @@ plugins:
exclude:
- tags.md
- README.md
- notes/index.md
- minify_html
- social
- rss:
Expand Down
16 changes: 16 additions & 0 deletions theme/all.html
Original file line number Diff line number Diff line change
@@ -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 %}
7 changes: 5 additions & 2 deletions theme/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -16,5 +19,5 @@ <h2>Recent notes</h2>
{% 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 %}

0 comments on commit c988131

Please sign in to comment.