feihong-code 0.6.0 → 7.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.
@@ -107,6 +107,50 @@
107
107
  return html;
108
108
  }
109
109
 
110
+ // 纯文本渲染:只转义HTML、换行和链接,不转标题/列表/加粗/代码块等格式
111
+ // 用于对话流展示,让输出像普通人聊天一样清爽
112
+ function renderPlainText(text) {
113
+ let html = escapeHtml(stripMarkdown(text == null ? '' : text));
114
+ html = html.replace(/(https?:\/\/[^\s<>"'()]+)/g, '<a href="$1" style="color:var(--brand,#4f6ef7);text-decoration:underline;" target="_blank">$1</a>');
115
+ html = html.replace(/\n/g, '<br>');
116
+ return html;
117
+ }
118
+
119
+ // 把 Markdown 格式符号全部去掉,只保留纯文本内容
120
+ function stripMarkdown(text) {
121
+ let s = String(text);
122
+ // 去掉代码块 ```...``` 里的标记,保留内容
123
+ s = s.replace(/```[\w]*\n?([\s\S]*?)```/g, '$1');
124
+ // 去掉行内代码 `code` 的反引号,保留内容
125
+ s = s.replace(/`([^`]+)`/g, '$1');
126
+ // 去掉加粗 **text** 和 __text__
127
+ s = s.replace(/\*\*([^*]+)\*\*/g, '$1');
128
+ s = s.replace(/__([^_]+)__/g, '$1');
129
+ // 去掉斜体 *text* 和 _text_(注意不要和列表冲突)
130
+ s = s.replace(/\*([^*\n]+)\*/g, '$1');
131
+ s = s.replace(/_([^_\n]+)_/g, '$1');
132
+ // 去掉标题 # ## ### 等,保留标题文字
133
+ s = s.replace(/^#{1,6}\s+/gm, '');
134
+ // 去掉无序列表符号 * - + (行首)
135
+ s = s.replace(/^[\s]*[-*+]\s+/gm, '');
136
+ // 去掉有序列表符号 1. 2. 等(行首)
137
+ s = s.replace(/^[\s]*\d+\.\s+/gm, '');
138
+ // 去掉引用符号 > (行首)
139
+ s = s.replace(/^[\s]*>\s?/gm, '');
140
+ // 去掉链接 [text](url) 只保留 text
141
+ s = s.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
142
+ // 去掉图片 ![alt](url) 只保留 alt
143
+ s = s.replace(/!\[([^\]]*)\]\([^)]+\)/g, '$1');
144
+ // 去掉分割线 --- *** ___
145
+ s = s.replace(/^[-*_]{3,}\s*$/gm, '');
146
+ // 去掉行号引用 L123、ln 456、第789行这种
147
+ s = s.replace(/\b[Ll][nN]?\s*\d+(-\d+)?/g, '');
148
+ s = s.replace(/第\d+行/g, '');
149
+ // 去掉多余的空行(连续3个以上换行变成2个)
150
+ s = s.replace(/\n{3,}/g, '\n\n');
151
+ return s.trim();
152
+ }
153
+
110
154
  function statusBadge(s) { return '<span class="badge ' + s + '">' + s + '</span>'; }
111
155
 
112
156
  function closeModal(id) { document.getElementById(id).classList.remove('show'); }