@lobehub/ui 2.1.7 → 2.1.9

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.
@@ -22,6 +22,16 @@ export declare function escapeMhchemCommands(text: string): string;
22
22
  * @returns The string with pipe characters escaped in LaTeX expressions
23
23
  */
24
24
  export declare function escapeLatexPipes(text: string): string;
25
+ /**
26
+ * Escapes underscores within \text{...} commands in LaTeX expressions.
27
+ * For example, \text{node_domain} becomes \text{node\_domain}.
28
+ * This function processes the content of \text{} blocks to replace all
29
+ * occurrences of '_' with '\_'.
30
+ *
31
+ * @param text The input string potentially containing LaTeX expressions
32
+ * @returns The string with underscores escaped within \text{...} commands
33
+ */
34
+ export declare function escapeTextUnderscores(text: string): string;
25
35
  /**
26
36
  * Preprocesses LaTeX content by performing multiple operations:
27
37
  * 1. Protects code blocks from processing
@@ -45,6 +45,27 @@ export function escapeLatexPipes(text) {
45
45
  return text;
46
46
  }
47
47
 
48
+ /**
49
+ * Escapes underscores within \text{...} commands in LaTeX expressions.
50
+ * For example, \text{node_domain} becomes \text{node\_domain}.
51
+ * This function processes the content of \text{} blocks to replace all
52
+ * occurrences of '_' with '\_'.
53
+ *
54
+ * @param text The input string potentially containing LaTeX expressions
55
+ * @returns The string with underscores escaped within \text{...} commands
56
+ */
57
+ export function escapeTextUnderscores(text) {
58
+ // This regular expression finds all \text{...} blocks.
59
+ // For each match, it takes the content inside the braces (the first capture group),
60
+ // replaces all underscores '_' with '\_', and then reconstructs the \text{...} command.
61
+ return text.replaceAll(/\\text{([^}]*)}/g, function (match, textContent) {
62
+ // match is the full matched string, e.g., "\text{node_domain}"
63
+ // textContent is the content within the braces, e.g., "node_domain"
64
+ var escapedTextContent = textContent.replaceAll('_', '\\_');
65
+ return "\\text{".concat(escapedTextContent, "}");
66
+ });
67
+ }
68
+
48
69
  /**
49
70
  * Preprocesses LaTeX content by performing multiple operations:
50
71
  * 1. Protects code blocks from processing
@@ -58,36 +79,41 @@ export function escapeLatexPipes(text) {
58
79
  */
59
80
  export function preprocessLaTeX(str) {
60
81
  // Step 1: Protect code blocks
61
- var codeBlocks = [];
62
- var content = str.replaceAll(/(```[\S\s]*?```|`[^\n`]+`)/g, function (match, code) {
63
- codeBlocks.push(code);
64
- return "<<CODE_BLOCK_".concat(codeBlocks.length - 1, ">>");
65
- });
82
+ // const codeBlocks: string[] = [];
83
+ // let content = str.replaceAll(/(```[\S\s]*?```|`[^\n`]+`)/g, (match, code) => {
84
+ // codeBlocks.push(code);
85
+ // return `<<CODE_BLOCK_${codeBlocks.length - 1}>>`;
86
+ // });
66
87
 
67
- // Step 2: Protect existing LaTeX expressions
68
- var latexExpressions = [];
69
- content = content.replaceAll(/(\$\$[\S\s]*?\$\$|\\\[[\S\s]*?\\]|\\\(.*?\\\))/g, function (match) {
70
- latexExpressions.push(match);
71
- return "<<LATEX_".concat(latexExpressions.length - 1, ">>");
72
- });
88
+ // // Step 2: Protect existing LaTeX expressions
89
+ // const latexExpressions: string[] = [];
90
+ // content = content.replaceAll(/(\$\$[\S\s]*?\$\$|\\\[[\S\s]*?\\]|\\\(.*?\\\))/g, (match) => {
91
+ // latexExpressions.push(match);
92
+ // return `<<LATEX_${latexExpressions.length - 1}>>`;
93
+ // });
73
94
 
74
95
  // Step 3: Escape dollar signs that are likely currency indicators
75
- content = content.replaceAll(/\$(?=\d)/g, '\\$');
96
+ // Deprecated, as it causes parsing errors for formulas starting with a number, such as `$1$`
97
+ // content = content.replaceAll(/\$(?=\d)/g, '\\$');
76
98
 
77
99
  // Step 4: Restore LaTeX expressions
78
- content = content.replaceAll(/<<LATEX_(\d+)>>/g, function (_, index) {
79
- return latexExpressions[Number.parseInt(index)];
80
- });
100
+ // content = content.replaceAll(
101
+ // /<<LATEX_(\d+)>>/g,
102
+ // (_, index) => latexExpressions[Number.parseInt(index)],
103
+ // );
81
104
 
82
- // Step 5: Restore code blocks
83
- content = content.replaceAll(/<<CODE_BLOCK_(\d+)>>/g, function (_, index) {
84
- return codeBlocks[Number.parseInt(index)];
85
- });
105
+ // // Step 5: Restore code blocks
106
+ // content = content.replaceAll(
107
+ // /<<CODE_BLOCK_(\d+)>>/g,
108
+ // (_, index) => codeBlocks[Number.parseInt(index)],
109
+ // );
110
+ var content = str;
86
111
 
87
112
  // Step 6: Apply additional escaping functions
88
113
  content = convertLatexDelimiters(content);
89
114
  content = escapeMhchemCommands(content);
90
115
  content = escapeLatexPipes(content);
116
+ content = escapeTextUnderscores(content);
91
117
  return content;
92
118
  }
93
119
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/ui",
3
- "version": "2.1.7",
3
+ "version": "2.1.9",
4
4
  "description": "Lobe UI is an open-source UI component library for building AIGC web apps",
5
5
  "keywords": [
6
6
  "lobehub",