From 76fa2633792b59aef843f363e6390ca2cd213eb4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 15 Sep 2023 22:05:07 +0100 Subject: [PATCH] Allow custom content for tag list page --- hooks/tags.py | 20 +++++++++++++------- theme/tag-list-template.md | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/hooks/tags.py b/hooks/tags.py index 5238091..cc74067 100644 --- a/hooks/tags.py +++ b/hooks/tags.py @@ -22,9 +22,7 @@ def on_files(files: Files, config): tags.update(metadata.get("tags", [])) for tag in tags: - found_tag_page = files.get_file_from_path(f"tags/{tag}.md") - - if found_tag_page: + if found_tag_page := files.get_file_from_path(f"tags/{tag}.md"): with open(found_tag_page.abs_src_path) as f: content, _ = meta.get_data(f.read()) @@ -32,18 +30,26 @@ def on_files(files: Files, config): else: content = "" - filename = f"{tag}.md" + filename = f"tags/{tag}.md" - write_file(template.render(tag=tag, content=content).encode(), os.path.join(TAGS_TEMP_DIR, "tags", filename)) + write_file(template.render(tag=tag, content=content).encode(), os.path.join(TAGS_TEMP_DIR, filename)) files.append(File( - path=f"tags/{filename}", + path=filename, src_dir=TAGS_TEMP_DIR, dest_dir=config["site_dir"], use_directory_urls=True )) - write_file(list_template.render().encode(), os.path.join(TAGS_TEMP_DIR, "tags", "index.md")) + if found_index_page := files.get_file_from_path("tags/index.md"): + with open(found_index_page.abs_src_path) as f: + content, _ = meta.get_data(f.read()) + + files.remove(found_index_page) + else: + content = "" + + write_file(list_template.render(content=content).encode(), os.path.join(TAGS_TEMP_DIR, "tags", "index.md")) files.append(File( path="tags/index.md", diff --git a/theme/tag-list-template.md b/theme/tag-list-template.md index a2a692e..4d68fb0 100644 --- a/theme/tag-list-template.md +++ b/theme/tag-list-template.md @@ -3,4 +3,4 @@ template: tag.html title: All tags --- -All the tags +{{ content }}