@webc.site/math 0.1.18 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -136
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,8 +24,8 @@ No need to load hundreds of KB of KaTeX/MathJax and large font packages. At just
|
|
|
24
24
|
- [2. Generation Speed (Ops/sec)](#2-generation-speed-opssec)
|
|
25
25
|
- [Usage](#usage)
|
|
26
26
|
- [JavaScript Examples](#javascript-examples)
|
|
27
|
-
- [Markdown Parser Plugins](#markdown-parser-plugins)
|
|
28
27
|
- [CSS and Math Font Configuration](#css-and-math-font-configuration)
|
|
28
|
+
- [Markdown Parser Plugins](#markdown-parser-plugins)
|
|
29
29
|
- [Features](#features)
|
|
30
30
|
- [Supported Syntax List](#supported-syntax-list)
|
|
31
31
|
- [Unsupported Syntax](#unsupported-syntax)
|
|
@@ -112,83 +112,62 @@ console.log(html);
|
|
|
112
112
|
// Output: Euler's identity: <math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msup><mi>e</mi><mrow><mi>i</mi><mi>π</mi></mrow></msup><mo>+</mo><mn>1</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">e^{i\pi} + 1 = 0</annotation></semantics></math>
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
###
|
|
116
|
-
|
|
117
|
-
#### 1. Marked Extension
|
|
115
|
+
### CSS and Math Font Configuration
|
|
118
116
|
|
|
119
|
-
|
|
117
|
+
To ensure beautifully typeset browser-native math equations, using a math font is recommended. We recommend the **Latin Modern Math** font from the `18s` package (derived from Donald Knuth's classical Computer Modern, supporting OpenType MATH table features).
|
|
120
118
|
|
|
121
|
-
|
|
122
|
-
import { marked } from "marked";
|
|
123
|
-
import mathMarked from "@webc.site/math-marked";
|
|
119
|
+
#### 1. Online Reference (Recommended)
|
|
124
120
|
|
|
125
|
-
|
|
121
|
+
Import the online font in CSS:
|
|
126
122
|
|
|
127
|
-
|
|
123
|
+
```css
|
|
124
|
+
/* Import the bundle (includes Source Han Sans t, monospace c, and math font m) */
|
|
125
|
+
@import url("//registry.npmmirror.com/18s/0.2.24/files/_.css");
|
|
128
126
|
```
|
|
129
127
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
Use [`@webc.site/math-remark`](https://www.npmjs.com/package/@webc.site/math-remark) to replace `inlineMath` and `math` nodes in Unified/Remark AST with native MathML HTML:
|
|
133
|
-
|
|
134
|
-
```javascript
|
|
135
|
-
import { unified } from "unified";
|
|
136
|
-
import remarkParse from "remark-parse";
|
|
137
|
-
import remarkMath from "remark-math";
|
|
138
|
-
import mathRemark from "@webc.site/math-remark";
|
|
139
|
-
import remarkHtml from "remark-html";
|
|
128
|
+
Or import the math font `m` only:
|
|
140
129
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
.use(remarkMath)
|
|
144
|
-
.use(mathRemark)
|
|
145
|
-
.use(remarkHtml, { sanitize: false })
|
|
146
|
-
.process("Euler's identity: $$e^{i\\pi} + 1 = 0$$");
|
|
130
|
+
```css
|
|
131
|
+
@import url("//registry.npmmirror.com/18s/0.2.24/files/m.css");
|
|
147
132
|
```
|
|
148
133
|
|
|
149
|
-
####
|
|
134
|
+
#### 2. Configure CSS Style
|
|
150
135
|
|
|
151
|
-
|
|
136
|
+
Set the font family for the `math` tag in your global CSS stylesheet.
|
|
152
137
|
|
|
153
|
-
|
|
154
|
-
import markdownit from "markdown-it";
|
|
155
|
-
import mathMarkdownIt from "@webc.site/math-markdown-it";
|
|
138
|
+
##### Option A: Using Online Fonts (Recommended for best visual quality)
|
|
156
139
|
|
|
157
|
-
|
|
140
|
+
For projects importing the `18s` font assets (which contains the optimized math font `m` and Source Han Sans `t`):
|
|
158
141
|
|
|
159
|
-
|
|
142
|
+
```css
|
|
143
|
+
math {
|
|
144
|
+
/* m is the math font, t is Source Han Sans (optimized with font slicing for Chinese characters to boost loading performance), math is system math font, sans-serif is default fallback */
|
|
145
|
+
font-family: m, t, math, sans-serif;
|
|
146
|
+
}
|
|
160
147
|
```
|
|
161
148
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
To ensure beautifully typeset browser-native math equations, using a math font is recommended. We recommend the **Latin Modern Math** font from the `18s` package (derived from Donald Knuth's classical Computer Modern, supporting OpenType MATH table features).
|
|
165
|
-
|
|
166
|
-
#### 1. Online Reference (Recommended)
|
|
149
|
+
##### Option B: Using System Math Fonts (Zero external font assets)
|
|
167
150
|
|
|
168
|
-
|
|
151
|
+
Leverage system-default math fonts to minimize loading size:
|
|
169
152
|
|
|
170
153
|
```css
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
math {
|
|
155
|
+
/* Prioritize system-built-in math fonts, fallback to standard CSS 'math' generic font family, and use sans-serif as a final fallback */
|
|
156
|
+
font-family: "STIX Two Math", "Latin Modern Math", "Cambria Math", math, sans-serif;
|
|
157
|
+
}
|
|
173
158
|
```
|
|
174
159
|
|
|
175
|
-
|
|
160
|
+
#### 3. Integration with Build Tools
|
|
176
161
|
|
|
177
|
-
|
|
178
|
-
@import url("//registry.npmmirror.com/18s/0.2.24/files/m.css");
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
#### 2. Local Installation
|
|
162
|
+
Install the `18s` font package via npm, and import it at your project entry using build tools like Vite or Webpack:
|
|
182
163
|
|
|
183
164
|
```bash
|
|
184
165
|
npm install 18s
|
|
185
166
|
```
|
|
186
167
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
Choose one of the following methods (Note: do not mix JS and CSS imports):
|
|
168
|
+
Import font styles in your project entry file:
|
|
190
169
|
|
|
191
|
-
|
|
170
|
+
##### Method A: Import in JS/TS Entry File
|
|
192
171
|
|
|
193
172
|
```javascript
|
|
194
173
|
// Import the bundle (includes Source Han Sans t, monospace c, and math font m)
|
|
@@ -201,7 +180,7 @@ Or import math font `m` only:
|
|
|
201
180
|
import "18s/m.css";
|
|
202
181
|
```
|
|
203
182
|
|
|
204
|
-
|
|
183
|
+
##### Method B: Import in CSS Stylesheet
|
|
205
184
|
|
|
206
185
|
```css
|
|
207
186
|
/* Import the bundle (includes Source Han Sans t, monospace c, and math font m) */
|
|
@@ -214,30 +193,51 @@ Or import math font `m` only:
|
|
|
214
193
|
@import "18s/m.css";
|
|
215
194
|
```
|
|
216
195
|
|
|
217
|
-
|
|
196
|
+
### Markdown Parser Plugins
|
|
218
197
|
|
|
219
|
-
|
|
198
|
+
#### 1. Marked Extension
|
|
220
199
|
|
|
221
|
-
|
|
200
|
+
Use [`@webc.site/math-marked`](https://www.npmjs.com/package/@webc.site/math-marked) to automatically parse and compile inline (`$...$`) and block (`$$...$$`) formulas:
|
|
222
201
|
|
|
223
|
-
|
|
202
|
+
```javascript
|
|
203
|
+
import { marked } from "marked";
|
|
204
|
+
import mathMarked from "@webc.site/math-marked";
|
|
224
205
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
font-family: m, t, math, sans-serif;
|
|
229
|
-
}
|
|
206
|
+
marked.use(mathMarked());
|
|
207
|
+
|
|
208
|
+
const html = marked.parse("Euler's identity: $$e^{i\\pi} + 1 = 0$$");
|
|
230
209
|
```
|
|
231
210
|
|
|
232
|
-
|
|
211
|
+
#### 2. Remark Plugin
|
|
233
212
|
|
|
234
|
-
|
|
213
|
+
Use [`@webc.site/math-remark`](https://www.npmjs.com/package/@webc.site/math-remark) to replace `inlineMath` and `math` nodes in Unified/Remark AST with native MathML HTML:
|
|
235
214
|
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
215
|
+
```javascript
|
|
216
|
+
import { unified } from "unified";
|
|
217
|
+
import remarkParse from "remark-parse";
|
|
218
|
+
import remarkMath from "remark-math";
|
|
219
|
+
import mathRemark from "@webc.site/math-remark";
|
|
220
|
+
import remarkHtml from "remark-html";
|
|
221
|
+
|
|
222
|
+
const html = await unified()
|
|
223
|
+
.use(remarkParse)
|
|
224
|
+
.use(remarkMath)
|
|
225
|
+
.use(mathRemark)
|
|
226
|
+
.use(remarkHtml, { sanitize: false })
|
|
227
|
+
.process("Euler's identity: $$e^{i\\pi} + 1 = 0$$");
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
#### 3. Markdown-it Plugin
|
|
231
|
+
|
|
232
|
+
Use [`@webc.site/math-markdown-it`](https://www.npmjs.com/package/@webc.site/math-markdown-it) to automatically parse and render inline/block formulas in `markdown-it`:
|
|
233
|
+
|
|
234
|
+
```javascript
|
|
235
|
+
import markdownit from "markdown-it";
|
|
236
|
+
import mathMarkdownIt from "@webc.site/math-markdown-it";
|
|
237
|
+
|
|
238
|
+
const md = markdownit().use(mathMarkdownIt);
|
|
239
|
+
|
|
240
|
+
const html = md.render("Euler's identity: $$e^{i\\pi} + 1 = 0$$");
|
|
241
241
|
```
|
|
242
242
|
|
|
243
243
|
## Features
|
|
@@ -466,8 +466,8 @@ In combination with the **Latin Modern Math** font (`m` from `18s` package), `@w
|
|
|
466
466
|
- [2. 生成速度对比](#2-生成速度对比)
|
|
467
467
|
- [使用方法](#使用方法)
|
|
468
468
|
- [JavaScript 示例](#javascript-示例)
|
|
469
|
-
- [Markdown 解析器插件](#markdown-解析器插件)
|
|
470
469
|
- [CSS 与数学字体配置](#css-与数学字体配置)
|
|
470
|
+
- [Markdown 解析器插件](#markdown-解析器插件)
|
|
471
471
|
- [功能特性](#功能特性)
|
|
472
472
|
- [支持的语法清单](#支持的语法清单)
|
|
473
473
|
- [不支持的语法](#不支持的语法)
|
|
@@ -554,83 +554,62 @@ console.log(html);
|
|
|
554
554
|
// 输出: 欧拉恒等式:<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msup><mi>e</mi><mrow><mi>i</mi><mi>π</mi></mrow></msup><mo>+</mo><mn>1</mn><mo>=</mo><mn>0</mn></mrow><annotation encoding="application/x-tex">e^{i\pi} + 1 = 0</annotation></semantics></math>
|
|
555
555
|
```
|
|
556
556
|
|
|
557
|
-
###
|
|
558
|
-
|
|
559
|
-
#### 1. Marked 插件
|
|
557
|
+
### CSS 与数学字体配置
|
|
560
558
|
|
|
561
|
-
|
|
559
|
+
为保证浏览器原生数学公式的排版,建议配置数学字体。推荐使用 `18s` 字体包的 **Latin Modern Math**(源自高德纳的 Computer Modern 字体,支持 OpenType 数学排版特性)。
|
|
562
560
|
|
|
563
|
-
|
|
564
|
-
import { marked } from "marked";
|
|
565
|
-
import mathMarked from "@webc.site/math-marked";
|
|
561
|
+
#### 1. 在线引用
|
|
566
562
|
|
|
567
|
-
|
|
563
|
+
在 CSS 中通过 `@import` 引入在线字体:
|
|
568
564
|
|
|
569
|
-
|
|
565
|
+
```css
|
|
566
|
+
/* 引入合并后的字体 CSS(包含思源黑体 t、代码字体 c 及数学字体 m) */
|
|
567
|
+
@import url("//registry.npmmirror.com/18s/0.2.24/files/_.css");
|
|
570
568
|
```
|
|
571
569
|
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
使用 [`@webc.site/math-remark`](https://www.npmjs.com/package/@webc.site/math-remark) 将 Unified/Remark AST 中的 `inlineMath` 与 `math` 节点替换为原生 MathML HTML:
|
|
575
|
-
|
|
576
|
-
```javascript
|
|
577
|
-
import { unified } from "unified";
|
|
578
|
-
import remarkParse from "remark-parse";
|
|
579
|
-
import remarkMath from "remark-math";
|
|
580
|
-
import mathRemark from "@webc.site/math-remark";
|
|
581
|
-
import remarkHtml from "remark-html";
|
|
570
|
+
或者仅按需引入数学字体 `m`:
|
|
582
571
|
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
.use(remarkMath)
|
|
586
|
-
.use(mathRemark)
|
|
587
|
-
.use(remarkHtml, { sanitize: false })
|
|
588
|
-
.process("欧拉恒等式:$$e^{i\\pi} + 1 = 0$$");
|
|
572
|
+
```css
|
|
573
|
+
@import url("//registry.npmmirror.com/18s/0.2.24/files/m.css");
|
|
589
574
|
```
|
|
590
575
|
|
|
591
|
-
####
|
|
576
|
+
#### 2. 配置 CSS 样式
|
|
592
577
|
|
|
593
|
-
|
|
578
|
+
在全局 CSS 样式表中,为 `math` 标签指定数学字体。
|
|
594
579
|
|
|
595
|
-
|
|
596
|
-
import markdownit from "markdown-it";
|
|
597
|
-
import mathMarkdownIt from "@webc.site/math-markdown-it";
|
|
580
|
+
##### 方案 A:使用在线字体(推荐,效果最完美)
|
|
598
581
|
|
|
599
|
-
|
|
582
|
+
引入 `18s` 提供的字体包(含切片优化思源黑体 `t` 与数学字体 `m`):
|
|
600
583
|
|
|
601
|
-
|
|
584
|
+
```css
|
|
585
|
+
math {
|
|
586
|
+
/* m 为数学字体,t 为思源黑体(对中文字符进行了切片优化以提升加载性能),math 为系统数学字体,sans-serif 为系统默认无衬线字体 */
|
|
587
|
+
font-family: m, t, math, sans-serif;
|
|
588
|
+
}
|
|
602
589
|
```
|
|
603
590
|
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
为保证浏览器原生数学公式的排版,建议配置数学字体。推荐使用 `18s` 字体包的 **Latin Modern Math**(源自高德纳的 Computer Modern 字体,支持 OpenType 数学排版特性)。
|
|
607
|
-
|
|
608
|
-
#### 1. 在线引用
|
|
591
|
+
##### 方案 B:使用系统数学字体(免引入外部资源)
|
|
609
592
|
|
|
610
|
-
|
|
593
|
+
直接使用系统内置数学字体以最小化体积:
|
|
611
594
|
|
|
612
595
|
```css
|
|
613
|
-
|
|
614
|
-
|
|
596
|
+
math {
|
|
597
|
+
/* 优先使用各平台内置的数学字体,其次降级到 CSS 标准 math 泛型,最后以系统默认无衬线字体作为后备 */
|
|
598
|
+
font-family: "STIX Two Math", "Latin Modern Math", "Cambria Math", math, sans-serif;
|
|
599
|
+
}
|
|
615
600
|
```
|
|
616
601
|
|
|
617
|
-
|
|
602
|
+
#### 3. 配合构建工具引用
|
|
618
603
|
|
|
619
|
-
|
|
620
|
-
@import url("//registry.npmmirror.com/18s/0.2.24/files/m.css");
|
|
621
|
-
```
|
|
622
|
-
|
|
623
|
-
#### 2. 本地安装字体包
|
|
604
|
+
通过 npm 安装 `18s` 字体包,配合 Vite、Webpack 等构建工具在项目入口中引入:
|
|
624
605
|
|
|
625
606
|
```bash
|
|
626
607
|
npm install 18s
|
|
627
608
|
```
|
|
628
609
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
选择以下一种方式引入字体映射(注意:请勿混用 JS 与 CSS 引入):
|
|
610
|
+
在项目入口文件中引入字体样式:
|
|
632
611
|
|
|
633
|
-
|
|
612
|
+
##### 方式 A:在 JS/TS 入口中引入
|
|
634
613
|
|
|
635
614
|
```javascript
|
|
636
615
|
// 引入合并后的字体 CSS(包含思源黑体 t、代码字体 c 及数学字体 m)
|
|
@@ -643,7 +622,7 @@ import "18s/_.css";
|
|
|
643
622
|
import "18s/m.css";
|
|
644
623
|
```
|
|
645
624
|
|
|
646
|
-
|
|
625
|
+
##### 方式 B:在 CSS 入口中引入
|
|
647
626
|
|
|
648
627
|
```css
|
|
649
628
|
/* 引入合并后的字体 CSS(包含思源黑体 t、代码字体 c 及数学字体 m) */
|
|
@@ -656,30 +635,51 @@ import "18s/m.css";
|
|
|
656
635
|
@import "18s/m.css";
|
|
657
636
|
```
|
|
658
637
|
|
|
659
|
-
|
|
638
|
+
### Markdown 解析器插件
|
|
660
639
|
|
|
661
|
-
|
|
640
|
+
#### 1. Marked 插件
|
|
662
641
|
|
|
663
|
-
|
|
642
|
+
使用 [`@webc.site/math-marked`](https://www.npmjs.com/package/@webc.site/math-marked) 自动解析并编译行内(`$...$`)与块级(`$$...$$`)公式:
|
|
664
643
|
|
|
665
|
-
|
|
644
|
+
```javascript
|
|
645
|
+
import { marked } from "marked";
|
|
646
|
+
import mathMarked from "@webc.site/math-marked";
|
|
666
647
|
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
font-family: m, t, math, sans-serif;
|
|
671
|
-
}
|
|
648
|
+
marked.use(mathMarked());
|
|
649
|
+
|
|
650
|
+
const html = marked.parse("欧拉恒等式:$$e^{i\\pi} + 1 = 0$$");
|
|
672
651
|
```
|
|
673
652
|
|
|
674
|
-
|
|
653
|
+
#### 2. Remark 插件
|
|
675
654
|
|
|
676
|
-
|
|
655
|
+
使用 [`@webc.site/math-remark`](https://www.npmjs.com/package/@webc.site/math-remark) 将 Unified/Remark AST 中的 `inlineMath` 与 `math` 节点替换为原生 MathML HTML:
|
|
677
656
|
|
|
678
|
-
```
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
657
|
+
```javascript
|
|
658
|
+
import { unified } from "unified";
|
|
659
|
+
import remarkParse from "remark-parse";
|
|
660
|
+
import remarkMath from "remark-math";
|
|
661
|
+
import mathRemark from "@webc.site/math-remark";
|
|
662
|
+
import remarkHtml from "remark-html";
|
|
663
|
+
|
|
664
|
+
const html = await unified()
|
|
665
|
+
.use(remarkParse)
|
|
666
|
+
.use(remarkMath)
|
|
667
|
+
.use(mathRemark)
|
|
668
|
+
.use(remarkHtml, { sanitize: false })
|
|
669
|
+
.process("欧拉恒等式:$$e^{i\\pi} + 1 = 0$$");
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
#### 3. Markdown-it 插件
|
|
673
|
+
|
|
674
|
+
使用 [`@webc.site/math-markdown-it`](https://www.npmjs.com/package/@webc.site/math-markdown-it) 自动解析并渲染 `markdown-it` 中的行内与块级公式:
|
|
675
|
+
|
|
676
|
+
```javascript
|
|
677
|
+
import markdownit from "markdown-it";
|
|
678
|
+
import mathMarkdownIt from "@webc.site/math-markdown-it";
|
|
679
|
+
|
|
680
|
+
const md = markdownit().use(mathMarkdownIt);
|
|
681
|
+
|
|
682
|
+
const html = md.render("欧拉恒等式:$$e^{i\\pi} + 1 = 0$$");
|
|
683
683
|
```
|
|
684
684
|
|
|
685
685
|
## 功能特性
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@webc.site/math","version":"0.1.
|
|
1
|
+
{"name":"@webc.site/math","version":"0.1.20","description":"The world's smallest and fastest web Markdown formula renderer / 全球最小最快的网页Markdown公式渲染器","keywords":["markdown","math","mathml","render","tex"],"homepage":"https://math.webc.site","license":"MulanPSL-2.0","author":"i18n.site@gmail.com","repository":{"type":"git","url":"git+https://github.com/webc-site/math.git"},"type":"module","exports":{".":{"types":"./mathml.d.ts","default":"./mathml.js"},"./md.js":{"types":"./md.d.ts","default":"./md.js"},"./*":"./*"},"dependencies":{}}
|