Skip to content

Commit 1513a20

Browse files
committed
Version 1.0
1 parent cdfe8e7 commit 1513a20

File tree

5 files changed

+145
-59
lines changed

5 files changed

+145
-59
lines changed

package-lock.json

Lines changed: 49 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "markdown-it-texmath",
3-
"version": "0.9.7",
3+
"version": "1.0.0",
44
"description": "markdown-it extension for rendering TeX Math",
55
"keywords": [
66
"TeX",
@@ -20,7 +20,7 @@
2020
},
2121
"license": "MIT",
2222
"devDependencies": {
23-
"katex": "^0.15.1",
24-
"markdown-it": "^12.2.0"
23+
"katex": "^0.15.6",
24+
"markdown-it": "^13.0.1"
2525
}
2626
}

readme.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77

88
Add TeX math equations to your Markdown documents rendered by [markdown-it](https://github.com/markdown-it/markdown-it) parser. [KaTeX](https://github.com/Khan/KaTeX) is used as a fast math renderer.
99

10+
## What's New?
11+
* `markdown-it-texmath` reached quite a stable state with version 1.0 .
12+
* Native `begin{...}` / `end{...}` environments are supported as delimiters itself ... thanks to [William Stein](https://github.com/williamstein) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/41).
13+
14+
```
15+
\begin{equation}
16+
a^2+b^2=c^2
17+
\end{equation}
18+
```
19+
They can even be nested.
20+
```
21+
\begin{equation}
22+
\begin{pmatrix}
23+
A & B \\ B & C
24+
\end{pmatrix}
25+
\end{equation}
26+
```
27+
28+
* Different delimiters can be merged. Delimiters options property supports array notation for that. Example: `delimiters: ['dollars','beg_end']`. Thanks to [Liu YongLiang](https://github.com/tlylt) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/40).
29+
30+
1031
## Features
1132
Simplify the process of authoring markdown documents containing math formulas.
1233
This extension is a comfortable tool for scientists, engineers and students with markdown as their first choice document format.
@@ -39,6 +60,10 @@ This extension is a comfortable tool for scientists, engineers and students with
3960
* inline: ``$$...$$``
4061
* display: `$$...$$`
4162
* display + equation number: `$$...$$ (1)`
63+
* `'beg_end'` (display math only)
64+
* inline: N/A
65+
* display: `begin{...}...end{...}`
66+
* display + equation number: N/A
4267

4368
## Show me
4469

@@ -123,6 +148,14 @@ Use following links for `texmath.js` and `texmath.css`
123148
But if someone wants to help here out, pull requests are always welcome.
124149

125150
## CHANGELOG
151+
### [1.0.0] on May 28, 2022
152+
* Update to KaTeX version `0.15.6`.
153+
* Update to `markdown-it` `13.0.1`.
154+
* [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/42) in level handling with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein).
155+
* [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/43) in mapping with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein).
156+
* Supporting native `begin{..}` / `end{...}` environments as delimiters itself. Thanks to [williamstein](https://github.com/williamstein) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/41).
157+
* Merging different delimiters for simultaneous use via `delimiters:[<delims1>, <delims2>]`. Thanks to [tlylt](https://github.com/tlylt) for [proposing](https://github.com/goessner/markdown-it-texmath/issues/40).
158+
126159
### [0.9.7] on December 07, 2021
127160
* Redundant `</math>` end-tag with display-mode equations removed. All modes were affected ... invisible effect though. Thanks to [yuanbug](https://github.com/yuanbug) for reporting.
128161
### [0.9.6] on November 16, 2021

test/bug-brackets.html renamed to test/beg_end-test.html

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,36 @@
77
<script src="https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it.min.js"></script>
88
<script src="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js"></script>
99
<script src="../texmath.js"></script>
10-
<script src="../texmath.js"></script>
10+
<title>beg_end test</title>
1111
</head>
1212
<body>
13+
<div id="out"></div>
1314
<script>
14-
let md;
15-
str = `
16-
# Simple Dollar tests
15+
const str =
16+
`"Euler\'s identity
17+
18+
\\begin{equation}
19+
e^{i\\pi}+1=0
20+
\\end{equation}
1721
18-
Inline $a+\$ = b$ and $$a+\$ = b$$ or ...
22+
$$
23+
e^{i\\pi}+1=0
24+
$$
1925
20-
$$a+\$ = b$$
26+
\\begin{equation}
27+
\\begin{pmatrix}
28+
A & B \\\\ B & C
29+
\\end{pmatrix}
30+
\\end{equation}
2131
22-
But this very special case does not work
2332
24-
$$a+ = \$$$
25-
`
33+
is a beautiful $a^2+b^2=c^2$ formula."`
2634
document.addEventListener("DOMContentLoaded", () => {
27-
md = markdownit().use(texmath.use(katex), {delimiters:'dollars'});
35+
const md = markdownit({html:true}).use(texmath, { engine: katex,
36+
delimiters: ["dollars","beg_end"]
37+
});
2838
out.innerHTML = md.render(str);
39+
// console.log(md.render(str))
2940
})
3041
</script>
3142
</body>

texmath.js

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) Stefan Goessner - 2017-21. All rights reserved.
2+
* Copyright (c) Stefan Goessner - 2017-22. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
@@ -14,7 +14,7 @@ function escapeHTML(text) {
1414
}
1515

1616
function texmath(md, options) {
17-
const delimiters = options && options.delimiters || 'dollars';
17+
const delimiters = texmath.mergeDelimiters(options && options.delimiters);
1818
const outerSpace = options && options.outerSpace || false; // inline rules, effectively `dollars` require surrounding spaces, i.e ` $\psi$ `, to be accepted as inline formulas. This is primarily a guard against misinterpreting single `$`'s in normal markdown text (relevant for inline math only. Default: `false`, for backwards compatibility).
1919
const katexOptions = options && options.katexOptions || {};
2020
katexOptions.throwOnError = katexOptions.throwOnError || false;
@@ -30,19 +30,33 @@ function texmath(md, options) {
3030
texmath.katex = { renderToString() { return 'No math renderer found.' } };
3131
}
3232

33-
if (delimiters in texmath.rules) {
34-
for (const rule of texmath.rules[delimiters].inline) {
35-
if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true;
36-
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
37-
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
38-
}
33+
// inject inline rules to markdown-it
34+
for (const rule of delimiters.inline) {
35+
if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true;
36+
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
37+
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
38+
}
39+
// inject block rules to markdown-it
40+
for (const rule of delimiters.block) {
41+
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
42+
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
43+
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
44+
}
45+
}
46+
47+
texmath.mergeDelimiters = function(delims) {
48+
const delimsArr = Array.isArray(delims) ? delims
49+
: typeof delims === "string" ? [delims]
50+
: ['dollars'];
51+
const delimiters = { inline:[], block:[]}; // target of merge process ...
3952

40-
for (const rule of texmath.rules[delimiters].block) {
41-
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
42-
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
43-
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
53+
for (const delim of delimsArr) // merge them into delimiters ...
54+
if (delim in texmath.rules) {
55+
delimiters.inline.push(...texmath.rules[delim].inline);
56+
delimiters.block.push(...texmath.rules[delim].block);
4457
}
45-
}
58+
59+
return delimiters;
4660
}
4761

4862
// texmath.inline = (rule) => dollar; // just for debugging/testing ..
@@ -100,7 +114,7 @@ texmath.block = (rule) =>
100114
token.markup = '';
101115
token.content = match[1];
102116
token.info = match[match.length-1]; // eq.no
103-
token.map = [ begLine, curline ];
117+
token.map = [ begLine, curline+1 ];
104118
// token.hidden = true;
105119
// end token ... superfluous ...
106120

@@ -305,6 +319,17 @@ texmath.rules = {
305319
}
306320
]
307321
},
322+
beg_end: {
323+
inline: [],
324+
block: [
325+
{
326+
name: "math_block",
327+
rex: /(\\(?:begin)\{([a-z]+)\}[\s\S]+?\\(?:end)\{\2\})/gmy, // regexp to match \begin{...}...\end{...} environment.
328+
tmpl: "<section><eqn>$1</eqn></section>",
329+
tag: "\\"
330+
}
331+
]
332+
},
308333
dollars: {
309334
inline: [
310335
{ name: 'math_inline_double',

0 commit comments

Comments
 (0)