-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from devlive-community/dev-archive
支持 KaTeX 渲染 (close #10)
- Loading branch information
Showing
6 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
title: KaTeX | ||
icon: square-function | ||
--- | ||
|
||
PageForge 支持使用 [KaTeX](https://katex.org "KaTeX" "_blank") 数学公式渲染引擎。 | ||
|
||
!!! danger "注意" | ||
该功能需要在配置文件中启用,可以在 `pageforge.yaml` 中配置 | ||
|
||
```yaml | ||
feature: | ||
katex: | ||
enable: true | ||
``` | ||
|
||
如果要传递自定义配置可以在 `pageforge.yaml` 中添加自定义配置 | ||
|
||
```yaml | ||
feature: | ||
katex: | ||
enable: true | ||
options: | ||
throwOnError: true | ||
``` | ||
!!! | ||
|
||
## CDN 配置 | ||
|
||
--- | ||
|
||
PageForge 默认使用的是以下 CDN | ||
|
||
```js | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css"> | ||
<script src="https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js"></script> | ||
``` | ||
|
||
可以修改 `pageforge.yaml` 中的 `cdn` 配置来配置自定义 CDN | ||
|
||
```yaml | ||
cdn: | ||
katexJs: "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.js" | ||
katexCss: "https://cdn.jsdelivr.net/npm/katex@latest/dist/katex.min.css" | ||
``` | ||
## 基本语法 | ||
--- | ||
```markdown | ||
:::katex | ||
\sum_{i=1}^n i = \frac{n(n+1)}{2} | ||
::: | ||
``` | ||
## 化学方程式 | ||
--- | ||
:::katex | ||
\ce{CO2 + C -> 2 CO} | ||
::: | ||
## 带编号的公式 | ||
--- | ||
:::katex | ||
\tag{1} E = mc^2 | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const {loadComponent} = require("../../component-loader"); | ||
const ConfigManager = require("../../config-manager"); | ||
const config = new ConfigManager(process.cwd()).getConfig(); | ||
|
||
const PageForgeKaTeXExtension = { | ||
name: 'pageforgeKatex', | ||
level: 'block', | ||
|
||
start(src) { | ||
if (!config.feature?.katex?.enable) { | ||
return -1; | ||
} | ||
|
||
// 匹配 :::katex 或 ::: katex 开头 | ||
const match = src.match(/^:::\s*katex\n/m); | ||
return match ? match.index : -1; | ||
}, | ||
|
||
tokenizer(src, tokens) { | ||
if (!config.feature?.katex?.enable) { | ||
return false; | ||
} | ||
|
||
// 匹配开头 | ||
const match = src.match(/^:::\s*katex\n/); | ||
if (!match) { | ||
return false; | ||
} | ||
|
||
// 查找结束标记 ::: | ||
const endIndex = src.indexOf('\n:::'); | ||
if (endIndex === -1) { | ||
return false; | ||
} | ||
|
||
// 提取内容 | ||
const content = src.slice(match[0].length, endIndex).trim(); | ||
const raw = src.slice(0, endIndex + 4); | ||
|
||
return { | ||
type: 'pageforgeKatex', | ||
raw, | ||
content, | ||
tokens: [] | ||
}; | ||
}, | ||
|
||
renderer(token) { | ||
return loadComponent('katex', { | ||
content: token.content, | ||
config: config.feature?.katex?.options || {} | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = PageForgeKaTeXExtension; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = function template(item) { | ||
return ` | ||
<div class="katex-wrapper my-4"> | ||
<div class="katex-formula overflow-x-auto"> | ||
<script> | ||
document.currentScript.parentElement.innerHTML = katex.renderToString( | ||
\`${item.content}\`, | ||
{ | ||
displayMode: true, | ||
throwOnError: false, | ||
...${JSON.stringify(item.config || {})} | ||
} | ||
); | ||
</script> | ||
</div> | ||
</div> | ||
`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters