@jared-ye/markdown-tex 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 +192 -0
- package/dist/ast/nodes.d.ts +120 -0
- package/dist/ast/nodes.d.ts.map +1 -0
- package/dist/ast/nodes.js +93 -0
- package/dist/ast/nodes.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/latex/latexParser.d.ts +12 -0
- package/dist/latex/latexParser.d.ts.map +1 -0
- package/dist/latex/latexParser.js +1029 -0
- package/dist/latex/latexParser.js.map +1 -0
- package/dist/latex/latexRenderer.d.ts +9 -0
- package/dist/latex/latexRenderer.d.ts.map +1 -0
- package/dist/latex/latexRenderer.js +118 -0
- package/dist/latex/latexRenderer.js.map +1 -0
- package/dist/markdown/markdownParser.d.ts +8 -0
- package/dist/markdown/markdownParser.d.ts.map +1 -0
- package/dist/markdown/markdownParser.js +488 -0
- package/dist/markdown/markdownParser.js.map +1 -0
- package/dist/markdown/markdownRenderer.d.ts +6 -0
- package/dist/markdown/markdownRenderer.d.ts.map +1 -0
- package/dist/markdown/markdownRenderer.js +83 -0
- package/dist/markdown/markdownRenderer.js.map +1 -0
- package/dist/transform/normalize.d.ts +9 -0
- package/dist/transform/normalize.d.ts.map +1 -0
- package/dist/transform/normalize.js +110 -0
- package/dist/transform/normalize.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jared Ye
|
|
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,192 @@
|
|
|
1
|
+
<!-- 语言切换:点击下方链接跳转到对应语言区块 -->
|
|
2
|
+
<p align="center">
|
|
3
|
+
<strong>View in</strong> · <a href="#en">🇺🇸 English</a> · <a href="#zh">🇨🇳 中文</a>
|
|
4
|
+
</p>
|
|
5
|
+
|
|
6
|
+
<details id="en" open>
|
|
7
|
+
<summary><strong>🇺🇸 English</strong></summary>
|
|
8
|
+
|
|
9
|
+
# 📄 markdown-tex
|
|
10
|
+
|
|
11
|
+
> **Markdown ⇄ LaTeX** conversion library with a unified AST. Round-trip safe for headings, paragraphs, lists, code blocks, math, links, images, tables, blockquotes, and inline formatting.
|
|
12
|
+
|
|
13
|
+
[](https://www.npmjs.com/package/markdown-tex)
|
|
14
|
+
[](https://github.com/JaredYe04/markdown-tex)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 Install
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install markdown-tex
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## ✨ Features
|
|
27
|
+
|
|
28
|
+
- **🔄 Bidirectional** — Convert Markdown → LaTeX body and LaTeX body → Markdown
|
|
29
|
+
- **🌳 Unified AST** — Single intermediate representation for both directions
|
|
30
|
+
- **✅ Round-trip safe** — Supported constructs survive MD → LaTeX → MD
|
|
31
|
+
- **📦 Zero config** — Works out of the box, no document preamble (use your own `\documentclass` and packages)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 📖 API
|
|
36
|
+
|
|
37
|
+
| Method | Description |
|
|
38
|
+
|--------|-------------|
|
|
39
|
+
| `markdownToLatex(md)` | Markdown string → LaTeX **body** (no `\documentclass`) |
|
|
40
|
+
| `latexToMarkdown(latex)` | LaTeX body → Markdown string |
|
|
41
|
+
| `markdownToAST(md)` | Markdown → unified AST |
|
|
42
|
+
| `latexToAST(latex)` | LaTeX → unified AST |
|
|
43
|
+
| `normalizeAST(ast)` | Normalize AST for round-trip comparison |
|
|
44
|
+
| `escapeLatex(str)` | Escape `# $ % & _ { }` etc. for preamble/titlepage |
|
|
45
|
+
|
|
46
|
+
**Types:** `AST`, `BlockNode`, `InlineNode` (exported for TypeScript).
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 💡 Examples
|
|
51
|
+
|
|
52
|
+
### Basic conversion
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import { markdownToLatex, latexToMarkdown } from 'markdown-tex'
|
|
56
|
+
|
|
57
|
+
// Markdown → LaTeX body
|
|
58
|
+
const latex = markdownToLatex('# Hello **world**\n\n- item one\n- item two')
|
|
59
|
+
// → \section{Hello \textbf{world}}
|
|
60
|
+
// \begin{itemize}
|
|
61
|
+
// \item item one
|
|
62
|
+
// \item item two
|
|
63
|
+
// \end{itemize}
|
|
64
|
+
|
|
65
|
+
// LaTeX → Markdown
|
|
66
|
+
const md = latexToMarkdown('\\section{Foo}\n\nBar with \\textbf{bold}.')
|
|
67
|
+
// → # Foo
|
|
68
|
+
// Bar with **bold**.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Math & code
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
const latex = markdownToLatex('Inline $E=mc^2$ and block:\n\n$$\n\\int_0^1 x\\,dx\n$$')
|
|
75
|
+
// Inline math → $...$; block math → \[ ... \]
|
|
76
|
+
|
|
77
|
+
const code = markdownToLatex('```js\nconst x = 1\n```')
|
|
78
|
+
// → \begin{verbatim} ... \end{verbatim}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Escape for titlepage / headers
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { escapeLatex } from 'markdown-tex'
|
|
85
|
+
|
|
86
|
+
const title = escapeLatex('Price: 100% & "quoted"')
|
|
87
|
+
// → Price: 100\% \& \"quoted\"
|
|
88
|
+
// Use in \title{}, \lhead{}, etc.
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
---
|
|
92
|
+
|
|
93
|
+
## 🔗 Links
|
|
94
|
+
|
|
95
|
+
- **Repository:** [github.com/JaredYe04/markdown-tex](https://github.com/JaredYe04/markdown-tex)
|
|
96
|
+
- **npm:** [markdown-tex](https://www.npmjs.com/package/markdown-tex)
|
|
97
|
+
|
|
98
|
+
</details>
|
|
99
|
+
|
|
100
|
+
<details id="zh">
|
|
101
|
+
<summary><strong>🇨🇳 中文</strong></summary>
|
|
102
|
+
|
|
103
|
+
# 📄 markdown-tex
|
|
104
|
+
|
|
105
|
+
> 基于**统一 AST** 的 **Markdown ⇄ LaTeX** 转换库。支持标题、段落、列表、代码块、数学、链接、图片、表格、引用及行内格式的往返转换。
|
|
106
|
+
|
|
107
|
+
[](https://www.npmjs.com/package/markdown-tex)
|
|
108
|
+
[](https://github.com/JaredYe04/markdown-tex)
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## 🚀 安装
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npm install markdown-tex
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## ✨ 特性
|
|
121
|
+
|
|
122
|
+
- **🔄 双向转换** — Markdown → LaTeX 正文、LaTeX 正文 → Markdown
|
|
123
|
+
- **🌳 统一 AST** — 单一中间表示,双向共用
|
|
124
|
+
- **✅ 往返安全** — 支持的语法在 MD → LaTeX → MD 后保持一致
|
|
125
|
+
- **📦 开箱即用** — 仅输出正文,不包含 `\documentclass` 与宏包(可自建前言)
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## 📖 API
|
|
130
|
+
|
|
131
|
+
| 方法 | 说明 |
|
|
132
|
+
|------|------|
|
|
133
|
+
| `markdownToLatex(md)` | Markdown 字符串 → LaTeX **正文**(无 `\documentclass`) |
|
|
134
|
+
| `latexToMarkdown(latex)` | LaTeX 正文 → Markdown 字符串 |
|
|
135
|
+
| `markdownToAST(md)` | Markdown → 统一 AST |
|
|
136
|
+
| `latexToAST(latex)` | LaTeX → 统一 AST |
|
|
137
|
+
| `normalizeAST(ast)` | 规范化 AST,用于往返比较 |
|
|
138
|
+
| `escapeLatex(str)` | 转义 `# $ % & _ { }` 等,用于前言/标题页 |
|
|
139
|
+
|
|
140
|
+
**类型:** `AST`、`BlockNode`、`InlineNode`(TypeScript 导出)。
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## 💡 示例
|
|
145
|
+
|
|
146
|
+
### 基础转换
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { markdownToLatex, latexToMarkdown } from 'markdown-tex'
|
|
150
|
+
|
|
151
|
+
// Markdown → LaTeX 正文
|
|
152
|
+
const latex = markdownToLatex('# 你好 **世界**\n\n- 第一项\n- 第二项')
|
|
153
|
+
// → \section{你好 \textbf{世界}}
|
|
154
|
+
// \begin{itemize}
|
|
155
|
+
// \item 第一项
|
|
156
|
+
// \item 第二项
|
|
157
|
+
// \end{itemize}
|
|
158
|
+
|
|
159
|
+
// LaTeX → Markdown
|
|
160
|
+
const md = latexToMarkdown('\\section{标题}\n\n内容与\\textbf{加粗}。')
|
|
161
|
+
// → # 标题
|
|
162
|
+
// 内容与**加粗**。
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### 数学与代码
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
const latex = markdownToLatex('行内 $E=mc^2$,块级:\n\n$$\n\\int_0^1 x\\,dx\n$$')
|
|
169
|
+
// 行内公式 → $...$;块级公式 → \[ ... \]
|
|
170
|
+
|
|
171
|
+
const code = markdownToLatex('```js\nconst x = 1\n```')
|
|
172
|
+
// → \begin{verbatim} ... \end{verbatim}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### 标题页/页眉转义
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
import { escapeLatex } from 'markdown-tex'
|
|
179
|
+
|
|
180
|
+
const title = escapeLatex('价格:100% & “引号”')
|
|
181
|
+
// → 价格:100\% \& \"引号\"
|
|
182
|
+
// 用于 \title{}、\lhead{} 等
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## 🔗 链接
|
|
188
|
+
|
|
189
|
+
- **仓库:** [github.com/JaredYe04/markdown-tex](https://github.com/JaredYe04/markdown-tex)
|
|
190
|
+
- **npm:** [markdown-tex](https://www.npmjs.com/package/markdown-tex)
|
|
191
|
+
|
|
192
|
+
</details>
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified AST for Markdown / LaTeX bidirectional conversion.
|
|
3
|
+
* All block-level nodes extend BlockNode; inline nodes extend InlineNode.
|
|
4
|
+
*/
|
|
5
|
+
export interface DocumentNode {
|
|
6
|
+
type: 'document';
|
|
7
|
+
children: BlockNode[];
|
|
8
|
+
}
|
|
9
|
+
export interface HeadingNode {
|
|
10
|
+
type: 'heading';
|
|
11
|
+
level: 1 | 2 | 3;
|
|
12
|
+
children: InlineNode[];
|
|
13
|
+
}
|
|
14
|
+
export interface ParagraphNode {
|
|
15
|
+
type: 'paragraph';
|
|
16
|
+
children: InlineNode[];
|
|
17
|
+
}
|
|
18
|
+
export interface ListNode {
|
|
19
|
+
type: 'list';
|
|
20
|
+
ordered: boolean;
|
|
21
|
+
items: ListItemNode[];
|
|
22
|
+
}
|
|
23
|
+
export interface ListItemNode {
|
|
24
|
+
type: 'list_item';
|
|
25
|
+
children: BlockNode[];
|
|
26
|
+
}
|
|
27
|
+
export interface CodeBlockNode {
|
|
28
|
+
type: 'code_block';
|
|
29
|
+
content: string;
|
|
30
|
+
/** optional language tag (e.g. "javascript") */
|
|
31
|
+
lang?: string;
|
|
32
|
+
}
|
|
33
|
+
export interface MathBlockNode {
|
|
34
|
+
type: 'math_block';
|
|
35
|
+
content: string;
|
|
36
|
+
}
|
|
37
|
+
export interface BlockquoteNode {
|
|
38
|
+
type: 'blockquote';
|
|
39
|
+
children: BlockNode[];
|
|
40
|
+
}
|
|
41
|
+
export interface ThematicBreakNode {
|
|
42
|
+
type: 'thematic_break';
|
|
43
|
+
}
|
|
44
|
+
/** GFM-style table: header row + optional separator + data rows (cell text only) */
|
|
45
|
+
export interface TableNode {
|
|
46
|
+
type: 'table';
|
|
47
|
+
headerRow: string[];
|
|
48
|
+
rows: string[][];
|
|
49
|
+
}
|
|
50
|
+
/** Unknown block: raw source for round-trip or error recovery */
|
|
51
|
+
export interface UnknownBlockNode {
|
|
52
|
+
type: 'unknown';
|
|
53
|
+
raw: string;
|
|
54
|
+
}
|
|
55
|
+
export type BlockNode = DocumentNode | HeadingNode | ParagraphNode | ListNode | ListItemNode | CodeBlockNode | MathBlockNode | BlockquoteNode | ThematicBreakNode | TableNode | UnknownBlockNode;
|
|
56
|
+
export interface TextNode {
|
|
57
|
+
type: 'text';
|
|
58
|
+
value: string;
|
|
59
|
+
}
|
|
60
|
+
export interface StrongNode {
|
|
61
|
+
type: 'strong';
|
|
62
|
+
children: InlineNode[];
|
|
63
|
+
}
|
|
64
|
+
export interface EmphasisNode {
|
|
65
|
+
type: 'emphasis';
|
|
66
|
+
children: InlineNode[];
|
|
67
|
+
}
|
|
68
|
+
export interface InlineCodeNode {
|
|
69
|
+
type: 'inline_code';
|
|
70
|
+
value: string;
|
|
71
|
+
}
|
|
72
|
+
export interface LinkNode {
|
|
73
|
+
type: 'link';
|
|
74
|
+
url: string;
|
|
75
|
+
children: InlineNode[];
|
|
76
|
+
}
|
|
77
|
+
export interface ImageNode {
|
|
78
|
+
type: 'image';
|
|
79
|
+
url: string;
|
|
80
|
+
alt?: string;
|
|
81
|
+
}
|
|
82
|
+
export interface MathInlineNode {
|
|
83
|
+
type: 'math_inline';
|
|
84
|
+
content: string;
|
|
85
|
+
}
|
|
86
|
+
export interface StrikethroughNode {
|
|
87
|
+
type: 'strikethrough';
|
|
88
|
+
children: InlineNode[];
|
|
89
|
+
}
|
|
90
|
+
/** Unknown inline: raw source */
|
|
91
|
+
export interface UnknownInlineNode {
|
|
92
|
+
type: 'unknown_inline';
|
|
93
|
+
raw: string;
|
|
94
|
+
}
|
|
95
|
+
export type InlineNode = TextNode | StrongNode | EmphasisNode | InlineCodeNode | LinkNode | ImageNode | MathInlineNode | StrikethroughNode | UnknownInlineNode;
|
|
96
|
+
export declare function isDocumentNode(n: BlockNode): n is DocumentNode;
|
|
97
|
+
export declare function isHeadingNode(n: BlockNode): n is HeadingNode;
|
|
98
|
+
export declare function isParagraphNode(n: BlockNode): n is ParagraphNode;
|
|
99
|
+
export declare function isListNode(n: BlockNode): n is ListNode;
|
|
100
|
+
export declare function isListItemNode(n: BlockNode): n is ListItemNode;
|
|
101
|
+
export declare function isCodeBlockNode(n: BlockNode): n is CodeBlockNode;
|
|
102
|
+
export declare function isMathBlockNode(n: BlockNode): n is MathBlockNode;
|
|
103
|
+
export declare function isBlockquoteNode(n: BlockNode): n is BlockquoteNode;
|
|
104
|
+
export declare function isThematicBreakNode(n: BlockNode): n is ThematicBreakNode;
|
|
105
|
+
export declare function isTableNode(n: BlockNode): n is TableNode;
|
|
106
|
+
export declare function isUnknownBlockNode(n: BlockNode): n is UnknownBlockNode;
|
|
107
|
+
export declare function isTextNode(n: InlineNode): n is TextNode;
|
|
108
|
+
export declare function isStrongNode(n: InlineNode): n is StrongNode;
|
|
109
|
+
export declare function isEmphasisNode(n: InlineNode): n is EmphasisNode;
|
|
110
|
+
export declare function isInlineCodeNode(n: InlineNode): n is InlineCodeNode;
|
|
111
|
+
export declare function isLinkNode(n: InlineNode): n is LinkNode;
|
|
112
|
+
export declare function isImageNode(n: InlineNode): n is ImageNode;
|
|
113
|
+
export declare function isMathInlineNode(n: InlineNode): n is MathInlineNode;
|
|
114
|
+
export declare function isStrikethroughNode(n: InlineNode): n is StrikethroughNode;
|
|
115
|
+
export declare function isUnknownInlineNode(n: InlineNode): n is UnknownInlineNode;
|
|
116
|
+
/** Root AST type */
|
|
117
|
+
export type AST = DocumentNode;
|
|
118
|
+
/** Create empty document */
|
|
119
|
+
export declare function createDocument(children?: BlockNode[]): DocumentNode;
|
|
120
|
+
//# sourceMappingURL=nodes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.d.ts","sourceRoot":"","sources":["../../src/ast/nodes.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,UAAU,CAAA;IAChB,QAAQ,EAAE,SAAS,EAAE,CAAA;CACtB;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,SAAS,CAAA;IACf,KAAK,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAChB,QAAQ,EAAE,UAAU,EAAE,CAAA;CACvB;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,WAAW,CAAA;IACjB,QAAQ,EAAE,UAAU,EAAE,CAAA;CACvB;AAED,MAAM,WAAW,Q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AE,SAAS,EAAO,GAAG,YAAY,CAEvE"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Unified AST for Markdown / LaTeX bidirectional conversion.
|
|
4
|
+
* All block-level nodes extend BlockNode; inline nodes extend InlineNode.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.isDocumentNode = isDocumentNode;
|
|
8
|
+
exports.isHeadingNode = isHeadingNode;
|
|
9
|
+
exports.isParagraphNode = isParagraphNode;
|
|
10
|
+
exports.isListNode = isListNode;
|
|
11
|
+
exports.isListItemNode = isListItemNode;
|
|
12
|
+
exports.isCodeBlockNode = isCodeBlockNode;
|
|
13
|
+
exports.isMathBlockNode = isMathBlockNode;
|
|
14
|
+
exports.isBlockquoteNode = isBlockquoteNode;
|
|
15
|
+
exports.isThematicBreakNode = isThematicBreakNode;
|
|
16
|
+
exports.isTableNode = isTableNode;
|
|
17
|
+
exports.isUnknownBlockNode = isUnknownBlockNode;
|
|
18
|
+
exports.isTextNode = isTextNode;
|
|
19
|
+
exports.isStrongNode = isStrongNode;
|
|
20
|
+
exports.isEmphasisNode = isEmphasisNode;
|
|
21
|
+
exports.isInlineCodeNode = isInlineCodeNode;
|
|
22
|
+
exports.isLinkNode = isLinkNode;
|
|
23
|
+
exports.isImageNode = isImageNode;
|
|
24
|
+
exports.isMathInlineNode = isMathInlineNode;
|
|
25
|
+
exports.isStrikethroughNode = isStrikethroughNode;
|
|
26
|
+
exports.isUnknownInlineNode = isUnknownInlineNode;
|
|
27
|
+
exports.createDocument = createDocument;
|
|
28
|
+
// --- Type guards ---
|
|
29
|
+
function isDocumentNode(n) {
|
|
30
|
+
return n.type === 'document';
|
|
31
|
+
}
|
|
32
|
+
function isHeadingNode(n) {
|
|
33
|
+
return n.type === 'heading';
|
|
34
|
+
}
|
|
35
|
+
function isParagraphNode(n) {
|
|
36
|
+
return n.type === 'paragraph';
|
|
37
|
+
}
|
|
38
|
+
function isListNode(n) {
|
|
39
|
+
return n.type === 'list';
|
|
40
|
+
}
|
|
41
|
+
function isListItemNode(n) {
|
|
42
|
+
return n.type === 'list_item';
|
|
43
|
+
}
|
|
44
|
+
function isCodeBlockNode(n) {
|
|
45
|
+
return n.type === 'code_block';
|
|
46
|
+
}
|
|
47
|
+
function isMathBlockNode(n) {
|
|
48
|
+
return n.type === 'math_block';
|
|
49
|
+
}
|
|
50
|
+
function isBlockquoteNode(n) {
|
|
51
|
+
return n.type === 'blockquote';
|
|
52
|
+
}
|
|
53
|
+
function isThematicBreakNode(n) {
|
|
54
|
+
return n.type === 'thematic_break';
|
|
55
|
+
}
|
|
56
|
+
function isTableNode(n) {
|
|
57
|
+
return n.type === 'table';
|
|
58
|
+
}
|
|
59
|
+
function isUnknownBlockNode(n) {
|
|
60
|
+
return n.type === 'unknown';
|
|
61
|
+
}
|
|
62
|
+
function isTextNode(n) {
|
|
63
|
+
return n.type === 'text';
|
|
64
|
+
}
|
|
65
|
+
function isStrongNode(n) {
|
|
66
|
+
return n.type === 'strong';
|
|
67
|
+
}
|
|
68
|
+
function isEmphasisNode(n) {
|
|
69
|
+
return n.type === 'emphasis';
|
|
70
|
+
}
|
|
71
|
+
function isInlineCodeNode(n) {
|
|
72
|
+
return n.type === 'inline_code';
|
|
73
|
+
}
|
|
74
|
+
function isLinkNode(n) {
|
|
75
|
+
return n.type === 'link';
|
|
76
|
+
}
|
|
77
|
+
function isImageNode(n) {
|
|
78
|
+
return n.type === 'image';
|
|
79
|
+
}
|
|
80
|
+
function isMathInlineNode(n) {
|
|
81
|
+
return n.type === 'math_inline';
|
|
82
|
+
}
|
|
83
|
+
function isStrikethroughNode(n) {
|
|
84
|
+
return n.type === 'strikethrough';
|
|
85
|
+
}
|
|
86
|
+
function isUnknownInlineNode(n) {
|
|
87
|
+
return n.type === 'unknown_inline';
|
|
88
|
+
}
|
|
89
|
+
/** Create empty document */
|
|
90
|
+
function createDocument(children = []) {
|
|
91
|
+
return { type: 'document', children };
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=nodes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nodes.js","sourceRoot":"","sources":["../../src/ast/nodes.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AA6IH,wCAEC;AAED,sCAEC;AAED,0CAEC;AAED,gCAEC;AAED,wCAEC;AAED,0CAEC;AAED,0CAEC;AAED,4CAEC;AAED,kDAEC;AAED,kCAEC;AAED,gDAEC;AAED,gCAEC;AAED,oCAEC;AAED,wCAEC;AAED,4CAEC;AAED,gCAEC;AAED,kCAEC;AAED,4CAEC;AAED,kDAEC;AAED,kDAEC;AAMD,wCAEC;AAxFD,sBAAsB;AAEtB,SAAgB,cAAc,CAAC,CAAY;IACzC,OAAO,CAAC,CAAC,IAAI,KAAK,UAAU,CAAA;AAC9B,CAAC;AAED,SAAgB,aAAa,CAAC,CAAY;IACxC,OAAO,CAAC,CAAC,IAAI,KAAK,SAAS,CAAA;AAC7B,CAAC;AAED,SAAgB,eAAe,CAAC,CAAY;IAC1C,OAAO,CAAC,CAAC,IAAI,KAAK,WAAW,CAAA;AAC/B,CAAC;AAED,SAAgB,UAAU,CAAC,CAAY;IACrC,OAAO,CAAC,CAAC,IAAI,KAAK,MAAM,CAAA;AAC1B,CAAC;AAED,SAAgB,cAAc,CAAC,CAAY;IACzC,OAAO,CAAC,CAAC,IAAI,KAAK,WAAW,CAAA;AAC/B,CAAC;AAED,SAAgB,eAAe,CAAC,CAAY;IAC1C,OAAO,CAAC,CAAC,IAAI,KAAK,YAAY,CAAA;AAChC,CAAC;AAED,SAAgB,eAAe,CAAC,CAAY;IAC1C,OAAO,CAAC,CAAC,IAAI,KAAK,YAAY,CAAA;AAChC,CAAC;AAED,SAAgB,gBAAgB,CAAC,CAAY;IAC3C,OAAO,CAAC,CAAC,IAAI,KAAK,YAAY,CAAA;AAChC,CAAC;AAED,SAAgB,mBAAmB,CAAC,C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.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown-tex: Markdown ⇄ LaTeX conversion library.
|
|
3
|
+
* Uses AST as intermediate representation; round-trip safe for supported subset.
|
|
4
|
+
*/
|
|
5
|
+
import { escapeLatex } from './latex/latexRenderer';
|
|
6
|
+
import { normalizeAST } from './transform/normalize';
|
|
7
|
+
import type { AST } from './ast/nodes';
|
|
8
|
+
export type { AST } from './ast/nodes';
|
|
9
|
+
export type { BlockNode, InlineNode } from './ast/nodes';
|
|
10
|
+
/** Markdown string → LaTeX body string (no document wrapper) */
|
|
11
|
+
export declare function markdownToLatex(markdown: string): string;
|
|
12
|
+
/** LaTeX body string → Markdown string */
|
|
13
|
+
export declare function latexToMarkdown(latex: string): string;
|
|
14
|
+
/** Markdown string → AST */
|
|
15
|
+
export declare function markdownToAST(markdown: string): AST;
|
|
16
|
+
/** LaTeX string → AST */
|
|
17
|
+
export declare function latexToAST(latex: string): AST;
|
|
18
|
+
/** Normalize AST for round-trip comparison (merge adjacent text, trim, etc.) */
|
|
19
|
+
export { normalizeAST };
|
|
20
|
+
/** Escape LaTeX special characters in plain text (for use in preamble, titlepage, etc.) */
|
|
21
|
+
export { escapeLatex };
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAAe,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAEtC,YAAY,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExD,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED,0CAA0C;AAC1C,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED,4BAA4B;AAC5B,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,GAAG,CAEnD;AAED,yBAAyB;AACzB,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAE7C;AAED,gFAAgF;AAChF,OAAO,EAAE,YAAY,EAAE,CAAA;AAEvB,2FAA2F;AAC3F,OAAO,EAAE,WAAW,EAAE,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Markdown-tex: Markdown ⇄ LaTeX conversion library.
|
|
4
|
+
* Uses AST as intermediate representation; round-trip safe for supported subset.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.escapeLatex = exports.normalizeAST = void 0;
|
|
8
|
+
exports.markdownToLatex = markdownToLatex;
|
|
9
|
+
exports.latexToMarkdown = latexToMarkdown;
|
|
10
|
+
exports.markdownToAST = markdownToAST;
|
|
11
|
+
exports.latexToAST = latexToAST;
|
|
12
|
+
const markdownParser_1 = require("./markdown/markdownParser");
|
|
13
|
+
const markdownRenderer_1 = require("./markdown/markdownRenderer");
|
|
14
|
+
const latexParser_1 = require("./latex/latexParser");
|
|
15
|
+
const latexRenderer_1 = require("./latex/latexRenderer");
|
|
16
|
+
Object.defineProperty(exports, "escapeLatex", { enumerable: true, get: function () { return latexRenderer_1.escapeLatex; } });
|
|
17
|
+
const normalize_1 = require("./transform/normalize");
|
|
18
|
+
Object.defineProperty(exports, "normalizeAST", { enumerable: true, get: function () { return normalize_1.normalizeAST; } });
|
|
19
|
+
/** Markdown string → LaTeX body string (no document wrapper) */
|
|
20
|
+
function markdownToLatex(markdown) {
|
|
21
|
+
const ast = (0, markdownParser_1.parseMarkdown)(markdown);
|
|
22
|
+
return (0, latexRenderer_1.renderLatex)(ast);
|
|
23
|
+
}
|
|
24
|
+
/** LaTeX body string → Markdown string */
|
|
25
|
+
function latexToMarkdown(latex) {
|
|
26
|
+
const ast = (0, latexParser_1.parseLatex)(latex);
|
|
27
|
+
return (0, markdownRenderer_1.renderMarkdown)(ast);
|
|
28
|
+
}
|
|
29
|
+
/** Markdown string → AST */
|
|
30
|
+
function markdownToAST(markdown) {
|
|
31
|
+
return (0, markdownParser_1.parseMarkdown)(markdown);
|
|
32
|
+
}
|
|
33
|
+
/** LaTeX string → AST */
|
|
34
|
+
function latexToAST(latex) {
|
|
35
|
+
return (0, latexParser_1.parseLatex)(latex);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAaH,0CAGC;AAGD,0CAGC;AAGD,sCAEC;AAGD,gCAEC;AA9BD,8DAAyD;AACzD,kEAA4D;AAC5D,qDAAgD;AAChD,yDAAgE;AAiCvD,4FAjCa,2BAAW,OAiCb;AAhCpB,qDAAoD;AA6B3C,6FA7BA,wBAAY,OA6BA;AAvBrB,gEAAgE;AAChE,SAAgB,eAAe,CAAC,QAAgB;IAC9C,MAAM,GAAG,GAAG,IAAA,8BAAa,EAAC,QAAQ,CAAC,CAAA;IACnC,OAAO,IAAA,2BAAW,EAAC,GAAG,CAAC,CAAA;AACzB,CAAC;AAED,0CAA0C;AAC1C,SAAgB,eAAe,CAAC,KAAa;IAC3C,MAAM,GAAG,GAAG,IAAA,wBAAU,EAAC,KAAK,CAAC,CAAA;IAC7B,OAAO,IAAA,iCAAc,EAAC,GAAG,CAAC,CAAA;AAC5B,CAAC;AAED,4BAA4B;AAC5B,SAAgB,aAAa,CAAC,QAAgB;IAC5C,OAAO,IAAA,8BAAa,EAAC,QAAQ,CAAC,CAAA;AAChC,CAAC;AAED,yBAAyB;AACzB,SAAgB,UAAU,CAAC,KAAa;IACtC,OAAO,IAAA,wBAAU,EAAC,KAAK,CAAC,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LaTeX → MetaDoc AST parser.
|
|
3
|
+
* Parses document body (between \begin{document} and \end{document} if present).
|
|
4
|
+
* Supports: \section, \subsection, \subsubsection, \textbf, \textit, \texttt,
|
|
5
|
+
* \begin{itemize}, \begin{enumerate}, \begin{verbatim}, \href{}{}, \includegraphics{},
|
|
6
|
+
* \[ ... \], and treats unknown commands as unknown nodes (no crash).
|
|
7
|
+
*/
|
|
8
|
+
import type { AST } from '../ast/nodes';
|
|
9
|
+
/** Extract body between \begin{document} and \end{document}; otherwise use full input; then strip control blocks */
|
|
10
|
+
export declare function sanitizeLatexBody(latex: string): string;
|
|
11
|
+
export declare function parseLatex(latex: string): AST;
|
|
12
|
+
//# sourceMappingURL=latexParser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"latexParser.d.ts","sourceRoot":"","sources":["../../src/latex/latexParser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACV,GAAG,EAaJ,MAAM,cAAc,CAAA;AA+CrB,oHAAoH;AACpH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAgBvD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,CAI7C"}
|