fix(frontend): add null checks for markdown code block text parameter
Prevents TypeError when rendering empty code blocks in markdown documents. Adds validation in doc-content.vue and document-preview.vue renderer.code.
This commit is contained in:
@@ -186,6 +186,11 @@ renderer.image = function ({href, title, text}) {
|
||||
|
||||
// 自定义代码块渲染器,只显示语言标签
|
||||
renderer.code = function ({text, lang}) {
|
||||
// 空值校验:防止 text 为 undefined 或 null
|
||||
if (!text || typeof text !== 'string') {
|
||||
text = '';
|
||||
}
|
||||
|
||||
// Mermaid 图表处理
|
||||
if (lang === 'mermaid') {
|
||||
// 生成唯一ID
|
||||
|
||||
@@ -192,12 +192,24 @@ async function renderText(blob: Blob, fileType: string) {
|
||||
async function renderMarkdown(blob: Blob) {
|
||||
const { marked } = await import('marked');
|
||||
const text = await blob.text();
|
||||
|
||||
// 校验文本内容是否有效
|
||||
if (!text || typeof text !== 'string') {
|
||||
markdownHtml.value = '<p style="color: var(--td-text-color-disabled); text-align: center; padding: 20px;">文档内容为空</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
marked.use({
|
||||
breaks: true,
|
||||
gfm: true,
|
||||
});
|
||||
const renderer = new marked.Renderer();
|
||||
renderer.code = function ({text, lang}) {
|
||||
// 空值校验:防止 text 为 undefined 或 null
|
||||
if (!text || typeof text !== 'string') {
|
||||
text = '';
|
||||
}
|
||||
|
||||
let highlighted = '';
|
||||
if (lang && hljs.getLanguage(lang)) {
|
||||
try { highlighted = hljs.highlight(text, { language: lang }).value; }
|
||||
|
||||
Reference in New Issue
Block a user