Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit f2626cd

Browse files
committed
refactor: improve markdown code block handling
- Rename fences to langtags for better clarity - Fix language tag handling for code blocks - Add tmux to recognized tools - Fix write operation detection for language tags with arguments
1 parent 4f251bb commit f2626cd

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

src/utils/markdownUtils.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ marked.use(
2121

2222
export function processNestedCodeBlocks(content: string) {
2323
if (content.split('```').length < 3) {
24-
return { processedContent: content, fences: [] };
24+
return { processedContent: content, langtags: [] };
2525
}
2626

2727
const lines = content.split('\n');
2828
const stack: string[] = [];
2929
let result = '';
3030
let currentBlock: string[] = [];
31-
const fences: string[] = [];
31+
const langtags: string[] = [];
3232

3333
for (const line of lines) {
3434
const strippedLine = line.trim();
3535
if (strippedLine.startsWith('```')) {
3636
const lang = strippedLine.slice(3);
37+
langtags.push(lang);
3738
if (stack.length === 0) {
3839
const remainingContent = lines.slice(lines.indexOf(line) + 1).join('\n');
3940
if (remainingContent.includes('```') && remainingContent.split('```').length > 2) {
4041
stack.push(lang);
41-
fences.push(lang);
4242
result += '~~~' + lang + '\n';
4343
} else {
4444
result += line + '\n';
@@ -64,7 +64,7 @@ export function processNestedCodeBlocks(content: string) {
6464

6565
return {
6666
processedContent: result.trim(),
67-
fences
67+
langtags
6868
};
6969
}
7070

@@ -82,7 +82,7 @@ export function transformThinkingTags(content: string) {
8282

8383
export function parseMarkdownContent(content: string) {
8484
const processedContent = transformThinkingTags(content);
85-
const { processedContent: transformedContent, fences } = processNestedCodeBlocks(processedContent);
85+
const { processedContent: transformedContent, langtags } = processNestedCodeBlocks(processedContent);
8686

8787
let parsedResult = marked.parse(transformedContent, {
8888
async: false,
@@ -91,13 +91,12 @@ export function parseMarkdownContent(content: string) {
9191
parsedResult = parsedResult.replace(
9292
/<pre><code(?:\s+class="([^"]+)")?>([^]*?)<\/code><\/pre>/g,
9393
(_, classes = "", code) => {
94-
const langtag = ((classes || "").split(" ")[1] || "Code").replace("language-", "");
95-
const args = fences?.shift() || "";
96-
94+
const langtag_fallback = ((classes || "").split(" ")[1] || "Code").replace("language-", "");
95+
const langtag = langtags?.shift() || langtag_fallback;
9796
const emoji = getCodeBlockEmoji(langtag);
9897
return `
9998
<details>
100-
<summary>${emoji} ${args || langtag}</summary>
99+
<summary>${emoji} ${langtag}</summary>
101100
<pre><code class="${classes}">${code}</code></pre>
102101
</details>
103102
`;
@@ -120,13 +119,13 @@ function isPath(langtag: string): boolean {
120119
}
121120

122121
function isTool(langtag: string): boolean {
123-
return ["ipython", "shell"].includes(langtag.split(" ")[0]);
122+
return ["ipython", "shell", "tmux"].includes(langtag.split(" ")[0].toLowerCase());
124123
}
125124

126125
function isOutput(langtag: string): boolean {
127126
return ["stdout", "stderr", "result"].includes(langtag.toLowerCase());
128127
}
129128

130129
function isWrite(langtag: string): boolean {
131-
return ["save", "patch", "append"].includes(langtag.toLowerCase());
130+
return ["save", "patch", "append"].includes(langtag.split(" ")[0].toLowerCase());
132131
}

0 commit comments

Comments
 (0)