@celhive/tool.js 0.0.9 → 0.0.10
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/markdown-patch.d.ts +1 -1
- package/lib/markdown-patch.js +49 -40
- package/package.json +1 -1
package/lib/markdown-patch.d.ts
CHANGED
package/lib/markdown-patch.js
CHANGED
|
@@ -2,19 +2,23 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.markdownPatch = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
* 组合多个函数,从左到右依次执行,类似 lodash 的 flow
|
|
6
|
+
*/
|
|
7
|
+
const flow = (...fns) => {
|
|
8
|
+
return (input) => fns.reduce((acc, fn) => fn(acc), input);
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* 在标题 # 和内容之间添加空格(跳过代码块)
|
|
8
12
|
*/
|
|
9
|
-
const
|
|
13
|
+
const fixHeadingSpaces = (md) => {
|
|
10
14
|
const lines = md.split('\n');
|
|
11
15
|
let inCodeBlock = false;
|
|
12
|
-
|
|
16
|
+
return lines
|
|
13
17
|
.map((line) => {
|
|
14
18
|
// 检查是否是代码块的开始或结束
|
|
15
19
|
if (line.trim().startsWith('```')) {
|
|
16
|
-
inCodeBlock = !inCodeBlock;
|
|
17
|
-
return line;
|
|
20
|
+
inCodeBlock = !inCodeBlock;
|
|
21
|
+
return line;
|
|
18
22
|
}
|
|
19
23
|
// 如果在代码块内,跳过处理
|
|
20
24
|
if (inCodeBlock) {
|
|
@@ -33,42 +37,47 @@ const markdownPatch = (md) => {
|
|
|
33
37
|
return line;
|
|
34
38
|
})
|
|
35
39
|
.join('\n');
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* 去除首尾空白字符
|
|
43
|
+
*/
|
|
44
|
+
const trimWhitespace = (md) => md.trim();
|
|
45
|
+
/**
|
|
46
|
+
* 将包含中文符号的粗体文本转换为 HTML bold 标签
|
|
47
|
+
*/
|
|
48
|
+
const convertBoldToHtml = (md) => {
|
|
49
|
+
return md.replace(/\*\*(.*?)\*\*/g, (match, p1) => {
|
|
38
50
|
return (p1.includes('(') && p1.includes(')')) || p1.includes(':')
|
|
39
51
|
? `<b>${p1}</b>`
|
|
40
52
|
: match;
|
|
41
53
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (hashes.length > 0 &&
|
|
64
|
-
spaces.length === 0 &&
|
|
65
|
-
!content.startsWith('#')) {
|
|
66
|
-
return `${leadingSpaces}${hashes} ${content}`;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return line;
|
|
70
|
-
})
|
|
71
|
-
.join('\n');
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* 在 mermaid 代码块前添加额外换行符
|
|
57
|
+
*/
|
|
58
|
+
const fixMermaidSpacing = (md) => {
|
|
59
|
+
return md.replace(/(\n)```mermaid/g, '$1\n```mermaid');
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* 在连续的 # 符号前添加换行符(不处理行首的 #)
|
|
63
|
+
*/
|
|
64
|
+
const addNewlineBeforeHeadings = (md) => {
|
|
65
|
+
return md.replace(/([^\n#])(#{2,})/g, '$1\n$2');
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* 确保 <video> 标签前至少有 2 个换行符(空行分隔)
|
|
69
|
+
*/
|
|
70
|
+
const addNewlineBeforeVideoTags = (md) => {
|
|
71
|
+
// 先处理没有换行符的情况:text<video> -> text\n\n<video>
|
|
72
|
+
let result = md.replace(/([^\n])(<video\b[^>]*>)/g, '$1\n\n$2');
|
|
73
|
+
// 再处理只有一个换行符的情况:text\n<video> -> text\n\n<video>
|
|
74
|
+
result = result.replace(/([^\n])\n(<video\b[^>]*>)/g, '$1\n\n$2');
|
|
72
75
|
return result;
|
|
73
76
|
};
|
|
74
|
-
|
|
77
|
+
/**
|
|
78
|
+
* 修复 markdown 格式缺失问题
|
|
79
|
+
* @param md - 需要修复的 markdown 字符串
|
|
80
|
+
* @returns string 修复后的 markdown 字符串
|
|
81
|
+
*/
|
|
82
|
+
exports.markdownPatch = flow(fixHeadingSpaces, trimWhitespace, convertBoldToHtml, fixMermaidSpacing, addNewlineBeforeHeadings, addNewlineBeforeVideoTags, fixHeadingSpaces // 再次修复标题空格,处理新插入的换行符后的标题
|
|
83
|
+
);
|