@open-guji/markdown-core 0.1.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 +201 -0
- package/README.md +104 -0
- package/dist/directives.d.ts +39 -0
- package/dist/directives.d.ts.map +1 -0
- package/dist/directives.js +106 -0
- package/dist/directives.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/normalize.d.ts +31 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +479 -0
- package/dist/normalize.js.map +1 -0
- package/dist/options.d.ts +81 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +34 -0
- package/dist/options.js.map +1 -0
- package/dist/parse.d.ts +11 -0
- package/dist/parse.d.ts.map +1 -0
- package/dist/parse.js +24 -0
- package/dist/parse.js.map +1 -0
- package/dist/plugin.d.ts +18 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +49 -0
- package/dist/plugin.js.map +1 -0
- package/dist/transform.d.ts +15 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +73 -0
- package/dist/transform.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# @open-guji/markdown-core
|
|
2
|
+
|
|
3
|
+
古籍 Markdown 扩展的**平台无关内核**:把源文里的短记号规范化为
|
|
4
|
+
[Directive](https://talk.commonmark.org/t/generic-directives-plugins-syntax/444)
|
|
5
|
+
中间表示,并提供解析成 mdast 的 remark 插件。
|
|
6
|
+
|
|
7
|
+
渲染器([web](../web)、[pdf](../pdf))都建在它之上;写新平台的渲染器时,
|
|
8
|
+
只要能消费指令,就不必重写词法分析。
|
|
9
|
+
|
|
10
|
+
规范见仓库 [`spec/`](../../spec),本包实现规范 **0.1.0**。
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i @open-guji/markdown-core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 用法
|
|
19
|
+
|
|
20
|
+
### 只要中间表示
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { gujiToDirective } from '@open-guji/markdown-core'
|
|
24
|
+
|
|
25
|
+
gujiToDirective('^惟我\n.皇上<聖躬>,本作[甲]{{乙}}')
|
|
26
|
+
// ':tt{n=1}惟我\n:kg{n=1}皇上:jz[聖躬],本作:gz[甲]:sz[乙]'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
纯字符串到字符串,无副作用,**逐行处理且不改变行数**——
|
|
30
|
+
渲染期报出的行号可以直接映射回源文。
|
|
31
|
+
|
|
32
|
+
### 解析成 mdast
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { parseGuji } from '@open-guji/markdown-core'
|
|
36
|
+
|
|
37
|
+
parseGuji('子曰<孔子也>。')
|
|
38
|
+
// root > paragraph > [ text('子曰'), textDirective{ name:'jz', children:[text('孔子也')] }, text('。') ]
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 作为 remark 插件
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import { unified } from 'unified'
|
|
45
|
+
import remarkParse from 'remark-parse'
|
|
46
|
+
import remarkGuji from '@open-guji/markdown-core'
|
|
47
|
+
import remarkRehype from 'remark-rehype'
|
|
48
|
+
|
|
49
|
+
unified().use(remarkParse).use(remarkGuji).use(remarkRehype)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
插件会自己注册 directive 的 micromark / mdast 扩展,**不需要再 use `remark-directive`**。
|
|
53
|
+
它必须排在 `remark-parse` 之后(要包裹 parser 才能在解析前完成规范化)。
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
| 导出 | 说明 |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `remarkGuji`(默认导出) | remark 插件 |
|
|
60
|
+
| `gujiToDirective(source, options?)` | 源文 → Directive Markdown |
|
|
61
|
+
| `transformInline(text, options?)` | 只做行内记号转换(单行) |
|
|
62
|
+
| `parseGuji(source, options?)` | 源文 → mdast |
|
|
63
|
+
| `gujiProcessor(options?)` | 返回配好的 unified processor |
|
|
64
|
+
| `formatDirective(tree, options?)` | mdast → Directive Markdown |
|
|
65
|
+
| `applyLineScope(tree)` | 抬头的行作用域归并 |
|
|
66
|
+
| `normalizeDirectiveNames(tree)` | 指令别名归一 |
|
|
67
|
+
| `DIRECTIVES` / `lookupDirective` / `resolveDirectiveName` / `parseLevel` | 指令注册表 |
|
|
68
|
+
| `buildMask` / `escapeDirectiveLabel` | 规范化用到的底层工具,写别的实现时可参考 |
|
|
69
|
+
|
|
70
|
+
## 选项
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
interface GujiOptions {
|
|
74
|
+
// 各记号开关,默认全开
|
|
75
|
+
jz?: boolean; bz?: boolean; sz?: boolean; gz?: boolean
|
|
76
|
+
tt?: boolean; kg?: boolean; zm?: boolean; sm?: boolean
|
|
77
|
+
|
|
78
|
+
maxSpanLength?: number // 行内记号内容上限,默认 64
|
|
79
|
+
maxLevel?: number // n 属性上限,默认 9
|
|
80
|
+
gzRequireCjk?: boolean // 改字内容须含 CJK,默认 true
|
|
81
|
+
zmRequireCjk?: boolean // 专名号内容须含 CJK,默认 true
|
|
82
|
+
indentedCode?: boolean // 识别缩进代码块,默认 true
|
|
83
|
+
|
|
84
|
+
shorthand?: boolean // 解析前先规范化短记号,默认 true
|
|
85
|
+
lineScope?: boolean // 抬头的行作用域归并,默认 true
|
|
86
|
+
normalizeNames?: boolean // 指令别名归一,默认 true
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
各项的语义与边界条件见 [`spec/syntax.md`](../../spec/syntax.md)。
|
|
91
|
+
|
|
92
|
+
## 已知限制
|
|
93
|
+
|
|
94
|
+
* **列号不精确**。规范化会在行内插入 `:jz[` 等文本,因此 mdast 节点的
|
|
95
|
+
`column` 指向的是规范化之后的文本。行号是准的。
|
|
96
|
+
路线图里有一版基于 micromark 词法扩展的实现,会解决这一点。
|
|
97
|
+
* **记号不跨行内结构**。像 `/孔**子/**` 这种跨越强调边界的写法属未定义行为。
|
|
98
|
+
* **简写式引用链接与改字冲突**。`[說文]`(配合别处的 `[說文]: url` 定义)会被
|
|
99
|
+
识别为改字;行内链接 `[說文](url)` 不受影响。见
|
|
100
|
+
[`spec/syntax.md` §4](../../spec/syntax.md#4-改字-)。
|
|
101
|
+
|
|
102
|
+
## 许可证
|
|
103
|
+
|
|
104
|
+
Apache-2.0
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指令注册表。规范见 spec/directives.md。
|
|
3
|
+
*/
|
|
4
|
+
export type DirectiveShape = 'text' | 'marker' | 'leaf' | 'container';
|
|
5
|
+
export interface DirectiveSpec {
|
|
6
|
+
/** 规范名,两字拼音首字母 */
|
|
7
|
+
name: string;
|
|
8
|
+
/** 中文名 */
|
|
9
|
+
cn: string;
|
|
10
|
+
/** 可接受的别名,解析时归一到 name */
|
|
11
|
+
aliases: readonly string[];
|
|
12
|
+
/** 允许的形态 */
|
|
13
|
+
shapes: readonly DirectiveShape[];
|
|
14
|
+
/** 已定义的属性名 */
|
|
15
|
+
attributes: readonly string[];
|
|
16
|
+
/** 默认 HTML 元素(行内形态) */
|
|
17
|
+
tagName: string;
|
|
18
|
+
/** 容器形态的 HTML 元素,未定义则不支持容器形态 */
|
|
19
|
+
blockTagName?: string;
|
|
20
|
+
/** 对应的源文短记号,供文档与反向规范化使用 */
|
|
21
|
+
shorthand?: {
|
|
22
|
+
open: string;
|
|
23
|
+
close: string;
|
|
24
|
+
} | {
|
|
25
|
+
linePrefix: string;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export declare const DIRECTIVES: readonly DirectiveSpec[];
|
|
29
|
+
/** 取指令定义;名字可以是规范名或别名。 */
|
|
30
|
+
export declare function lookupDirective(name: string): DirectiveSpec | undefined;
|
|
31
|
+
/** 把别名归一到规范名;未注册的名字原样返回。 */
|
|
32
|
+
export declare function resolveDirectiveName(name: string): string;
|
|
33
|
+
/** 是否为古籍扩展定义的指令。 */
|
|
34
|
+
export declare function isGujiDirective(name: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 解析 `n` 属性:非数字、小于 1 时取 1,超过上限时钳制。
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseLevel(value: unknown, maxLevel?: number): number;
|
|
39
|
+
//# sourceMappingURL=directives.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../src/directives.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAA;AAErE,MAAM,WAAW,aAAa;IAC5B,kBAAkB;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU;IACV,EAAE,EAAE,MAAM,CAAA;IACV,yBAAyB;IACzB,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;IAC1B,YAAY;IACZ,MAAM,EAAE,SAAS,cAAc,EAAE,CAAA;IACjC,cAAc;IACd,UAAU,EAAE,SAAS,MAAM,EAAE,CAAA;IAC7B,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,gCAAgC;IAChC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,2BAA2B;IAC3B,SAAS,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,CAAA;CACrE;AAED,eAAO,MAAM,UAAU,EAAE,SAAS,aAAa,EA0E9C,CAAA;AAQD,yBAAyB;AACzB,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,SAAS,CAEvE;AAED,4BAA4B;AAC5B,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzD;AAED,oBAAoB;AACpB,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,QAAQ,SAAI,GAAG,MAAM,CAI/D"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 指令注册表。规范见 spec/directives.md。
|
|
3
|
+
*/
|
|
4
|
+
export const DIRECTIVES = [
|
|
5
|
+
{
|
|
6
|
+
name: 'jz',
|
|
7
|
+
cn: '夹注',
|
|
8
|
+
aliases: ['note', 'jiazhu', 'xiaozhu'],
|
|
9
|
+
shapes: ['text'],
|
|
10
|
+
attributes: ['type'],
|
|
11
|
+
tagName: 'span',
|
|
12
|
+
shorthand: { open: '<', close: '>' }
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: 'bz',
|
|
16
|
+
cn: '补字',
|
|
17
|
+
aliases: ['add', 'ins', 'buzi'],
|
|
18
|
+
shapes: ['text'],
|
|
19
|
+
attributes: ['src'],
|
|
20
|
+
tagName: 'ins',
|
|
21
|
+
shorthand: { open: '((', close: '))' }
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: 'sz',
|
|
25
|
+
cn: '删字',
|
|
26
|
+
aliases: ['del', 'shanzi'],
|
|
27
|
+
shapes: ['text'],
|
|
28
|
+
attributes: ['src'],
|
|
29
|
+
tagName: 'del',
|
|
30
|
+
shorthand: { open: '{{', close: '}}' }
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'gz',
|
|
34
|
+
cn: '改字',
|
|
35
|
+
aliases: ['sub', 'amend', 'gaizi'],
|
|
36
|
+
shapes: ['text'],
|
|
37
|
+
attributes: ['src'],
|
|
38
|
+
tagName: 'ins',
|
|
39
|
+
shorthand: { open: '[', close: ']' }
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: 'tt',
|
|
43
|
+
cn: '抬头',
|
|
44
|
+
aliases: ['raise', 'taitou'],
|
|
45
|
+
shapes: ['marker', 'text', 'container'],
|
|
46
|
+
attributes: ['n'],
|
|
47
|
+
tagName: 'span',
|
|
48
|
+
blockTagName: 'div',
|
|
49
|
+
shorthand: { linePrefix: '^' }
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: 'kg',
|
|
53
|
+
cn: '空格',
|
|
54
|
+
aliases: ['space', 'kongge'],
|
|
55
|
+
shapes: ['marker'],
|
|
56
|
+
attributes: ['n'],
|
|
57
|
+
tagName: 'span',
|
|
58
|
+
shorthand: { linePrefix: '.' }
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'zm',
|
|
62
|
+
cn: '专名号',
|
|
63
|
+
aliases: ['name', 'zhuanming'],
|
|
64
|
+
shapes: ['text'],
|
|
65
|
+
attributes: ['type'],
|
|
66
|
+
tagName: 'span',
|
|
67
|
+
shorthand: { open: '/', close: '/' }
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: 'sm',
|
|
71
|
+
cn: '书名号',
|
|
72
|
+
aliases: ['book', 'shuming'],
|
|
73
|
+
shapes: ['text'],
|
|
74
|
+
attributes: ['part'],
|
|
75
|
+
tagName: 'span',
|
|
76
|
+
shorthand: { open: '《', close: '》' }
|
|
77
|
+
}
|
|
78
|
+
];
|
|
79
|
+
const BY_NAME = new Map();
|
|
80
|
+
for (const spec of DIRECTIVES) {
|
|
81
|
+
BY_NAME.set(spec.name, spec);
|
|
82
|
+
for (const alias of spec.aliases)
|
|
83
|
+
BY_NAME.set(alias, spec);
|
|
84
|
+
}
|
|
85
|
+
/** 取指令定义;名字可以是规范名或别名。 */
|
|
86
|
+
export function lookupDirective(name) {
|
|
87
|
+
return BY_NAME.get(name);
|
|
88
|
+
}
|
|
89
|
+
/** 把别名归一到规范名;未注册的名字原样返回。 */
|
|
90
|
+
export function resolveDirectiveName(name) {
|
|
91
|
+
return BY_NAME.get(name)?.name ?? name;
|
|
92
|
+
}
|
|
93
|
+
/** 是否为古籍扩展定义的指令。 */
|
|
94
|
+
export function isGujiDirective(name) {
|
|
95
|
+
return BY_NAME.has(name);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 解析 `n` 属性:非数字、小于 1 时取 1,超过上限时钳制。
|
|
99
|
+
*/
|
|
100
|
+
export function parseLevel(value, maxLevel = 9) {
|
|
101
|
+
const n = typeof value === 'number' ? value : Number.parseInt(String(value ?? ''), 10);
|
|
102
|
+
if (!Number.isFinite(n) || n < 1)
|
|
103
|
+
return 1;
|
|
104
|
+
return Math.min(Math.trunc(n), maxLevel);
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=directives.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"directives.js","sourceRoot":"","sources":["../src/directives.ts"],"names":[],"mappings":"AAAA;;GAEG;AAuBH,MAAM,CAAC,MAAM,UAAU,GAA6B;IAClD;QACE,IAAI,EAAE,IAAI;QACV,EAAE,EAAE,IAAI;QACR,OAAO,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC;QACtC,MAAM,EAAE,CAAC,MAAM,CAAC;QAChB,UAAU,EAAE,CAAC,MAAM,CAAC;QACpB,OAAO,EAAE,MAAM;QACf,SAAS,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,EAAE;KACrC;IAC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AI,IAAI,IAAI,CAAA;AACxC,CAAC;AAED,oBAAoB;AACpB,MAAM,UAAU,eAAe,CAAC,IAAY;IAC1C,OAAO,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAC1B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,KAAc,EAAE,QAAQ,GAAG,CAAC;IACrD,MAAM,CAAC,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IACtF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,CAAC,CAAA;IAC1C,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAA;AAC1C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-guji/markdown-core`
|
|
3
|
+
*
|
|
4
|
+
* 古籍 Markdown 扩展的平台无关内核:
|
|
5
|
+
* 短记号 → Directive 的规范化器、指令注册表、remark 插件与 mdast 变换。
|
|
6
|
+
*
|
|
7
|
+
* 规范见仓库 `spec/` 目录。
|
|
8
|
+
*/
|
|
9
|
+
export { default as remarkGuji, default } from './plugin.js';
|
|
10
|
+
export { gujiToDirective, transformInline, buildMask, escapeDirectiveLabel } from './normalize.js';
|
|
11
|
+
export { gujiProcessor, parseGuji, formatDirective } from './parse.js';
|
|
12
|
+
export { applyLineScope, normalizeDirectiveNames } from './transform.js';
|
|
13
|
+
export { DIRECTIVES, lookupDirective, resolveDirectiveName, isGujiDirective, parseLevel, type DirectiveSpec, type DirectiveShape } from './directives.js';
|
|
14
|
+
export { DEFAULT_SYNTAX_OPTIONS, resolveSyntaxOptions, resolveOptions, type GujiToggles, type GujiSyntaxOptions, type ResolvedSyntaxOptions, type GujiOptions, type ResolvedGujiOptions } from './options.js';
|
|
15
|
+
/** 本包实现的规范版本。 */
|
|
16
|
+
export declare const SPEC_VERSION = "0.1.0";
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAE5D,OAAO,EACL,eAAe,EACf,eAAe,EACf,SAAS,EACT,oBAAoB,EACrB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEtE,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAExE,OAAO,EACL,UAAU,EACV,eAAe,EACf,oBAAoB,EACpB,eAAe,EACf,UAAU,EACV,KAAK,aAAa,EAClB,KAAK,cAAc,EACpB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,cAAc,EACd,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,qBAAqB,EAC1B,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACzB,MAAM,cAAc,CAAA;AAErB,iBAAiB;AACjB,eAAO,MAAM,YAAY,UAAU,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-guji/markdown-core`
|
|
3
|
+
*
|
|
4
|
+
* 古籍 Markdown 扩展的平台无关内核:
|
|
5
|
+
* 短记号 → Directive 的规范化器、指令注册表、remark 插件与 mdast 变换。
|
|
6
|
+
*
|
|
7
|
+
* 规范见仓库 `spec/` 目录。
|
|
8
|
+
*/
|
|
9
|
+
export { default as remarkGuji, default } from './plugin.js';
|
|
10
|
+
export { gujiToDirective, transformInline, buildMask, escapeDirectiveLabel } from './normalize.js';
|
|
11
|
+
export { gujiProcessor, parseGuji, formatDirective } from './parse.js';
|
|
12
|
+
export { applyLineScope, normalizeDirectiveNames } from './transform.js';
|
|
13
|
+
export { DIRECTIVES, lookupDirective, resolveDirectiveName, isGujiDirective, parseLevel } from './directives.js';
|
|
14
|
+
export { DEFAULT_SYNTAX_OPTIONS, resolveSyntaxOptions, resolveOptions } from './options.js';
|
|
15
|
+
/** 本包实现的规范版本。 */
|
|
16
|
+
export const SPEC_VERSION = '0.1.0';
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA;AAE5D,OAAO,EACL,eAAe,EACf,eAAe,EACf,SAAS,EACT,oBAAoB,EACrB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEtE,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAA;AAExE,OAAO,EACL,UAAU,EACV,eAAe,EACf,oBAAoB,EACpB,eAAe,EACf,UAAU,EAGX,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,cAAc,EAMf,MAAM,cAAc,CAAA;AAErB,iBAAiB;AACjB,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 规范化器:古籍 Markdown 短记号 → Directive Markdown。
|
|
3
|
+
*
|
|
4
|
+
* 规范见 spec/syntax.md 与 spec/directives.md。三条硬性保证:
|
|
5
|
+
*
|
|
6
|
+
* 1. 逐行处理,输出行数与行序不变;
|
|
7
|
+
* 2. 幂等:`normalize(normalize(x)) === normalize(x)`;
|
|
8
|
+
* 3. 不生效区(代码块、代码片段、链接目标、HTML、转义)逐字保留。
|
|
9
|
+
*/
|
|
10
|
+
import { type GujiSyntaxOptions } from './options.js';
|
|
11
|
+
/**
|
|
12
|
+
* 构造与 `s` 等长的掩码串:不生效区的字符置为 NUL。
|
|
13
|
+
* 之后所有记号匹配都在掩码串上做、输出时取原串,索引天然对齐。
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildMask(s: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* 转义 label 中不配对的方括号,否则指令会被提前截断。
|
|
18
|
+
* 配对的方括号(例如 label 内含链接)保持原样。
|
|
19
|
+
*/
|
|
20
|
+
export declare function escapeDirectiveLabel(s: string): string;
|
|
21
|
+
/** 只做行内记号的转换,供测试与其他实现参考。 */
|
|
22
|
+
export declare function transformInline(text: string, options?: GujiSyntaxOptions): string;
|
|
23
|
+
/**
|
|
24
|
+
* 古籍 Markdown → Directive Markdown。
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* gujiToDirective('^惟我\n.皇上<聖躬>')
|
|
28
|
+
* // ':tt{n=1}惟我\n:kg{n=1}皇上:jz[聖躬]'
|
|
29
|
+
*/
|
|
30
|
+
export declare function gujiToDirective(source: string, options?: GujiSyntaxOptions): string;
|
|
31
|
+
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,cAAc,CAAA;AAwDrB;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CA8E3C;AAkJD;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAuBtD;AAyFD,4BAA4B;AAC5B,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAIjF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAkFnF"}
|