@ww_nero/mini-cli 1.0.86 → 1.0.87

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ww_nero/mini-cli",
3
- "version": "1.0.86",
3
+ "version": "1.0.87",
4
4
  "description": "极简的 AI 命令行助手",
5
5
  "bin": {
6
6
  "mini": "bin/mini.js"
@@ -1,21 +1,64 @@
1
1
  const chalk = require('chalk');
2
- const { marked } = require('marked');
2
+ const { Marked } = require('marked');
3
3
  const { markedTerminal } = require('marked-terminal');
4
4
  const { splitThinkContent, summarizeReasoning } = require('./think');
5
5
 
6
- // 配置markdown终端渲染器
7
- const renderer = markedTerminal({
6
+ const ANSI_PATTERN = /\u001B\[[0-9;]*m/g;
7
+
8
+ const createUnderlineFormatter = (char) => (text = '') => {
9
+ const plainText = text.replace(ANSI_PATTERN, '').trim();
10
+ if (!plainText) {
11
+ return text;
12
+ }
13
+ return `${text}\n${char.repeat(Math.max(plainText.length, 3))}`;
14
+ };
15
+
16
+ const prefixLines = (prefix, text = '') => text
17
+ .split('\n')
18
+ .map((line) => (line ? `${prefix}${line}` : line))
19
+ .join('\n');
20
+
21
+ const createMarkdownParser = (options) => {
22
+ const parser = new Marked();
23
+ parser.use(markedTerminal(options));
24
+ return parser;
25
+ };
26
+
27
+ const coloredMarkdownParser = createMarkdownParser({
28
+ showSectionPrefix: false,
8
29
  heading: chalk.cyan.bold,
9
- firstHeading: chalk.cyan.bold,
30
+ firstHeading: chalk.cyan.bold.underline,
10
31
  strong: chalk.bold,
11
32
  em: chalk.italic,
12
33
  blockquote: chalk.gray,
13
34
  code: chalk.yellow,
14
35
  codespan: chalk.yellow,
15
36
  table: chalk.gray,
37
+ link: chalk.blue,
16
38
  href: chalk.blue.underline
17
39
  });
18
- marked.use(renderer);
40
+
41
+ const plainMarkdownParser = createMarkdownParser({
42
+ showSectionPrefix: false,
43
+ firstHeading: createUnderlineFormatter('='),
44
+ heading: createUnderlineFormatter('-'),
45
+ blockquote: (text) => prefixLines('> ', text.replace(/^ {4}/gm, '')),
46
+ codespan: (text) => `\`${text}\``,
47
+ link: (text) => text,
48
+ href: (text) => text
49
+ });
50
+
51
+ const shouldUseColoredMarkdown = () => Boolean(
52
+ process.stdout &&
53
+ process.stdout.isTTY &&
54
+ chalk.supportsColor &&
55
+ chalk.supportsColor.level > 0
56
+ );
57
+
58
+ const renderMarkdownToTerminal = (text) => {
59
+ const parser = shouldUseColoredMarkdown() ? coloredMarkdownParser : plainMarkdownParser;
60
+ return parser.parse(text);
61
+ };
19
62
 
20
63
  const mergeReasoningContent = (...segments) => {
21
64
  const normalized = segments
@@ -38,8 +81,7 @@ const printAssistantContent = (text, ensureNewline) => {
38
81
  return;
39
82
  }
40
83
  ensureNewline();
41
- // 使用marked渲染markdown,生成带ANSI样式的终端输出
42
- const output = marked(text);
84
+ const output = renderMarkdownToTerminal(text);
43
85
  process.stdout.write(output);
44
86
  if (!text.endsWith('\n')) {
45
87
  process.stdout.write('\n');