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

load mathjax only when mathjax is needed #887

Merged
merged 1 commit into from May 17, 2016
Merged

Conversation

seisman
Copy link
Contributor

@seisman seisman commented May 14, 2016

原始代码中若开启mathjax选项,则所有页面都会加载mathjax。如果100篇博文里只有10篇有公式,那么对于另外90篇博文来说,加载mathjax就太浪费了。此PR的目的是实现仅当需要时再加载mathjax。

改动只有两处:

  1. 加了配置参数 per_page,默认值为false
  2. 加了判断条件 {% if not theme.mathjax.per_page or (page.total or page.mathjax)%} (其他行只是修改了代码缩进)

最终的结果是:

  1. theme.mathjax.enable==false,则所有页面都不加载mathjax
  2. theme.mathjax.enable==true,且 theme.mathjax.per_page==false,则所有页面都加载mathjax。由于theme.mathjax.per_page默认值为false,所以增加这个参数不会造成兼容性问题,不介意所有页面都加载mathjax的只要不修改这个参数就可以了
  3. 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。

@iissnan iissnan merged commit 28e80ea into iissnan:5.1.0 May 17, 2016
@iissnan
Copy link
Owner

iissnan commented May 17, 2016

Thanks. 👍

@duohappy
Copy link

Good job! Thank you

@Simpleyyt
Copy link

Simpleyyt commented Jul 7, 2017

为什么要叫 per_page?
为什么判断首页不用 is_home?

@stevenjoezhang
Copy link
Contributor

is_home() 在这里改了 theme-next/hexo-theme-next#78
per_page 的讨论见 theme-next/hexo-theme-next#257

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants