Skip to content

Commit

Permalink
feat: support head_tags
Browse files Browse the repository at this point in the history
fix: latest_blog_posts use head_tags
  • Loading branch information
kamilkrzyskow committed Oct 1, 2024
1 parent 29c10d4 commit 9e8bddb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
31 changes: 26 additions & 5 deletions mkdocs_nype/plugins/latest_blog_posts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ def on_page_markdown(self, markdown: str, page: Page, config: MkDocsConfig, file
js = f"<script>{minify_plugin.jsmin.jsmin(JS_TEMPLATE)}</script>\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)


Expand Down Expand Up @@ -146,6 +164,7 @@ def insert_latest_posts(line, config: MkDocsConfig):
date = post.config.date["created"].strftime(strftime)
date_span = f'<span class="nype-latest-post-date">{date}</span>'
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
Expand All @@ -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" }',
)


Expand Down Expand Up @@ -217,14 +238,16 @@ def render_html_grid_li(post, strftime):
MARKDOWN_GRID_TEMPLATE: str = (
"""
- {title}
{title_attr_list}
---
{li_entries}
---
[{read_more}]({blog_index_url})
[{read_more}]({blog_index_url})
{read_more_attr_list}
""".lstrip()
)
Expand Down Expand Up @@ -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()
)

Expand Down
19 changes: 19 additions & 0 deletions mkdocs_nype/templates/nype-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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" -%}
</{{ head_tag.name }}>
{% endif %}
{% endfor %}
{% endif %}

<script>
const nypeScriptConfig = JSON.parse('{{ page_nype_config.js | tojson }}');
</script>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }
Expand Down

0 comments on commit 9e8bddb

Please sign in to comment.