Convert context to JSON when it is being passed to JavaScript

Fixes #850
This commit is contained in:
Dougal Matthews
2016-03-01 10:20:45 +00:00
parent 3446d472e9
commit 1c252ba4bb
4 changed files with 12 additions and 3 deletions

View File

@@ -19,6 +19,7 @@ You can determine your currently installed version using `mkdocs --version`:
`mkdocs build` and `mkdocs serve` (#832)
* Fixed issues with Unicode filenames under Windows and Python 2. (#833)
* Improved the styling of in-line code in the MkDocs theme. (#718)
* Bugfix: convert variables to JSON when being passed to JavaScript (#850)
## Version 0.15.3 (2016-02-18)

View File

@@ -11,6 +11,7 @@ import jinja2
import json
from mkdocs import nav, search, utils
from mkdocs.utils import filters
from mkdocs.relative_path_ext import RelativePathExtension
import mkdocs
@@ -228,6 +229,7 @@ def build_pages(config, dump_json=False):
site_navigation = nav.SiteNavigation(config['pages'], config['use_directory_urls'])
loader = jinja2.FileSystemLoader(config['theme_dir'] + [config['mkdocs_templates'], ])
env = jinja2.Environment(loader=loader)
env.filters['tojson'] = filters.tojson
search_index = search.SearchIndex()
build_template('404.html', env, config, site_navigation)

View File

@@ -27,9 +27,9 @@
{% if current_page %}
<script>
// Current page data
var mkdocs_page_name = "{{ page_title }}";
var mkdocs_page_input_path = "{{ current_page.input_path }}";
var mkdocs_page_url = "{{ current_page.abs_url }}";
var mkdocs_page_name = {{ page_title|tojson|safe }};
var mkdocs_page_input_path = {{ current_page.input_path|tojson|safe }};
var mkdocs_page_url = {{ current_page.abs_url|tojson|safe }};
</script>
{% endif %}
<script src="{{ base_url }}/js/jquery-2.1.1.min.js"></script>

6
mkdocs/utils/filters.py Normal file
View File

@@ -0,0 +1,6 @@
import json
import jinja2
def tojson(obj, **kwargs):
return jinja2.Markup(json.dumps(obj, **kwargs))