@celhive/tool.js 0.0.8 → 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.
@@ -3,4 +3,4 @@
3
3
  * @param md - 需要修复的 markdown 字符串
4
4
  * @returns string 修复后的 markdown 字符串
5
5
  */
6
- export declare const markdownPatch: (md: string) => string;
6
+ export declare const markdownPatch: (input: string) => string;
@@ -2,19 +2,23 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.markdownPatch = void 0;
4
4
  /**
5
- * 修复 markdown 格式缺失问题
6
- * @param md - 需要修复的 markdown 字符串
7
- * @returns string 修复后的 markdown 字符串
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 markdownPatch = (md) => {
13
+ const fixHeadingSpaces = (md) => {
10
14
  const lines = md.split('\n');
11
15
  let inCodeBlock = false;
12
- const mendTitle = lines
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
- const mendedTitle = mendTitle.trim();
37
- const mendBold = mendedTitle.replace(/\*\*(.*?)\*\*/g, (match, p1) => {
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
- // 写一个正则,如果 ```mermaid 前面只有一个换行符,就添加一个换行符
43
- const mendMermaid = mendBold.replace(/(\n)```mermaid/g, '$1\n```mermaid');
44
- // 在连续的 # 符号(1个以上)前面插入换行符,但不处理行首的 #
45
- const mendHashNewline = mendMermaid.replace(/([^\n#])(#{2,})/g, '$1\n$2');
46
- // 再次处理新行中的标题格式,确保 # 和内容之间有空格
47
- const lines2 = mendHashNewline.split('\n');
48
- let inCodeBlock2 = false;
49
- const result = lines2
50
- .map((line) => {
51
- // 检查是否是代码块的开始或结束
52
- if (line.trim().startsWith('```')) {
53
- inCodeBlock2 = !inCodeBlock2;
54
- return line;
55
- }
56
- // 如果在代码块内,跳过处理
57
- if (inCodeBlock2) {
58
- return line;
59
- }
60
- const match = line.match(/^(\s*)(#+)(\s*)(.*)$/);
61
- if (match) {
62
- const [, leadingSpaces, hashes, spaces, content] = match;
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
- exports.markdownPatch = markdownPatch;
77
+ /**
78
+ * 修复 markdown 格式缺失问题
79
+ * @param md - 需要修复的 markdown 字符串
80
+ * @returns string 修复后的 markdown 字符串
81
+ */
82
+ exports.markdownPatch = flow(fixHeadingSpaces, trimWhitespace, convertBoldToHtml, fixMermaidSpacing, addNewlineBeforeHeadings, addNewlineBeforeVideoTags, fixHeadingSpaces // 再次修复标题空格,处理新插入的换行符后的标题
83
+ );
@@ -1,8 +1,6 @@
1
1
  interface stableStreamingProps {
2
2
  /** Number of characters output per millisecond */
3
3
  ratePerMs: number;
4
- /** Callback invoked with each character */
5
- callback: (char: string) => void;
6
4
  }
7
5
  /**
8
6
  * StableStreaming class to stream characters at a stable rate.
@@ -14,7 +12,7 @@ export declare class StableStreaming {
14
12
  private timer;
15
13
  private pendingResolvers;
16
14
  constructor(options: stableStreamingProps);
17
- streaming(chars: string): Promise<void>;
15
+ streaming(chars: string, callback: (char: string) => void): Promise<void>;
18
16
  private start;
19
17
  }
20
18
  export {};
@@ -15,25 +15,25 @@ class StableStreaming {
15
15
  }
16
16
  this.options = options;
17
17
  }
18
- streaming(chars) {
18
+ streaming(chars, callback) {
19
19
  if (!chars)
20
20
  return Promise.resolve();
21
21
  this.queue.push(...chars.split(''));
22
22
  return new Promise((resolve) => {
23
23
  this.pendingResolvers.push(resolve);
24
24
  if (!this.timer) {
25
- this.start();
25
+ this.start(callback);
26
26
  }
27
27
  });
28
28
  }
29
- start() {
29
+ start(callback) {
30
30
  const charsPerTick = Math.max(1, Math.floor(this.options.ratePerMs));
31
31
  this.timer = setInterval(() => {
32
32
  let remaining = charsPerTick;
33
33
  while (remaining > 0 && this.queue.length) {
34
34
  const char = this.queue.shift();
35
35
  if (char !== undefined) {
36
- this.options.callback(char);
36
+ callback(char);
37
37
  }
38
38
  remaining -= 1;
39
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celhive/tool.js",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"