mdast-util-to-markdown-cjk-friendly 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 +28 -0
- package/README.md +16 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +248 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Copyright (c) 2025 Tatsunori Uchino <tats.u@live.jp>
|
|
2
|
+
|
|
3
|
+
MIT LICENSE
|
|
4
|
+
|
|
5
|
+
Based on remark-gfm:
|
|
6
|
+
|
|
7
|
+
(The MIT License)
|
|
8
|
+
|
|
9
|
+
Copyright (c) Titus Wormer <tituswormer@gmail.com>
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
12
|
+
a copy of this software and associated documentation files (the
|
|
13
|
+
'Software'), to deal in the Software without restriction, including
|
|
14
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
15
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
16
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
17
|
+
the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be
|
|
20
|
+
included in all copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
23
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
24
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
25
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
26
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
27
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
28
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# mdast-util-to-markdown-cjk-friendly
|
|
2
|
+
|
|
3
|
+
`mdast-util-to-markdown` extension for CJK-friendly emphasis and strong serialization.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { toMarkdown } from "mdast-util-to-markdown";
|
|
9
|
+
import { cjkFriendlyToMarkdown } from "mdast-util-to-markdown-cjk-friendly";
|
|
10
|
+
|
|
11
|
+
const markdown = toMarkdown(tree, {
|
|
12
|
+
extensions: [cjkFriendlyToMarkdown()],
|
|
13
|
+
});
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
This package is also loaded automatically by `remark-cjk-friendly`.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Options } from "mdast-util-to-markdown";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Add CJK-friendly `toMarkdown` handlers for emphasis and strong.
|
|
7
|
+
*/
|
|
8
|
+
declare function cjkFriendlyToMarkdown(): Options;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { cjkFriendlyToMarkdown };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { classifyCharacter, classifyPrecedingCharacter, isCjk, isCjkOrIvs, isNonCjkPunctuation, isNonEmojiGeneralUseVS, isSpaceOrPunctuation, isUnicodeWhitespace } from "micromark-extension-cjk-friendly-util";
|
|
2
|
+
import { codes } from "micromark-util-symbol";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const encodedOutsideBoundary = ";".codePointAt(0) ?? codes.eof;
|
|
6
|
+
const encodedInsideBoundary = "&".codePointAt(0) ?? codes.eof;
|
|
7
|
+
/**
|
|
8
|
+
* Add CJK-friendly `toMarkdown` handlers for emphasis and strong.
|
|
9
|
+
*/
|
|
10
|
+
function cjkFriendlyToMarkdown() {
|
|
11
|
+
return { handlers: {
|
|
12
|
+
emphasis,
|
|
13
|
+
strong,
|
|
14
|
+
text
|
|
15
|
+
} };
|
|
16
|
+
}
|
|
17
|
+
emphasis.peek = emphasisPeek;
|
|
18
|
+
strong.peek = strongPeek;
|
|
19
|
+
function emphasis(node, parent, state, info) {
|
|
20
|
+
return serializeAttention(node, parent, state, info, emphasisPeek(node, parent, state), 1);
|
|
21
|
+
}
|
|
22
|
+
function strong(node, parent, state, info) {
|
|
23
|
+
return serializeAttention(node, parent, state, info, strongPeek(node, parent, state), 2);
|
|
24
|
+
}
|
|
25
|
+
function emphasisPeek(_, _parent, state) {
|
|
26
|
+
return state.options.emphasis || "*";
|
|
27
|
+
}
|
|
28
|
+
function strongPeek(_, _parent, state) {
|
|
29
|
+
return state.options.strong || "*";
|
|
30
|
+
}
|
|
31
|
+
function serializeAttention(node, parent, state, info, marker, size) {
|
|
32
|
+
const sequence = marker.repeat(size);
|
|
33
|
+
const exit = state.enter(size === 1 ? "emphasis" : "strong");
|
|
34
|
+
const tracker = state.createTracker(info);
|
|
35
|
+
const before = tracker.move(sequence);
|
|
36
|
+
let between = tracker.move(state.containerPhrasing(node, {
|
|
37
|
+
after: marker,
|
|
38
|
+
before,
|
|
39
|
+
...tracker.current()
|
|
40
|
+
}));
|
|
41
|
+
const beforeBoundary = resolveBeforeBoundary(node, parent, state, info.before);
|
|
42
|
+
const afterBoundary = resolveAfterBoundary(node, parent, state, info.after);
|
|
43
|
+
const open = encodeInfoCjk(beforeBoundary, firstCodePoint(between), marker, "open");
|
|
44
|
+
if (open.inside && between) between = encodeFirstCodePoint(between);
|
|
45
|
+
const close = encodeInfoCjk({
|
|
46
|
+
current: lastCodePoint(between),
|
|
47
|
+
previous: codePointBeforeLast(between)
|
|
48
|
+
}, afterBoundary, marker, "close");
|
|
49
|
+
if (close.inside && between) between = encodeLastCodePoint(between);
|
|
50
|
+
const after = tracker.move(sequence);
|
|
51
|
+
exit();
|
|
52
|
+
const encodeAfterSupplementary = close.outside && shouldEncodeAfterSupplementaryText(parent, state);
|
|
53
|
+
state.attentionEncodeSurroundingInfo = {
|
|
54
|
+
after: close.outside && !encodeAfterSupplementary,
|
|
55
|
+
before: open.outside
|
|
56
|
+
};
|
|
57
|
+
getCjkFriendlyState(state).cjkFriendlyEncodeAfterSupplementaryText = encodeAfterSupplementary;
|
|
58
|
+
return before + between + after;
|
|
59
|
+
}
|
|
60
|
+
function text(node, _parent, state, info) {
|
|
61
|
+
const cjkFriendlyState = getCjkFriendlyState(state);
|
|
62
|
+
if (!cjkFriendlyState.cjkFriendlyEncodeAfterSupplementaryText) return state.safe(node.value, info);
|
|
63
|
+
cjkFriendlyState.cjkFriendlyEncodeAfterSupplementaryText = false;
|
|
64
|
+
const [first = ""] = [...node.value];
|
|
65
|
+
const rest = node.value.slice(first.length);
|
|
66
|
+
return `${encodeCharacterReference(first.codePointAt(0) ?? codes.eof)}${state.safe(rest, {
|
|
67
|
+
...info,
|
|
68
|
+
before: ";"
|
|
69
|
+
})}`;
|
|
70
|
+
}
|
|
71
|
+
function encodeInfoCjk(before, after, marker, target) {
|
|
72
|
+
const beforeKind = classifyBoundaryBefore(before);
|
|
73
|
+
const afterKind = classifyCharacter(after);
|
|
74
|
+
if (!isCjkOrIvs(beforeKind) && !isCjkOrIvs(afterKind)) return target === "open" ? encodeInfoFallback(beforeKind, afterKind, marker) : encodeInfoFallback(afterKind, beforeKind, marker);
|
|
75
|
+
const raw = {
|
|
76
|
+
inside: false,
|
|
77
|
+
outside: false
|
|
78
|
+
};
|
|
79
|
+
const preserveOutside = {
|
|
80
|
+
inside: true,
|
|
81
|
+
outside: false
|
|
82
|
+
};
|
|
83
|
+
const preserveInside = {
|
|
84
|
+
inside: false,
|
|
85
|
+
outside: true
|
|
86
|
+
};
|
|
87
|
+
const encodeBoth = {
|
|
88
|
+
inside: true,
|
|
89
|
+
outside: true
|
|
90
|
+
};
|
|
91
|
+
for (const candidate of [
|
|
92
|
+
raw,
|
|
93
|
+
preserveOutside,
|
|
94
|
+
preserveInside,
|
|
95
|
+
encodeBoth
|
|
96
|
+
]) {
|
|
97
|
+
const candidateBefore = target === "open" ? candidate.outside ? encodedBoundaryBeforeContext : before : candidate.inside ? encodedBoundaryBeforeContext : before;
|
|
98
|
+
const candidateAfter = target === "open" ? candidate.inside ? encodedBoundaryAfter : after : candidate.outside ? encodedBoundaryAfter : after;
|
|
99
|
+
if (target === "open" ? canOpen(marker, candidateBefore, candidateAfter) : canClose(marker, candidateBefore, candidateAfter)) return candidate;
|
|
100
|
+
}
|
|
101
|
+
return encodeBoth;
|
|
102
|
+
}
|
|
103
|
+
function encodeInfoFallback(outsideKind, insideKind, marker) {
|
|
104
|
+
if (isLetterLike(outsideKind)) return isLetterLike(insideKind) ? marker === "_" ? {
|
|
105
|
+
inside: true,
|
|
106
|
+
outside: true
|
|
107
|
+
} : {
|
|
108
|
+
inside: false,
|
|
109
|
+
outside: false
|
|
110
|
+
} : isUnicodeWhitespace(insideKind) ? {
|
|
111
|
+
inside: true,
|
|
112
|
+
outside: true
|
|
113
|
+
} : {
|
|
114
|
+
inside: false,
|
|
115
|
+
outside: true
|
|
116
|
+
};
|
|
117
|
+
if (isUnicodeWhitespace(outsideKind)) return isLetterLike(insideKind) ? {
|
|
118
|
+
inside: false,
|
|
119
|
+
outside: false
|
|
120
|
+
} : isUnicodeWhitespace(insideKind) ? {
|
|
121
|
+
inside: true,
|
|
122
|
+
outside: true
|
|
123
|
+
} : {
|
|
124
|
+
inside: false,
|
|
125
|
+
outside: false
|
|
126
|
+
};
|
|
127
|
+
return isUnicodeWhitespace(insideKind) ? {
|
|
128
|
+
inside: true,
|
|
129
|
+
outside: false
|
|
130
|
+
} : {
|
|
131
|
+
inside: false,
|
|
132
|
+
outside: false
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function canOpen(marker, before, afterCode) {
|
|
136
|
+
const beforeKind = classifyBoundaryBefore(before);
|
|
137
|
+
const { close, open } = getAttentionSides(beforeKind, classifyCharacter(afterCode));
|
|
138
|
+
return marker === "_" ? open && (isSpaceOrPunctuation(beforeKind) || !close) : open;
|
|
139
|
+
}
|
|
140
|
+
function canClose(marker, before, afterCode) {
|
|
141
|
+
const afterKind = classifyCharacter(afterCode);
|
|
142
|
+
const { close, open } = getAttentionSides(classifyBoundaryBefore(before), afterKind);
|
|
143
|
+
return marker === "_" ? close && (isSpaceOrPunctuation(afterKind) || !open) : close;
|
|
144
|
+
}
|
|
145
|
+
function getAttentionSides(beforeKind, afterKind) {
|
|
146
|
+
const beforeNonCjkPunctuation = isNonCjkPunctuation(beforeKind);
|
|
147
|
+
const beforeSpaceOrNonCjkPunctuation = beforeNonCjkPunctuation || isUnicodeWhitespace(beforeKind);
|
|
148
|
+
const afterNonCjkPunctuation = isNonCjkPunctuation(afterKind);
|
|
149
|
+
const afterSpaceOrNonCjkPunctuation = afterNonCjkPunctuation || isUnicodeWhitespace(afterKind);
|
|
150
|
+
return {
|
|
151
|
+
open: !afterSpaceOrNonCjkPunctuation || afterNonCjkPunctuation && (beforeSpaceOrNonCjkPunctuation || isCjkOrIvs(beforeKind)),
|
|
152
|
+
close: !beforeSpaceOrNonCjkPunctuation || beforeNonCjkPunctuation && (afterSpaceOrNonCjkPunctuation || isCjk(afterKind))
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function isLetterLike(kind) {
|
|
156
|
+
return !isUnicodeWhitespace(kind) && !isNonCjkPunctuation(kind);
|
|
157
|
+
}
|
|
158
|
+
function classifyBoundaryBefore(before) {
|
|
159
|
+
const kind = classifyCharacter(before.current);
|
|
160
|
+
return before.current === null || !isNonEmojiGeneralUseVS(kind) ? kind : classifyPrecedingCharacter(kind, () => before.previous, before.current);
|
|
161
|
+
}
|
|
162
|
+
function resolveBeforeBoundary(node, parent, state, fallback) {
|
|
163
|
+
let current = lastCodePoint(fallback);
|
|
164
|
+
let previous = codePointBeforeLast(fallback);
|
|
165
|
+
if (needsPreviousBoundaryRecovery(current) || needsPreviousContext(current, previous)) {
|
|
166
|
+
const siblingText = getAdjacentSiblingText(node, parent, state, -1);
|
|
167
|
+
if (siblingText) {
|
|
168
|
+
current = lastCodePoint(siblingText);
|
|
169
|
+
previous = codePointBeforeLast(siblingText);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
current,
|
|
174
|
+
previous
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function resolveAfterBoundary(node, parent, state, fallback) {
|
|
178
|
+
const current = firstCodePoint(fallback);
|
|
179
|
+
if (!needsNextBoundaryRecovery(current)) return current;
|
|
180
|
+
const siblingText = getAdjacentSiblingText(node, parent, state, 1);
|
|
181
|
+
return siblingText ? firstCodePoint(siblingText) : current;
|
|
182
|
+
}
|
|
183
|
+
function needsPreviousBoundaryRecovery(codePoint) {
|
|
184
|
+
return codePoint !== null && 56320 <= codePoint && codePoint <= 57343;
|
|
185
|
+
}
|
|
186
|
+
function needsNextBoundaryRecovery(codePoint) {
|
|
187
|
+
return codePoint !== null && 55296 <= codePoint && codePoint <= 56319;
|
|
188
|
+
}
|
|
189
|
+
function needsPreviousContext(current, previous) {
|
|
190
|
+
return isNonEmojiGeneralUseVS(classifyCharacter(current)) && (previous === null || previous === codes.eof);
|
|
191
|
+
}
|
|
192
|
+
function getAdjacentSiblingText(node, parent, state, offset) {
|
|
193
|
+
if (!parent) return;
|
|
194
|
+
const stackIndex = state.indexStack.at(-1);
|
|
195
|
+
const siblings = parent.children;
|
|
196
|
+
const index = typeof stackIndex === "number" ? stackIndex : siblings.indexOf(node);
|
|
197
|
+
if (index < 0) return;
|
|
198
|
+
const sibling = siblings[index + offset];
|
|
199
|
+
return getNodeTextContent(sibling) || void 0;
|
|
200
|
+
}
|
|
201
|
+
function shouldEncodeAfterSupplementaryText(parent, state) {
|
|
202
|
+
if (!parent) return false;
|
|
203
|
+
const stackIndex = state.indexStack.at(-1);
|
|
204
|
+
if (typeof stackIndex !== "number") return false;
|
|
205
|
+
const sibling = parent.children[stackIndex + 1];
|
|
206
|
+
return sibling?.type === "text" && (firstCodePoint(sibling.value) ?? 0) > 65535;
|
|
207
|
+
}
|
|
208
|
+
function getCjkFriendlyState(state) {
|
|
209
|
+
return state;
|
|
210
|
+
}
|
|
211
|
+
function getNodeTextContent(node) {
|
|
212
|
+
if (!node || typeof node !== "object") return "";
|
|
213
|
+
if ("value" in node && typeof node.value === "string") return node.value;
|
|
214
|
+
if ("alt" in node && typeof node.alt === "string") return node.alt;
|
|
215
|
+
if ("children" in node && Array.isArray(node.children)) return node.children.map(getNodeTextContent).join("");
|
|
216
|
+
return "";
|
|
217
|
+
}
|
|
218
|
+
function encodeCharacterReference(codePoint) {
|
|
219
|
+
return `&#x${(codePoint ?? 0).toString(16).toUpperCase()};`;
|
|
220
|
+
}
|
|
221
|
+
function encodeFirstCodePoint(value) {
|
|
222
|
+
const [first = ""] = [...value];
|
|
223
|
+
return encodeCharacterReference(first.codePointAt(0) ?? codes.eof) + value.slice(first.length);
|
|
224
|
+
}
|
|
225
|
+
function encodeLastCodePoint(value) {
|
|
226
|
+
const characters = [...value];
|
|
227
|
+
const last = characters.pop();
|
|
228
|
+
return `${characters.join("")}${encodeCharacterReference(last?.codePointAt(0) ?? codes.eof)}`;
|
|
229
|
+
}
|
|
230
|
+
function codePointBeforeLast(value) {
|
|
231
|
+
const characters = [...value];
|
|
232
|
+
characters.pop();
|
|
233
|
+
return characters.at(-1)?.codePointAt(0) ?? codes.eof;
|
|
234
|
+
}
|
|
235
|
+
function firstCodePoint(value) {
|
|
236
|
+
return value.codePointAt(0) ?? codes.eof;
|
|
237
|
+
}
|
|
238
|
+
function lastCodePoint(value) {
|
|
239
|
+
return [...value].at(-1)?.codePointAt(0) ?? codes.eof;
|
|
240
|
+
}
|
|
241
|
+
const encodedBoundaryBeforeContext = {
|
|
242
|
+
current: encodedOutsideBoundary,
|
|
243
|
+
previous: codes.eof
|
|
244
|
+
};
|
|
245
|
+
const encodedBoundaryAfter = encodedInsideBoundary;
|
|
246
|
+
|
|
247
|
+
//#endregion
|
|
248
|
+
export { cjkFriendlyToMarkdown };
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mdast-util-to-markdown-cjk-friendly",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
|
+
"default": "./dist/index.mjs"
|
|
9
|
+
},
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"url": "https://github.com/tats-u/markdown-cjk-friendly"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Tatsunori Uchino <tats.u@live.jp> (https://github.com/tats-u)",
|
|
24
|
+
"bugs": "https://github.com/tats-u/markdown-cjk-friendly/issues",
|
|
25
|
+
"keywords": [
|
|
26
|
+
"mdast-util",
|
|
27
|
+
"mdast",
|
|
28
|
+
"markdown",
|
|
29
|
+
"remark-stringify",
|
|
30
|
+
"japanese",
|
|
31
|
+
"chinese",
|
|
32
|
+
"korean",
|
|
33
|
+
"cjk"
|
|
34
|
+
],
|
|
35
|
+
"description": "mdast-util-to-markdown extension to make Markdown emphasis output more friendly with Chinese, Japanese, and Korean (CJK)",
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"mdast-util-to-markdown": "^2.1.2",
|
|
39
|
+
"micromark-util-symbol": "^2.0.1",
|
|
40
|
+
"micromark-extension-cjk-friendly-util": "3.0.1"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@types/mdast": "*"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"@types/mdast": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=18"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/mdast": "^4.0.4",
|
|
55
|
+
"mdast-util-from-markdown": "^2.0.3"
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsdown",
|
|
59
|
+
"build:lib": "tsdown",
|
|
60
|
+
"dev": "tsdown --watch --sourcemaps",
|
|
61
|
+
"dev:lib": "tsdown --watch --sourcemaps",
|
|
62
|
+
"test": "vitest run",
|
|
63
|
+
"test:lib": "vitest run",
|
|
64
|
+
"test:up": "vitest -u",
|
|
65
|
+
"test:watch": "vitest watch",
|
|
66
|
+
"test:lib:watch": "vitest watch",
|
|
67
|
+
"lint:type": "tsc --noEmit"
|
|
68
|
+
}
|
|
69
|
+
}
|