From 9e8bddbdb7f0a157f8de2a7ba243f7bd753beb36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Krzy=C5=9Bk=C3=B3w?= Date: Tue, 1 Oct 2024 21:40:56 +0200 Subject: [PATCH] feat: support head_tags fix: latest_blog_posts use head_tags --- .../plugins/latest_blog_posts/plugin.py | 31 ++++++++++++++++--- mkdocs_nype/templates/nype-base.html | 19 ++++++++++++ pyproject.toml | 2 +- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/mkdocs_nype/plugins/latest_blog_posts/plugin.py b/mkdocs_nype/plugins/latest_blog_posts/plugin.py index 939f09b..4f58ecd 100644 --- a/mkdocs_nype/plugins/latest_blog_posts/plugin.py +++ b/mkdocs_nype/plugins/latest_blog_posts/plugin.py @@ -80,6 +80,24 @@ def on_page_markdown(self, markdown: str, page: Page, config: MkDocsConfig, file js = f"\n" lines[0] = f"{css}{js}{lines[0]}" + # Add timeago so that users don't have to + nype_config = page.meta.get("nype_config") + if nype_config is None: + page.meta["nype_config"] = nype_config = {} + + head_tags = nype_config.get("head_tags") + if head_tags is None: + nype_config["head_tags"] = head_tags = [] + + timeago_tag = { + "name": "script", + "attributes": { + "src": "https://cdnjs.cloudflare.com/ajax/libs/timeago.js/4.0.2/timeago.min.js", + "defer": "", + }, + } + head_tags.append(timeago_tag) + return "\n".join(lines) @@ -146,6 +164,7 @@ def insert_latest_posts(line, config: MkDocsConfig): date = post.config.date["created"].strftime(strftime) date_span = f'{date}' li_entries += f" - {date_span}\n [{text}]({href})\n" + li_entries += ' {: class="nype-latest-post-entry" }\n' elif display == "html_simple": insert_body = HTML_SIMPLE_TEMPLATE blog_index_url = instance.blog.file.url @@ -167,6 +186,8 @@ def insert_latest_posts(line, config: MkDocsConfig): blog_index_url=blog_index_url, read_more=read_more, title=title, + title_attr_list='{: class="nype-latest-posts-title" }', + read_more_attr_list='{: class="nype-latest-posts-read-more" }', ) @@ -217,6 +238,7 @@ def render_html_grid_li(post, strftime): MARKDOWN_GRID_TEMPLATE: str = ( """ - {title} +{title_attr_list} --- @@ -224,7 +246,8 @@ def render_html_grid_li(post, strftime): --- - [{read_more}]({blog_index_url}) + [{read_more}]({blog_index_url}) + {read_more_attr_list} """.lstrip() ) @@ -254,12 +277,10 @@ def render_html_grid_li(post, strftime): color: var(--md-default-fg-color--light); display: block; } -.grid.cards ul { +.nype-latest-post-entry { + margin-left: 0 !important; list-style: none; } -.grid.cards ul li { - margin-left: 0; -} """.strip() ) diff --git a/mkdocs_nype/templates/nype-base.html b/mkdocs_nype/templates/nype-base.html index 44cc6aa..60405da 100644 --- a/mkdocs_nype/templates/nype-base.html +++ b/mkdocs_nype/templates/nype-base.html @@ -124,6 +124,25 @@ {% endif %} {%- set _ = page_nype_config.js.update({ name: value })-%} {% endfor %} + + {#- Set tags with external assets. Mostly for usage in plugins like latest_blog_post -#} + {%- if page_nype_config.head_tags -%} + {%- for head_tag in page_nype_config.head_tags -%} + <{{ head_tag.name -}} + {%- for attr_name, attr_value in head_tag.attributes.items() %} + {% if attr_value -%} + {{ attr_name }}="{{ attr_value }}"{# Whitespace -#} + {% else -%} + {{ attr_name }}{# Whitespace -#} + {% endif -%} + {% endfor -%} + > + {%- if head_tag.name == "script" -%} + + {% endif %} + {% endfor %} + {% endif %} + diff --git a/pyproject.toml b/pyproject.toml index 23744a7..461918a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ build-backend = "hatchling.build" [project] name = "mkdocs-nype" -version = "0.16.8" +version = "0.16.9" description = "MkDocs theme for Nype MkDocs projects, extends the Material for MkDocs theme" authors = [ { name = "Kamil Krzyśków", email = "kk@npe.cm" }