load mathjax only when mathjax is needed #887
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
原始代码中若开启
mathjax
选项,则所有页面都会加载mathjax。如果100篇博文里只有10篇有公式,那么对于另外90篇博文来说,加载mathjax就太浪费了。此PR的目的是实现仅当需要时再加载mathjax。改动只有两处:
per_page
,默认值为false
{% if not theme.mathjax.per_page or (page.total or page.mathjax)%}
(其他行只是修改了代码缩进)最终的结果是:
theme.mathjax.enable==false
,则所有页面都不加载mathjaxtheme.mathjax.enable==true
,且theme.mathjax.per_page==false
,则所有页面都加载mathjax。由于theme.mathjax.per_page
默认值为false
,所以增加这个参数不会造成兼容性问题,不介意所有页面都加载mathjax的只要不修改这个参数就可以了theme.mathjax.enable==true
,且theme.mathjax.per_page==true
,则意味着会按需加载mathjax。此时需要在每个有公式的博文的front-matter中加上mathjax: true
。新增的条件语句的后半部分的作用是仅在index页以及mathjax: true
的页面加载mathjax。这里的一个小trick是,使用
page.total
来判断当前页面是否是index。对于index页面,page.total
是当前页面的总post数目,相当于true,对于post页面,page.total
是未定义的,相当于false。