Skip to content

Commit 79fc1e5

Browse files
committed
feat: mathematical and chemical content inside code blocks
1 parent 36d5001 commit 79fc1e5

File tree

9 files changed

+113
-21
lines changed

9 files changed

+113
-21
lines changed

.github/workflows/update-resources.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
uses: peaceiris/actions-hugo@v3
1818
with:
1919
extended: true
20-
hugo-version: 0.147.5
20+
hugo-version: 0.147.8
2121
- name: setup node
2222
uses: actions/setup-node@v4
2323
with:

config/_default/hugo.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
mediaTypes:
2+
font/woff:
3+
suffixes:
4+
- woff
5+
font/woff2:
6+
suffixes:
7+
- woff2

exampleSite/content/english/post/math-typesetting.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,48 @@ $$
3939
Chemical equations:
4040

4141
\[
42-
\tag\*{(2)} \ce{Zn^2+ <=>[+ 2OH-][+ 2H+] $\underset{\text{amphoteric hydroxide}}{\ce{Zn(OH)2 v}}$ <=>[+ 2OH-][+ 2H+] $\underset{\text{tetrahydroxozincate}}{\ce{[Zn(OH)4]^2-}}$}
42+
\tag*{(2)} \ce{Zn^2+ <=>[+ 2OH-][+ 2H+] $\underset{\text{amphoteric hydroxide}}{\ce{Zn(OH)2 v}}$ <=>[+ 2OH-][+ 2H+] $\underset{\text{tetrahydroxozincate}}{\ce{[Zn(OH)4]^2-}}$}
4343
\]
4444

4545
**Note:** Use the online reference of [Supported TeX Functions](https://katex.org/docs/supported.html)
46+
47+
### Mathematical formulae inside code blocks
48+
49+
As an alternative to the standard syntax used above, formulae can also be
50+
authored using a
51+
[GLFM math block](https://docs.gitlab.com/ee/user/markdown.html#math):
52+
53+
````markdown
54+
The probability of getting \(k\) heads when flipping \(n\) coins is:
55+
56+
```math
57+
\tag*{(1)} P(E) = {n \choose k} p^k (1-p)^{n-k}
58+
```
59+
````
60+
61+
This `math` block renders to:
62+
63+
The probability of getting \(k\) heads when flipping \(n\) coins is:
64+
65+
```math
66+
\tag*{(1)} P(E) = {n \choose k} p^k (1-p)^{n-k}
67+
```
68+
69+
### Chemical equations inside code blocks
70+
71+
You can use a code block adorned with `chem` in order to render
72+
a chemical equation:
73+
74+
````markdown
75+
```chem
76+
\tag*{(2)} \ce{Hg^2+ ->[I-] $\underset{\mathrm{red}}{\ce{HgI2}}$ ->[I-] $\underset{\mathrm{red}}{\ce{[Hg^{II}I4]^2-}$}
77+
```
78+
````
79+
80+
This `chem` block renders to:
81+
82+
<!-- prettier-ignore-start -->
83+
```chem
84+
\tag*{(2)} \ce{Hg^2+ ->[I-] $\underset{\mathrm{red}}{\ce{HgI2}}$ ->[I-] $\underset{\mathrm{red}}{\ce{[Hg^{II}I4]^2-}}$}
85+
```
86+
<!-- prettier-ignore-end -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ partial "math.html" . -}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ partial "math.html" . -}}

layouts/partials/head.html

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,6 @@
235235
<!-- KaTeX-->
236236
{{ $noop := .WordCount }}
237237
{{ if .Page.Store.Get "hasMath" }}
238-
{{ $katex_css_url := printf "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex%s.css" (cond hugo.IsProduction ".min" "") -}}
239-
{{ with try (resources.GetRemote $katex_css_url) -}}
240-
{{ with .Err -}}
241-
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}}
242-
{{ else with.Value -}}
243-
{{ with resources.Copy (printf "css/katex%s.css" (cond hugo.IsProduction ".min" "")) . }}
244-
{{ $secureCSS := . | resources.Fingerprint "sha512" -}}
245-
<link
246-
rel="stylesheet"
247-
href="{{- .RelPermalink -}}"
248-
integrity="{{- $secureCSS.Data.Integrity -}}"
249-
crossorigin="anonymous"
250-
/>
251-
{{ end -}}
252-
{{ end -}}
253-
{{ end -}}
238+
{{ partial "katex.html" . }}
254239
{{ end }}
255240
</head>

