Skip to content

Commit

Permalink
fix: code render issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinko committed Nov 23, 2024
1 parent 5fa3f3a commit 9ecef4e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/components/Common/Editor/editorPlugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ export const ProcessCodeBlocks = (content: string): string => {
if (!content) return '';
const codeBlockRegex = /```(?:(\w*)\n)?([\s\S]*?)```/g;

const htmlRegex = /(<[^>]+>)/gi;
try {
return content.replace(codeBlockRegex, (match, language, code) => {
if (!language || !(language in codeBlockLanguages)) {
return '```plain\n' + code.trim() + '\n```';
}
return '```' + language + '\n' + code.trim() + '\n```';
}).replace(htmlRegex, '\\$1');
} catch (error) {

return content
let lastIndex = 0;
let result = '';
let match;

while ((match = codeBlockRegex.exec(content)) !== null) {
const beforeCode = content.slice(lastIndex, match.index);
result += beforeCode.replace(/(<[^>]+>)/gi, '\\$1');

const [_, language, code] = match;
if (!language || !(language in codeBlockLanguages)) {
result += '```plain\n' + code.trim() + '\n```';
} else {
result += '```' + language + '\n' + code.trim() + '\n```';
}

lastIndex = codeBlockRegex.lastIndex;
}

const afterLastCode = content.slice(lastIndex);
result += afterLastCode.replace(/(<[^>]+>)/gi, '\\$1');

return result;
};

export const MyPlugins = [
Expand Down

0 comments on commit 9ecef4e

Please sign in to comment.