Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
goessner committed May 28, 2022
1 parent cdfe8e7 commit 1513a20
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 59 deletions.
81 changes: 49 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdown-it-texmath",
"version": "0.9.7",
"version": "1.0.0",
"description": "markdown-it extension for rendering TeX Math",
"keywords": [
"TeX",
Expand All @@ -20,7 +20,7 @@
},
"license": "MIT",
"devDependencies": {
"katex": "^0.15.1",
"markdown-it": "^12.2.0"
"katex": "^0.15.6",
"markdown-it": "^13.0.1"
}
}
33 changes: 33 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@

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.

## What's New?
* `markdown-it-texmath` reached quite a stable state with version 1.0 .
* 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).

```
\begin{equation}
a^2+b^2=c^2
\end{equation}
```
They can even be nested.
```
\begin{equation}
\begin{pmatrix}
A & B \\ B & C
\end{pmatrix}
\end{equation}
```

* 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).


## Features
Simplify the process of authoring markdown documents containing math formulas.
This extension is a comfortable tool for scientists, engineers and students with markdown as their first choice document format.
Expand Down Expand Up @@ -39,6 +60,10 @@ This extension is a comfortable tool for scientists, engineers and students with
* inline: ``$$...$$``
* display: `$$...$$`
* display + equation number: `$$...$$ (1)`
* `'beg_end'` (display math only)
* inline: N/A
* display: `begin{...}...end{...}`
* display + equation number: N/A

## Show me

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

## CHANGELOG
### [1.0.0] on May 28, 2022
* Update to KaTeX version `0.15.6`.
* Update to `markdown-it` `13.0.1`.
* [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/42) in level handling with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein).
* [Bug fixed](https://github.com/goessner/markdown-it-texmath/pull/43) in mapping with `markdown-it`. Thanks to [williamstein](https://github.com/williamstein).
* 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).
* 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).

### [0.9.7] on December 07, 2021
* 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.
### [0.9.6] on November 16, 2021
Expand Down
31 changes: 21 additions & 10 deletions test/bug-brackets.html → test/beg_end-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,36 @@
<script src="https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js"></script>
<script src="../texmath.js"></script>
<script src="../texmath.js"></script>
<title>beg_end test</title>
</head>
<body>
<div id="out"></div>
<script>
let md;
str = `
# Simple Dollar tests
const str =
`"Euler\'s identity
\\begin{equation}
e^{i\\pi}+1=0
\\end{equation}
Inline $a+\$ = b$ and $$a+\$ = b$$ or ...
$$
e^{i\\pi}+1=0
$$
$$a+\$ = b$$
\\begin{equation}
\\begin{pmatrix}
A & B \\\\ B & C
\\end{pmatrix}
\\end{equation}
But this very special case does not work
$$a+ = \$$$
`
is a beautiful $a^2+b^2=c^2$ formula."`
document.addEventListener("DOMContentLoaded", () => {
md = markdownit().use(texmath.use(katex), {delimiters:'dollars'});
const md = markdownit({html:true}).use(texmath, { engine: katex,
delimiters: ["dollars","beg_end"]
});
out.innerHTML = md.render(str);
// console.log(md.render(str))
})
</script>
</body>
Expand Down
53 changes: 39 additions & 14 deletions texmath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Stefan Goessner - 2017-21. All rights reserved.
* Copyright (c) Stefan Goessner - 2017-22. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
Expand All @@ -14,7 +14,7 @@ function escapeHTML(text) {
}

function texmath(md, options) {
const delimiters = options && options.delimiters || 'dollars';
const delimiters = texmath.mergeDelimiters(options && options.delimiters);
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).
const katexOptions = options && options.katexOptions || {};
katexOptions.throwOnError = katexOptions.throwOnError || false;
Expand All @@ -30,19 +30,33 @@ function texmath(md, options) {
texmath.katex = { renderToString() { return 'No math renderer found.' } };
}

if (delimiters in texmath.rules) {
for (const rule of texmath.rules[delimiters].inline) {
if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true;
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
}
// inject inline rules to markdown-it
for (const rule of delimiters.inline) {
if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true;
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
}
// inject block rules to markdown-it
for (const rule of delimiters.block) {
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
}
}

texmath.mergeDelimiters = function(delims) {
const delimsArr = Array.isArray(delims) ? delims
: typeof delims === "string" ? [delims]
: ['dollars'];
const delimiters = { inline:[], block:[]}; // target of merge process ...

for (const rule of texmath.rules[delimiters].block) {
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
for (const delim of delimsArr) // merge them into delimiters ...
if (delim in texmath.rules) {
delimiters.inline.push(...texmath.rules[delim].inline);
delimiters.block.push(...texmath.rules[delim].block);
}
}

return delimiters;
}

// texmath.inline = (rule) => dollar; // just for debugging/testing ..
Expand Down Expand Up @@ -100,7 +114,7 @@ texmath.block = (rule) =>
token.markup = '';
token.content = match[1];
token.info = match[match.length-1]; // eq.no
token.map = [ begLine, curline ];
token.map = [ begLine, curline+1 ];
// token.hidden = true;
// end token ... superfluous ...

Expand Down Expand Up @@ -305,6 +319,17 @@ texmath.rules = {
}
]
},
beg_end: {
inline: [],
block: [
{
name: "math_block",
rex: /(\\(?:begin)\{([a-z]+)\}[\s\S]+?\\(?:end)\{\2\})/gmy, // regexp to match \begin{...}...\end{...} environment.
tmpl: "<section><eqn>$1</eqn></section>",
tag: "\\"
}
]
},
dollars: {
inline: [
{ name: 'math_inline_double',
Expand Down

0 comments on commit 1513a20

Please sign in to comment.