fix: md table render

This commit is contained in:
Han Xiao 2025-03-20 16:32:18 +08:00
parent 76ee13f515
commit e3a7a4c393

View File

@ -375,22 +375,38 @@ export function fixCodeBlockIndentation(markdownText: string): string {
const openingBlock = codeBlockStack[codeBlockStack.length - 1]; const openingBlock = codeBlockStack[codeBlockStack.length - 1];
if (line.trim().length > 0) { if (line.trim().length > 0) {
// For non-empty lines // Calculate proper base indentation for the code block
const trimmedLine = line.trimStart(); let baseIndent;
// If we're in a list context, maintain proper indentation
if (openingBlock.listIndent) { if (openingBlock.listIndent) {
// For code blocks in lists, we need to preserve the list indentation plus the code fence indentation // For code blocks in lists
// The total indentation should be at least listIndent + some standard indentation (usually 4 spaces) baseIndent = openingBlock.listIndent + " ";
const codeIndent = openingBlock.indent.length > openingBlock.listIndent.length ?
openingBlock.indent :
openingBlock.listIndent + " ";
result.push(`${codeIndent}${trimmedLine}`);
} else { } else {
// Not in a list, use the opening fence indentation // Not in a list
result.push(`${openingBlock.indent}${trimmedLine}`); baseIndent = openingBlock.indent;
} }
// Get the indentation of this specific line
const lineIndentMatch = line.match(/^(\s*)/);
const lineIndent = lineIndentMatch ? lineIndentMatch[0] : '';
// Find the common prefix between the line's indent and the opening block's indent
// This represents the part of the indentation that's due to the markdown structure
let commonPrefix = '';
const minLength = Math.min(lineIndent.length, openingBlock.indent.length);
for (let i = 0; i < minLength; i++) {
if (lineIndent[i] === openingBlock.indent[i]) {
commonPrefix += lineIndent[i];
} else {
break;
}
}
// Remove just the common prefix (markdown structure indentation)
// and keep the rest (code's own indentation)
const contentAfterCommonIndent = line.substring(commonPrefix.length);
// Add the proper base indentation plus the preserved code indentation
result.push(`${baseIndent}${contentAfterCommonIndent}`);
} else { } else {
// For empty lines, just keep them as is // For empty lines, just keep them as is
result.push(line); result.push(line);