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

Support math expressions in typst style #57

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions USAGE-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ Hi, My name is ....

- 如果读者通过 RSS 阅读你的文章,这些 Callouts 将会显示为普通的 `<blockquote>`

### KaTeX
### 数学公式

- 在文章 front matter 中设置 `math = true` 开启 [KaTeX](https://katex.org/) 支持
- 在文章 front matter 中设置 `math = true` 或 `math = "katex"` 开启 [KaTeX](https://katex.org/) 公式渲染。使用 `$...$` 标记行内公式,`$$...$$` 标记块级公式。

- 行内公式 `$...$`,块级公式 `$$...$$`
- 设置`math = "typst"` 开启 [Typst](https://typst.app/) 公式渲染. 使用 `$...$` 标记行内公式,`$ ... $` ($内侧两端加空格)标记块级公式。

### Mermaid

Expand Down
6 changes: 3 additions & 3 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ Now the myblog directory may looks like this:

- If people read your posts via rss reader, these callouts will appear as normal `<blockquote>`

### KaTeX
### Math

- Set `math = true` in the front matter to enable [KaTeX](https://katex.org/) support
- Set `math = true` or `math = "katex"`in the front matter to enable formula rending with [KaTeX](https://katex.org/): `$...$` for inline formula, `$$...$$` for block-level formula.

- Inline formula `$...$`, block-level formula `$$...$$`
- Set `math = "typst"` to enable formula rendering with [Typst](https://typst.app/): `$...$` for inline formula, `$ ... $` (insert space at start and end) for block-level formula.

### Mermaid

Expand Down
Binary file added static/js/c2bda2230c7262de9e2a.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions static/js/typst-auto-render.js

Large diffs are not rendered by default.

18 changes: 2 additions & 16 deletions templates/home.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import 'macros/math.html' as macro_math -%}
{% extends "_base.html" %}

{% block page %}homepage{% endblock page%}
Expand All @@ -12,22 +13,7 @@
<link id="hl" rel="stylesheet" type="text/css" href="/hl-light.css" />
{% endif %}
{% if section.extra.math %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/katex.min.css" integrity="sha384-3UiQGuEI4TTMaFmGIZumfRPtfKQ3trwQE2JgosJxCnGmQpL/lJdjpcHkaaFwHlcI" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/katex.min.js" integrity="sha384-G0zcxDFp5LWZtDuRMnBkk3EphCK1lhEf4UEyEM693ka574TZGwo4IWwS6QLzM/2t" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
throwOnError: false
});
});
</script>
{{ macro_math::math_render(style = section.extra.math) }}
{% endif %}
{% endblock head %}

Expand Down
44 changes: 44 additions & 0 deletions templates/macros/math.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% macro math_render(style) %}

{% if style is string %}
{% set format = style | lower %}
{% elif style == true%}
{% set format = "katex" %}
{% else %}
{{ throw(message = "Undefined math style") }}
{% endif %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css"
integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous">
{% if format == "typst" %}
<script defer src="/js/typst-auto-render.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
throwOnError: false
});
});
</script>
{% elif format == "katex" %}
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"
integrity="sha384-XjKyOOlGwcjNTAIQHIpgOno0Hl1YQqzUOEleOLALmuqehneUG+vnGctmUb0ZY0l8"
crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"
integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05"
crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
throwOnError: false
});
});
</script>
{% else %}
{{ throw(message = "Unsupported math render engine " ~ style) }}
{% endif %}
{% endmacro %}
18 changes: 2 additions & 16 deletions templates/post.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import 'macros/math.html' as macro_math -%}
{% extends "_base.html" %}

{% block page %}post{% endblock page %}
Expand All @@ -24,22 +25,7 @@
<link id="hl" rel="stylesheet" type="text/css" href="/hl-light.css" />
{% endif %}
{% if page.extra.math %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js" integrity="sha384-XjKyOOlGwcjNTAIQHIpgOno0Hl1YQqzUOEleOLALmuqehneUG+vnGctmUb0ZY0l8" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
throwOnError: false
});
});
</script>
{{ macro_math::math_render(style = page.extra.math) }}
{% endif %}
{% endblock head %}

Expand Down
2 changes: 1 addition & 1 deletion templates/projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% include "_header.html" %}
<div id="wrapper">
<main>
{% set data = load_data(path="content/projects/data.toml", format="toml") %}
{% set data = load_data(path="content" ~ section.path ~ "data.toml", format="toml") %}
{% for proj in data.project %}
<div class="proj">
<div class="content">
Expand Down
18 changes: 2 additions & 16 deletions templates/prose.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% import 'macros/math.html' as macro_math -%}
{% extends "_base.html" %}

{% block page %}prose-page{% endblock page %}
Expand All @@ -14,22 +15,7 @@
<link id="hl" rel="stylesheet" type="text/css" href="/hl-light.css" />
{% endif %}
{% if section.extra.math %}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js" integrity="sha384-XjKyOOlGwcjNTAIQHIpgOno0Hl1YQqzUOEleOLALmuqehneUG+vnGctmUb0ZY0l8" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(document.body, {
delimiters: [
{ left: '$$', right: '$$', display: true },
{ left: '$', right: '$', display: false },
{ left: '\\(', right: '\\)', display: false },
{ left: '\\[', right: '\\]', display: true }
],
throwOnError: false
});
});
</script>
{{ macro_math::math_render(style = section.extra.math) }}
{% endif %}
{% endblock head %}

Expand Down