cotes2020 / jekyll-theme-chirpy

A minimal, responsive, and feature-rich Jekyll theme for technical writing.
https://chirpy.cotes.page
MIT License
7.52k stars 5.88k forks source link

How can I globally modify the contents of headers in all pages? #641

Closed diyanqi closed 2 years ago

diyanqi commented 2 years ago

Checklist

Description

I want to change a MathJax source because it doesn't work on my blog whenever I type $...$ or $$...$$. And sometimes I think there just isn't any MathJax render in the Chirpy Theme. However, when I tried to edit the JS file by means of this, I could not find a folder named _includes. So I created one but it still didn't work.

How can I solve it? What file should I edit?

rehnertz commented 2 years ago

Just set math: true in the frontmatter of posts, then $...$ and $$...$$ will work. The CDN is listed in _data/assets/cross_origin.yml and can be modified by yourself.

BTW, Mathjax3 disables the use of $ by default, and chirpy enables it in _includes/js-selector.html:

MathJax = {
  tex: {
    inlineMath: [              /* start/end delimiter pairs for in-line math */
      ['$','$'],
      ['\\(','\\)']
    ],
    displayMath: [             /* start/end delimiter pairs for display math */
      ['$$', '$$'],
      ['\\[', '\\]']
    ]
  }
};

If you really want to change the head, just copy _includes/head.html to your project (only if you are using the chirpy starter with a gem theme) from the repository and modify it.

diyanqi commented 2 years ago

Just set math: true in the frontmatter of posts, then $...$ and $$...$$ will work. The CDN is listed in _data/assets/cross_origin.yml and can be modified by yourself.

BTW, Mathjax3 disables the use of $ by default, and chirpy enables it in _includes/js-selector.html:

MathJax = {
  tex: {
    inlineMath: [              /* start/end delimiter pairs for in-line math */
      ['$','$'],
      ['\\(','\\)']
    ],
    displayMath: [             /* start/end delimiter pairs for display math */
      ['$$', '$$'],
      ['\\[', '\\]']
    ]
  }
};

If you really want to change the head, just copy _includes/head.html to your project (only if you are using the chirpy starter with a gem theme) from the repository and modify it.

Thanks for you answering. 您回答得好快啊(

By following your steps, I’ve already succeeded in rendering the MathJax blocks. However, it’s inconvenient to set math: true for each passage as some of the Jekyll passage editors do not support editing frontmatter data. Also, the heading content of the passage will be presented in the homepage as well, which contains some math formulas. So I wonder if there is a way to set MathJax active globally?

diyanqi commented 2 years ago

Something like this: 39002504-540B-4A53-920F-86B23354FC16

diyanqi commented 2 years ago

This picture is captured from the homepage.

rehnertz commented 2 years ago

Just set math: true in the frontmatter of posts, then $...$ and $$...$$ will work. The CDN is listed in _data/assets/cross_origin.yml and can be modified by yourself. BTW, Mathjax3 disables the use of $ by default, and chirpy enables it in _includes/js-selector.html:

MathJax = {
  tex: {
    inlineMath: [              /* start/end delimiter pairs for in-line math */
      ['$','$'],
      ['\\(','\\)']
    ],
    displayMath: [             /* start/end delimiter pairs for display math */
      ['$$', '$$'],
      ['\\[', '\\]']
    ]
  }
};

If you really want to change the head, just copy _includes/head.html to your project (only if you are using the chirpy starter with a gem theme) from the repository and modify it.

Thanks for you answering. 您回答得好快啊(

By following your steps, I’ve already succeeded in rendering the MathJax blocks. However, it’s inconvenient to set math: true for each passage as some of the Jekyll passage editors do not support editing frontmatter data. Also, the heading content of the passage will be presented in the homepage as well, which contains some math formulas. So I wonder if there is a way to set MathJax active globally?

(正好在线,邮箱收到回复了而已 2333) It seems that there is no perfect solution to display maths in excerpt (or maybe you need to write a ruby plugin) even if you enable Mathjax globally:

---
title: 测试
date: 2022-07-15 20:10:00 +0800
math: true
---

你 $a = 1$

$$
    \sqrt{b^2 + c}
$$

Found a solution: use \\[ ... \\] instead of $$ ... $$ for block maths will work.

Check _includes/js-selector.html:

{% if page.math %}
  <!-- MathJax -->
  <script>
  /* see: <https://docs.mathjax.org/en/latest/options/input/tex.html#tex-options> */
  MathJax = {
    tex: {
      inlineMath: [              /* start/end delimiter pairs for in-line math */
        ['$','$'],
        ['\\(','\\)']
      ],
      displayMath: [             /* start/end delimiter pairs for display math */
        ['$$', '$$'],
        ['\\[', '\\]']
      ]
    }
  };
  </script>
  <script src="{{ site.data.assets[origin].polyfill.js | relative_url }}"></script>
  <script id="MathJax-script" async src="{{ site.data.assets[origin].mathjax.js | relative_url }}">
  </script>
{% endif %}

The code between {% if page.math %} and {% endif %} is what imports Mathjax. Copy _layouts/home.html to your project with the same path, and paste the code at the end. Mathjax should be imported in homepage.

I guess you are using Chirpy Starter which is based on a ruby gem theme. Your local file with the same path will completely override the file of the theme, so you need to copy the original file and then modify it.

To enable math by default for all posts, one way is to delete the if in js-selector.html. Another is mentioned in Jekyll Documentation which can set math: true by default.

defaults:
  - scope:
      path: ''          # An empty string here means all files in the project
      type: posts
    values:
      layout: post
      comments: true    # Enable comments in posts.
      toc: true         # Display TOC column in posts.
      math: true # <======== ENABLE MATHJAX BY DEFAULT
      # DO NOT modify the following parameter unless you are confident enough
      # to update the code of all other post links in this project.
      permalink: /posts/:title/
diyanqi commented 2 years ago

我滴任务完成啦!

Thanks for your answering. I achieved my goal now.

sukhjinder-kumar commented 1 year ago

I followed the above instructions, however, I don't seem to get desired results. I tried using \\( ... \\), and \\[ ... \\] instead of $ ... $ and $$ ... $$ as well as downloaded _layouts/home.html and appended the code snippet mentioned at the end. But nothing changed, still, posts on the home page don't render math equations. Any suggestion as to what I am doing wrong?