Install

brew install hugo

themes/xxx/layouts/partials/ 下新增 backlink.html​,内容如下。

{{ $re := $.File.BaseFileName }}
{{ $backlinks := slice }}
{{ range .Site.AllPages }}
   {{ if and (findRE $re .RawContent) (not (eq $re .File.BaseFileName)) }}
          {{ $backlinks = $backlinks | append . }}
   {{ end }}
{{ end }}

{{ if gt (len $backlinks) 0 }}
  <div class="bl-section">
        <h4>Links to this note</h4>
        <div class="backlinks">
          <ul>
           {{ range $backlinks }}
                  <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
           {{ end }}
         </ul>
        </div>
  </div>
{{ else  }}
  <div class="bl-section">
        <h4>No notes link to this note</h4>
  </div>
{{ end }}

themes/xxx/layouts/_default/single.html 中增加下面代码,放到合适的地方。

{{ partial "backlinks.html" . }}

放在文章下方的话可以插入如下位置

<div class="post-content">
    {{- if not (.Param "disableAnchoredHeadings") }}
    {{- partial "anchored_headings.html" .Content -}}
    {{- else }}{{ .Content }}{{ end }}
    {{ partial "backlinks.html" . }}
</div>

MathJax support

方案1

themes/xxx/layouts/_default/single.htmlheader 中增加下面代码。

<script type="text/javascript"
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

方案2

增加 themes/xxx/layouts/partials/mathjax.html 文件,并在其中增加下面代码,实现即便是页面间的跳转,也可以实时渲染公式。

<script>
  MathJax = {
    tex: {
      inlineMath: [['$', '$'], ['\\(', '\\)']],
      displayMath: [['$$','$$'], ['\\[', '\\]']],
      processEscapes: true,
      processEnvironments: true
    },
    options: {
      skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre']
    }
  };

  window.addEventListener('load', (event) => {
      document.querySelectorAll("mjx-container").forEach(function(x){
        x.parentElement.classList += 'has-jax'})
    });

</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script type="text/javascript" id="MathJax-script" async
  src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

<style>
code.has-jax {
    font: inherit;
    font-size: 100%;
    background: inherit;
    border: inherit;
    color: #515151;
}
</style>

themes/xxx/layouts/partials/header.html 中增加下面代码,放到合适的地方。

{{ partial "mathjax.html" . }}

如果不想所有页面都加载 MathJax,就添加下面代码。

{{ if .Params.mathjax }}{{ partial "mathjax.html" . }}{{ end }}

并通过 Custom Front-matter Parameters 设置 mathjax: true​。具体是添加下面代码到需要导出并用 MathJax 展示的 org 文件头部,导出后就是 .md 文件中的 metadata。

#+HUGO_CUSTOM_FRONT_MATTER: :mathjax true

另外对于一些用到额外包以及复杂的块 Latex,MathJax 的支持并不是很好。可以将块导出为 svg,即在头部增加 #+options: tex:dvisvgm 就可以了。

增强

MathJax 对于 algorithm2e 等一些算法包不支持,可以通过 pseudocode.js 支持页面渲染!


Links to this note