@cobapen/markdown 0.5.0 → 0.5.2
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/lib/cjk-break/cjk-break.d.ts +3 -0
- package/lib/cjk-break/cjk-break.js +61 -0
- package/lib/index.js +2 -2
- package/package.json +3 -3
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { getEAWOfCodePoint } from "meaw";
|
|
2
|
+
function is_surrogate(c1, c2) {
|
|
3
|
+
return c1 >= 0xD800 && c1 <= 0xDBFF && c2 >= 0xDC00 && c2 <= 0xDFFF;
|
|
4
|
+
}
|
|
5
|
+
function is_hangul(c) {
|
|
6
|
+
return /[\u1100-\u11FF\u302E\u302F\u3131-\u318E\u3200-\u321E\u3260-\u327E\uA960-\uA97C\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uFFA0-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]/.test(c);
|
|
7
|
+
}
|
|
8
|
+
function is_fwh(codepoint) {
|
|
9
|
+
const eaw = getEAWOfCodePoint(codepoint) || "A";
|
|
10
|
+
return /^[FWH]/.test(eaw);
|
|
11
|
+
}
|
|
12
|
+
function process_inlines(tokens) {
|
|
13
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
14
|
+
if (tokens[i].type !== "softbreak")
|
|
15
|
+
continue;
|
|
16
|
+
let last = " ";
|
|
17
|
+
let next = " ";
|
|
18
|
+
for (let j = i - 1; j >= 0; j--) {
|
|
19
|
+
if (tokens[j].type !== "text")
|
|
20
|
+
continue;
|
|
21
|
+
if (tokens[j].content.length === 0)
|
|
22
|
+
continue;
|
|
23
|
+
const c1 = tokens[j].content.charCodeAt(tokens[j].content.length - 2);
|
|
24
|
+
const c2 = tokens[j].content.charCodeAt(tokens[j].content.length - 1);
|
|
25
|
+
last = tokens[j].content.slice(is_surrogate(c1, c2) ? -2 : -1);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
for (let j = i + 1; j < tokens.length; j++) {
|
|
29
|
+
if (tokens[j].type !== "text")
|
|
30
|
+
continue;
|
|
31
|
+
if (tokens[j].content.length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
const c1 = tokens[j].content.charCodeAt(0);
|
|
34
|
+
const c2 = tokens[j].content.charCodeAt(1);
|
|
35
|
+
next = tokens[j].content.slice(0, is_surrogate(c1, c2) ? 2 : 1);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
let remove_break = false;
|
|
39
|
+
if (last === "\u200b" || next === "\u200b")
|
|
40
|
+
remove_break = true;
|
|
41
|
+
if (is_fwh(last.codePointAt(0)) && is_fwh(next.codePointAt(0))) {
|
|
42
|
+
if (!is_hangul(last) && !is_hangul(next))
|
|
43
|
+
remove_break = true;
|
|
44
|
+
}
|
|
45
|
+
if (remove_break) {
|
|
46
|
+
tokens[i].type = "text";
|
|
47
|
+
tokens[i].content = "";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function handler(state) {
|
|
52
|
+
for (let blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
|
|
53
|
+
if (state.tokens[blkIdx].type !== "inline")
|
|
54
|
+
continue;
|
|
55
|
+
process_inlines(state.tokens[blkIdx].children || []);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export const cjk_break = (md) => {
|
|
59
|
+
md.core.ruler.push("cjk_breaks", handler);
|
|
60
|
+
};
|
|
61
|
+
export default cjk_break;
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import markdownIt from "markdown-it";
|
|
2
2
|
import advTable from "markdown-it-adv-table";
|
|
3
3
|
import anchor from "markdown-it-anchor";
|
|
4
|
-
import cjkbreaks from "markdown-it-cjk-breaks";
|
|
5
4
|
import deflist from "markdown-it-deflist";
|
|
6
5
|
import footnote from "markdown-it-footnote";
|
|
7
6
|
import toc from "markdown-it-table-of-contents";
|
|
7
|
+
import { cjk_break } from "./cjk-break/cjk-break.js";
|
|
8
8
|
import { fence_custom } from "./code/fence-custom.js";
|
|
9
9
|
import { highlighterForMarkdownIt } from "./code/highlight.js";
|
|
10
10
|
import { replacelink } from "./link/replacelink.js";
|
|
@@ -44,7 +44,7 @@ export class CMarkdown {
|
|
|
44
44
|
setup(md) {
|
|
45
45
|
md.renderer.rules.fence = fence_custom;
|
|
46
46
|
md.use(anchor)
|
|
47
|
-
.use(
|
|
47
|
+
.use(cjk_break)
|
|
48
48
|
.use(footnote)
|
|
49
49
|
.use(deflist)
|
|
50
50
|
.use(replacelink)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobapen/markdown",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "A markdown converter for cobapen website",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown"
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"markdown-it": "^14.1.0",
|
|
43
43
|
"markdown-it-adv-table": "^0.1.2",
|
|
44
44
|
"markdown-it-anchor": "^9.2.0",
|
|
45
|
-
"markdown-it-cjk-breaks": "file:src/cjk-break",
|
|
46
45
|
"markdown-it-deflist": "^3.0.0",
|
|
47
46
|
"markdown-it-footnote": "^4.0.0",
|
|
48
|
-
"markdown-it-table-of-contents": "^1.1.0"
|
|
47
|
+
"markdown-it-table-of-contents": "^1.1.0",
|
|
48
|
+
"meaw": "^10.0.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@cobapen/eslint-config": "^0.5.0",
|