layouts/partials/katex.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{{/* stylesheet */ -}}
2+
3+
{{ $cssFile := cond hugo.IsProduction "katex.min.css" "katex.css" -}}
4+
{{ $cssUrl := printf "https://unpkg.com/katex@latest/dist/%s" $cssFile -}}
5+
{{ with try (resources.GetRemote $cssUrl) -}}
6+
{{ with .Err -}}
7+
{{ errorf "Could not retrieve KaTeX css file from CDN. Reason: %s." . -}}
8+
{{ else with .Value -}}
9+
{{ with resources.Copy (printf "css/%s" $cssFile) . -}}
10+
{{ $cssHash := . | fingerprint "sha512" -}}
11+
<link
12+
rel="stylesheet"
13+
href="{{- .RelPermalink -}}"
14+
integrity="{{- $cssHash.Data.Integrity -}}"
15+
crossorigin="anonymous">
16+
{{ end -}}
17+
{{ end -}}
18+
{{ end -}}
19+
20+
{{/* font files */ -}}
21+
22+
{{ $fontFiles := slice -}}
23+
{{ $data := dict -}}
24+
{{ $url := "https://unpkg.com/katex@latest/dist/fonts?meta" -}}
25+
{{ with try (resources.GetRemote $url) -}}
26+
{{ with .Err -}}
27+
{{ errorf "%s" . -}}
28+
{{ else with ( .Value | unmarshal ) -}}
29+
{{ range .files -}}
30+
{{ $fontFiles = $fontFiles | append .path -}}
31+
{{ end -}}
32+
{{ else -}}
33+
{{ errorf "Unable to get remote resource %q" $url -}}
34+
{{ end -}}
35+
{{ end -}}
36+
37+
{{ range $fontFile := $fontFiles -}}
38+
{{ $fontUrl := (printf "https://unpkg.com/katex@latest%s" $fontFile) -}}
39+
{{ with try (resources.GetRemote $fontUrl) -}}
40+
{{ with .Err -}}
41+
{{ errorf "Could not retrieve KaTeX font file from CDN. Reason: %s." . -}}
42+
{{ else with .Value -}}
43+
{{ with resources.Copy (printf "css/fonts/%s" (replace $fontFile "/dist/fonts/" "")) . -}}
44+
{{ .Publish -}}
45+
{{ end -}}
46+
{{ end -}}
47+
{{ end -}}
48+
{{ end -}}

layouts/partials/math.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{{ $opts := dict "output" "htmlAndMathml" "displayMode" (not (eq ($.Type) "inline")) -}}
2+
{{ with try (transform.ToMath .Inner $opts) -}}
3+
{{ with .Err -}}
4+
{{ errorf "Unable to render mathematical markup to HTML using the transform.ToMath function. The KaTeX display engine threw the following error: %s." . $.Position -}}
5+
{{ else -}}
6+
{{ .Value -}}
7+
{{ $.Page.Store.Set "hasMath" true -}}
8+
{{ end -}}
9+
{{ end -}}

netlify.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"""
1111

1212
[build.environment]
13-
HUGO_VERSION = "0.147.5"
14-
DART_SASS_VERSION = "1.89.0"
13+
HUGO_VERSION = "0.147.8"
14+
DART_SASS_VERSION = "1.89.1"
1515
NODE_VERSION = "24"
16-
GO_VERSION = "1.24.3"
16+
GO_VERSION = "1.24.4"
1717
HUGO_ENV = "production"
1818
HUGO_THEME = "repo"
1919
HUGO_BASEURL = "https://anatole-demo.netlify.app"

0 commit comments

Comments
 (0)