格式化markdown内容

renderMarkdown(content) {
      if (!content) return '';
      
      // 预处理内容,修复符号与文字之间缺少空格的问题
      let processedContent = content
        // 修复标题符号后缺少空格的问题:# -> # 
        .replace(/(#{1,6})([^\s#])/g, '$1 $2')
        // 修复列表符号后缺少空格的问题:- -> - 
        .replace(/^(\s*[-*+])([^\s])/gm, '$1 $2')
        // 修复有序列表符号后缺少空格的问题:1. -> 1. 
        .replace(/^(\s*\d+\.)([^\s])/gm, '$1 $2')
        // 修复引用符号后缺少空格的问题:> -> > 
        .replace(/^(\s*>+)([^\s>])/gm, '$1 $2')
        // 修复粗体符号与文字之间的空格:**text** -> **text**
        .replace(/(\*\*)([^\s*])/g, '$1$2')
        .replace(/([^\s*])(\*\*)/g, '$1$2')
        // 修复斜体符号与文字之间的空格:*text* -> *text*
        .replace(/(?<!\*)(\*)([^\s*])/g, '$1$2')
        .replace(/([^\s*])(\*)(?!\*)/g, '$1$2')
        // 修复代码符号与文字之间的空格:`code` -> `code`
        .replace(/(`)([^\s`])/g, '$1$2')
        .replace(/([^\s`])(`)/g, '$1$2');
      
      return md.render(processedContent);
    }

 

posted @ 2026-01-29 20:04  青云码上  阅读(1)  评论(0)    收藏  举报