mdast-util-math-inline 1.0.0
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/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/from-markdown.d.ts +22 -0
- package/dist/from-markdown.d.ts.map +1 -0
- package/dist/from-markdown.js +38 -0
- package/dist/from-markdown.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/to-markdown.d.ts +3 -0
- package/dist/to-markdown.d.ts.map +1 -0
- package/dist/to-markdown.js +68 -0
- package/dist/to-markdown.js.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 weavepage
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# mdast-util-math-inline
|
|
2
|
+
|
|
3
|
+
[mdast](https://github.com/syntax-tree/mdast) utilities for inline math (`:math[...]`).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mdast-util-math-inline
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
import { fromMarkdown } from 'mdast-util-from-markdown'
|
|
15
|
+
import { toMarkdown } from 'mdast-util-to-markdown'
|
|
16
|
+
import { mathInline } from 'micromark-extension-math-inline'
|
|
17
|
+
import { mathInlineFromMarkdown, mathInlineToMarkdown } from 'mdast-util-math-inline'
|
|
18
|
+
|
|
19
|
+
// Parse
|
|
20
|
+
const tree = fromMarkdown(':math[x^2]', {
|
|
21
|
+
extensions: [mathInline()],
|
|
22
|
+
mdastExtensions: [mathInlineFromMarkdown()]
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// Stringify
|
|
26
|
+
const md = toMarkdown(tree, {
|
|
27
|
+
extensions: [mathInlineToMarkdown()]
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## AST
|
|
32
|
+
|
|
33
|
+
### `InlineMath`
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
interface InlineMath extends Literal {
|
|
37
|
+
type: 'inlineMath'
|
|
38
|
+
value: string
|
|
39
|
+
data?: {
|
|
40
|
+
hName: 'code'
|
|
41
|
+
hProperties: { className: ['language-math', 'math-inline'] }
|
|
42
|
+
hChildren: [{ type: 'text', value: string }]
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
The `data` fields enable compatibility with `remark-rehype` and `rehype-katex`.
|
|
48
|
+
|
|
49
|
+
## Escaping
|
|
50
|
+
|
|
51
|
+
When serializing to markdown:
|
|
52
|
+
- `]` is escaped as `\]`
|
|
53
|
+
- `\` before `]` or `\` is escaped as `\\`
|
|
54
|
+
- Balanced brackets don't need escaping
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
MIT
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Extension } from 'mdast-util-from-markdown';
|
|
2
|
+
import type { Literal } from 'mdast';
|
|
3
|
+
export interface InlineMath extends Literal {
|
|
4
|
+
type: 'inlineMath';
|
|
5
|
+
data?: {
|
|
6
|
+
hName: string;
|
|
7
|
+
hProperties: {
|
|
8
|
+
className: string[];
|
|
9
|
+
};
|
|
10
|
+
hChildren: Array<{
|
|
11
|
+
type: 'text';
|
|
12
|
+
value: string;
|
|
13
|
+
}>;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
declare module 'mdast' {
|
|
17
|
+
interface PhrasingContentMap {
|
|
18
|
+
inlineMath: InlineMath;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export declare function mathInlineFromMarkdown(): Extension;
|
|
22
|
+
//# sourceMappingURL=from-markdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"from-markdown.d.ts","sourceRoot":"","sources":["../src/from-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,SAAS,EAAS,MAAM,0BAA0B,CAAA;AAChF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAEpC,MAAM,WAAW,UAAW,SAAQ,OAAO;IACzC,IAAI,EAAE,YAAY,CAAA;IAClB,IAAI,CAAC,EAAE;QACL,KAAK,EAAE,MAAM,CAAA;QACb,WAAW,EAAE;YAAE,SAAS,EAAE,MAAM,EAAE,CAAA;SAAE,CAAA;QACpC,SAAS,EAAE,KAAK,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,CAAC,CAAA;KAClD,CAAA;CACF;AAED,OAAO,QAAQ,OAAO,CAAC;IACrB,UAAU,kBAAkB;QAC1B,UAAU,EAAE,UAAU,CAAA;KACvB;CACF;AAED,wBAAgB,sBAAsB,IAAI,SAAS,CAsClD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export function mathInlineFromMarkdown() {
|
|
2
|
+
let mathValue = '';
|
|
3
|
+
return {
|
|
4
|
+
enter: {
|
|
5
|
+
mathInline: enterMathInline,
|
|
6
|
+
mathInlineData: enterMathInlineData
|
|
7
|
+
},
|
|
8
|
+
exit: {
|
|
9
|
+
mathInlineData: exitMathInlineData,
|
|
10
|
+
mathInline: exitMathInline
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
function enterMathInline(token) {
|
|
14
|
+
// @ts-expect-error: inlineMath is a custom node type
|
|
15
|
+
this.enter({ type: 'inlineMath', value: '' }, token);
|
|
16
|
+
}
|
|
17
|
+
function enterMathInlineData() {
|
|
18
|
+
mathValue = '';
|
|
19
|
+
}
|
|
20
|
+
function exitMathInlineData(token) {
|
|
21
|
+
mathValue = processEscapes(this.sliceSerialize(token));
|
|
22
|
+
}
|
|
23
|
+
function exitMathInline(token) {
|
|
24
|
+
// @ts-expect-error: accessing custom node
|
|
25
|
+
const node = this.stack[this.stack.length - 1];
|
|
26
|
+
node.value = mathValue;
|
|
27
|
+
node.data = {
|
|
28
|
+
hName: 'code',
|
|
29
|
+
hProperties: { className: ['language-math', 'math-inline'] },
|
|
30
|
+
hChildren: [{ type: 'text', value: mathValue }]
|
|
31
|
+
};
|
|
32
|
+
this.exit(token);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function processEscapes(input) {
|
|
36
|
+
return input.replace(/\\([\]\\])/g, '$1');
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=from-markdown.js.map
|
|
@@ -0,0 +1 @@
|
|
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,CAAA;AAC3C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAmB,MAAM,oBAAoB,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-markdown.d.ts","sourceRoot":"","sources":["../src/to-markdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAGrD,wBAAgB,oBAAoB,IAAI,OAAO,CAc9C"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export function mathInlineToMarkdown() {
|
|
2
|
+
return {
|
|
3
|
+
handlers: {
|
|
4
|
+
// @ts-expect-error: inlineMath is a custom node type
|
|
5
|
+
inlineMath: handleInlineMath
|
|
6
|
+
},
|
|
7
|
+
unsafe: [
|
|
8
|
+
{
|
|
9
|
+
character: ':',
|
|
10
|
+
after: 'math\\[',
|
|
11
|
+
inConstruct: ['phrasing']
|
|
12
|
+
}
|
|
13
|
+
]
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function handleInlineMath(node) {
|
|
17
|
+
return ':math[' + escapeValue(node.value) + ']';
|
|
18
|
+
}
|
|
19
|
+
function escapeValue(value) {
|
|
20
|
+
// Pre-count ] to know how many we have for balancing
|
|
21
|
+
let remainingClose = 0;
|
|
22
|
+
for (const char of value) {
|
|
23
|
+
if (char === ']')
|
|
24
|
+
remainingClose++;
|
|
25
|
+
}
|
|
26
|
+
let result = '';
|
|
27
|
+
let bracketDepth = 0;
|
|
28
|
+
for (let i = 0; i < value.length; i++) {
|
|
29
|
+
const char = value[i];
|
|
30
|
+
if (char === '[') {
|
|
31
|
+
// Escape if there aren't enough remaining ] to balance
|
|
32
|
+
if (bracketDepth >= remainingClose) {
|
|
33
|
+
result += '\\[';
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
bracketDepth++;
|
|
37
|
+
result += char;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (char === ']') {
|
|
41
|
+
remainingClose--;
|
|
42
|
+
if (bracketDepth > 0) {
|
|
43
|
+
// Balanced bracket, no escape needed
|
|
44
|
+
bracketDepth--;
|
|
45
|
+
result += char;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// Unbalanced, must escape
|
|
49
|
+
result += '\\]';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (char === '\\') {
|
|
53
|
+
// Escape backslash if followed by ] or \ or [ (to prevent creating escape sequences)
|
|
54
|
+
const next = value[i + 1];
|
|
55
|
+
if (next === ']' || next === '\\' || next === '[') {
|
|
56
|
+
result += '\\\\';
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
result += '\\';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
result += char;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=to-markdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"to-markdown.js","sourceRoot":"","sources":["../src/to-markdown.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,QAAQ,EAAE;YACR,qDAAqD;YACrD,UAAU,EAAE,gBAAgB;SAC7B;QACD,MAAM,EAAE;YACN;gBACE,SAAS,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,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;YACzB,IAAI,IAAI,KAAK,GAAG,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;gBAClD,MAAM,IAAI,MAAM,CAAA;YAClB,CAAC;iBAAM,CAAC;gBACN,MAAM,IAAI,IAAI,CAAA;YAChB,CAAC;QACH,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,IAAI,CAAA;QAChB,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mdast-util-math-inline",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "mdast utility to parse and serialize inline math (:math[...])",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "echo 'Tests run via remark-math-inline package'",
|
|
18
|
+
"clean": "rm -rf dist"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"mdast-util-from-markdown": "^2.0.0",
|
|
22
|
+
"mdast-util-to-markdown": "^2.0.0",
|
|
23
|
+
"micromark-extension-math-inline": "^1.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/mdast": "^4.0.0",
|
|
27
|
+
"typescript": "^5.7.0",
|
|
28
|
+
"vitest": "^2.0.0"
|
|
29
|
+
},
|
|
30
|
+
"keywords": ["mdast", "math", "latex", "katex", "inline"],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/weavepage/remark-math-inline.git",
|
|
35
|
+
"directory": "packages/mdast-util-math-inline"
|
|
36
|
+
}
|
|
37
|
+
}
|