ai-read-over-mobile 0.0.12 → 0.0.14

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.
@@ -51,6 +51,7 @@ import ChatTools from "./chat-tools.vue";
51
51
  import '../static/iconfont/iconfont.css';
52
52
  import katex from 'katex';
53
53
  import 'katex/dist/katex.min.css';
54
+ import 'katex/contrib/mhchem/mhchem.js';
54
55
 
55
56
  export default {
56
57
  name: 'ReadOver',
@@ -90,49 +91,59 @@ export default {
90
91
  },
91
92
  renderFormulas(text) {
92
93
  if (!text) return '';
93
-
94
- // 如果不是字符串,转字符串
95
- if (typeof text === 'number') {
96
- return text;
94
+ text = text.replace(/>/g, '>')
95
+ .replace(/&lt;/g, '<')
96
+ .replace(/&amp;/g, '&')
97
+ .replace(/&quot;/g, '"')
98
+ .replace(/&#39;/g, "'");
99
+ // 如果整个文本包含 LaTeX 命令且没有定界符,整体包裹
100
+ const hasDelimiter = /\$|\\\(|\\\[/.test(text);
101
+ if (!hasDelimiter) {
102
+ text = text.replace(
103
+ /(\\(?:frac|sqrt|sum|int|cdot|times|geq|leq|neq|pm|div|alpha|beta|gamma|theta|pi|sigma|omega|infty|log|sin|cos|tan|left|right|overline|hat|vec|bar|lim|in|notin|supset|subseteq|ln|Gamma|to)(?:\{[^{}]*(?:\{[^{}]*(?:\{[^{}]*\}[^{}]*)*\}[^{}]*)*\}|[^\s{},。?!、;:\u4e00-\u9fa5])*)/g,
104
+ (match) => `\\(${match}\\)`
105
+ );
97
106
  }
98
- const {renderToString} = katex;
107
+ const { renderToString } = katex;
99
108
  return text
100
- .replace(/\$\$(.*?)\$\$/gs, (match) => {
109
+ // $$ ... $$ 块级公式
110
+ .replace(/\$\$([\s\S]*?)\$\$/gs, function (_, formula) {
101
111
  try {
102
- // 渲染块级公式
103
- return renderToString(match.slice(2, -2), {displayMode: false});
112
+ const trimmedFormula = formula.trim()
113
+ return renderToString(trimmedFormula, { displayMode: true });
104
114
  } catch (error) {
105
115
  console.error('Error rendering block formula:', error);
106
- return match;
116
+ return _;
107
117
  }
108
118
  })
109
- .replace(/\$.*?\$/g, (match) => {
110
- match = match.replace(/<br>/g, '');
119
+ // \(...\) 行内公式
120
+ .replace(/\\\((.*?)\\\)/gs, function (_, formula) {
111
121
  try {
112
- // 渲染行内公式
113
- return renderToString(match.slice(1, -1), {displayMode: false});
122
+ const trimmedFormula = formula.trim()
123
+ return renderToString(trimmedFormula, { displayMode: false });
114
124
  } catch (error) {
115
125
  console.error('Error rendering inline formula:', error);
116
- return match;
126
+ return _;
117
127
  }
118
128
  })
119
- .replace(/\\\(.*?\\\)/g, (match) => {
129
+ // \[...\] 块级公式
130
+ .replace(/\\\[(.*?)\\\]/gs, function (_, formula) {
120
131
  try {
121
- // 渲染行内公式
122
- return renderToString(match.slice(2, -2), {displayMode: false});
132
+ const trimmedFormula = formula.trim()
133
+ return renderToString(trimmedFormula, { displayMode: true });
123
134
  } catch (error) {
124
- console.error('Error rendering inline formula:', error);
125
- return match;
135
+ console.error('Error rendering block formula:', error);
136
+ return _;
126
137
  }
127
138
  })
128
- .replace(/\\\[[\s\S]*?\\\]/g, (match) => {
129
- match = match.replace(/<br>/g, '');
139
+ // $...$ 行内公式
140
+ .replace(/\$(.+?)\$/gs, function (_, formula) {
130
141
  try {
131
- // 渲染块级公式
132
- return renderToString(match.slice(2, -2), {displayMode: false});
142
+ const trimmedFormula = formula.trim()
143
+ return renderToString(trimmedFormula, { displayMode: false });
133
144
  } catch (error) {
134
- console.error('Error rendering block formula:', error);
135
- return match;
145
+ console.error('Error rendering inline formula with $...$:', error);
146
+ return _;
136
147
  }
137
148
  });
138
149
  },