Skip to content

Commit

Permalink
Merge pull request #18 from liang2kl/dev
Browse files Browse the repository at this point in the history
Make MkDocs' variables available inside blog templates
  • Loading branch information
liang2kl authored Feb 25, 2022
2 parents 2be147f + 836d34c commit 94ea126
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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

0 comments on commit 94ea126

Please sign in to comment.