Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make MkDocs' variables available inside blog templates #18

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ These variables are available inside your template:
- `show_total`: whether to show the total number of the blog

You can refer to the original template for help.

## Access to the Original MkDocs Template Variables

Use `mkdocs_context` inside your template to access variables that are available inside MkDocs' templates. For example, to access `base_url`:

```jinja
<img href="{{ mkdocs_context.base_url }}/img/1.png" alt="">
```
8 changes: 7 additions & 1 deletion mkdocs_blogging_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class BloggingPlugin(BasePlugin):
tags = {}
tags_page_html = None
tags_index_url = ""
mkdocs_template_context = None

util = Util()

Expand Down Expand Up @@ -163,6 +164,10 @@ def on_config(self, config):
self.blog_pages = []
self.tags = {}

def on_template_context(self, context, template_name, config):
self.mkdocs_template_context = context
return context

def on_page_markdown(self, markdown, page, config, files):
if "tags" in self.features and "tags" in page.meta:
tags = page.meta["tags"]
Expand Down Expand Up @@ -257,7 +262,8 @@ def generate_html(self, pages) -> str:
pages=blog_pages, page_size=self.size,
paging=self.paging, is_revision=self.sort["by"] == "revision",
show_total=self.show_total, theme_options=theme_options,
index_url=self.tags_index_url, show_tags="tags" in self.features
index_url=self.tags_index_url, show_tags="tags" in self.features,
mkdocs_context=self.mkdocs_template_context
)

def sorted_pages(self, pages):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="mkdocs-blogging-plugin",
version="1.2.1",
version="1.3.0",
description="Mkdocs plugin that generates a blog index page sorted by creation date.",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
Expand Down