Compare commits
2 Commits
5e9566cc31
...
0e412041f3
Author | SHA1 | Date |
---|---|---|
Jake Howard | 0e412041f3 | |
Jake Howard | 2cd086c57c |
|
@ -1,5 +1,4 @@
|
|||
nav:
|
||||
- README.md
|
||||
- tags.md
|
||||
- colophon.md
|
||||
- ...
|
||||
|
|
|
@ -4,7 +4,7 @@ hide: [toc]
|
|||
|
||||
# Colophon
|
||||
|
||||
This site is built with [`mkdocs`](https://www.mkdocs.org/) and [`mkdocs-material`](https://squidfunk.github.io/mkdocs-material/). Sure, it's designed for docs, but with a few modifications (like the [`tags`](https://squidfunk.github.io/mkdocs-material/plugins/tags/) plugin) it can be used for notes.
|
||||
This site is built with [`mkdocs`](https://www.mkdocs.org/) and [`mkdocs-material`](https://squidfunk.github.io/mkdocs-material/). Sure, it's designed for docs, but with a few modifications (like the [`tags`](https://squidfunk.github.io/mkdocs-material/plugins/tags/) plugin) it can be used for notes. I made some fairly large modifications to how tags work, so the sidebar is structured by tag rather than by directory (directories are fairly arbitrary, and might get removed entirely in future).
|
||||
|
||||
It's been through a few iterations in the past, starting with [`gatsby-theme-code-notes`](https://github.com/mrmartineau/gatsby-theme-code-notes/), on to [`hugo-theme-notes`](https://github.com/RealOrangeOne/hugo-theme-notes/), and finally here.
|
||||
|
||||
|
|
|
@ -2,4 +2,4 @@
|
|||
hide: [toc]
|
||||
---
|
||||
|
||||
# Tags
|
||||
# Notes
|
|
@ -1,2 +1,80 @@
|
|||
import jinja2
|
||||
from mkdocs.structure.nav import Navigation, Section
|
||||
from collections import defaultdict
|
||||
from mkdocs.utils import meta
|
||||
from mkdocs.plugins import event_priority
|
||||
|
||||
@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)
|
||||
|
||||
|
||||
def get_notes_from_nav(nav: Navigation):
|
||||
notes_section = next(item for item in nav if item.is_section and item.title == "Notes")
|
||||
|
||||
notes = [item for item in nav.pages if item.is_page and notes_section in item.ancestors and item.file.src_uri != "notes/index.md"]
|
||||
|
||||
return notes_section, notes
|
||||
|
||||
|
||||
@jinja2.pass_context
|
||||
def get_notes(context):
|
||||
notes_section, notes = get_notes_from_nav(context["nav"])
|
||||
|
||||
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
|
||||
|
||||
|
||||
class TagSection(Section):
|
||||
"""
|
||||
A modified section which is active if any of its children are active
|
||||
(regardless of what mkdocs tells it).
|
||||
"""
|
||||
@property
|
||||
def active(self):
|
||||
return any(c.active for c in self.children)
|
||||
|
||||
@active.setter
|
||||
def active(self, value: bool):
|
||||
pass
|
||||
|
||||
|
||||
@event_priority(100)
|
||||
def on_nav(nav: Navigation, config, files):
|
||||
"""
|
||||
Structure the nav sections by
|
||||
"""
|
||||
notes_section, notes = get_notes_from_nav(nav)
|
||||
|
||||
notes_page = notes_section.children[0]
|
||||
|
||||
pages_to_tag = defaultdict(list)
|
||||
|
||||
orphan_pages = []
|
||||
|
||||
for note in notes:
|
||||
with open(note.file.abs_src_path) as f:
|
||||
_, metadata = meta.get_data(f.read())
|
||||
|
||||
if tags := metadata.get("tags"):
|
||||
for tag in tags:
|
||||
pages_to_tag[tag].append(note)
|
||||
else:
|
||||
orphan_pages.append(note)
|
||||
|
||||
tag_sections = [
|
||||
TagSection(tag, pages)
|
||||
for tag, pages in pages_to_tag.items()
|
||||
]
|
||||
|
||||
notes_section.children = tag_sections + orphan_pages + [notes_page]
|
||||
|
|
|
@ -26,7 +26,7 @@ theme:
|
|||
features:
|
||||
- navigation.top
|
||||
- navigation.sections
|
||||
- navigation.index
|
||||
- navigation.indexes
|
||||
- toc.follow
|
||||
- search.suggest
|
||||
- navigation.top
|
||||
|
@ -95,13 +95,13 @@ plugins:
|
|||
order_by: title
|
||||
- tags:
|
||||
enabled: true
|
||||
tags_file: tags.md
|
||||
tags_file: notes/index.md
|
||||
- git-revision-date-localized:
|
||||
enable_creation_date: true
|
||||
fallback_to_build_date: true
|
||||
exclude:
|
||||
- tags.md
|
||||
- README.md
|
||||
- notes/index.md
|
||||
- minify_html
|
||||
- social
|
||||
- rss:
|
||||
|
|
|
@ -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 →</a></p>
|
||||
<p><a href="{{ notes_index.url }}">View all {{ notes|length }} →</a></p>
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in New Issue