feat: 表格渲染样式重新(完整版)、项目代码清理和优化

This commit is contained in:
lixinran 2025-09-10 18:35:29 +08:00
parent 53a24bfd09
commit 25abc09cb4
5 changed files with 124 additions and 2 deletions

Binary file not shown.

View File

@ -38,7 +38,7 @@ const options: Options = {
if (!text) return '' if (!text) return ''
if (!obj.useMd) return text if (!obj.useMd) return text
try { try {
// Configure marked renderer to add target="_blank" to links // Configure marked renderer to add target="_blank" to links and table classes
const renderer = { const renderer = {
link(token: Tokens.Link) { link(token: Tokens.Link) {
const href = token.href || '' const href = token.href || ''
@ -46,6 +46,13 @@ const options: Options = {
const text = token.text || '' const text = token.text || ''
return `<a href="${href}"${title} target="_blank">${text}</a>` return `<a href="${href}"${title} target="_blank">${text}</a>`
}, },
table(token: Tokens.Table) {
const header = token.header.map(cell => `<th>${cell.text}</th>`).join('')
const rows = token.rows.map(row =>
`<tr>${row.map(cell => `<td>${cell.text}</td>`).join('')}</tr>`
).join('')
return `<table class="markdown-table"><thead><tr>${header}</tr></thead><tbody>${rows}</tbody></table>`
},
} }
marked.use({ renderer, gfm: true }) marked.use({ renderer, gfm: true })

View File

@ -317,4 +317,58 @@
background: #4f90f22d; background: #4f90f22d;
border: 1px solid #4f90f2; border: 1px solid #4f90f2;
} }
/* Markdown表格样式 */
.markdown-table {
border-collapse: collapse;
width: 100%;
margin: 4px 0;
font-size: 11px;
border: 1px solid #e0e0e0;
border-radius: 6px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
background-color: #fafafa;
overflow: hidden;
white-space: normal !important; /* 覆盖MindElixir的pre-wrap */
}
.markdown-table th,
.markdown-table td {
border: 1px solid #e0e0e0;
padding: 8px 12px;
text-align: left;
vertical-align: top;
position: relative;
white-space: normal !important; /* 覆盖MindElixir的pre-wrap */
}
.markdown-table th {
background-color: #f5f5f5;
font-weight: 600;
color: #333;
text-align: center;
border-bottom: 1px solid #d0d0d0;
}
.markdown-table td {
background-color: #fff;
}
.markdown-table tr:nth-child(even) td {
background-color: #f8f8f8;
}
.markdown-table tr:hover td {
background-color: #f0f8ff;
}
/* 移除多余的边框,保持简洁 */
.markdown-table th:not(:last-child),
.markdown-table td:not(:last-child) {
border-right: 1px solid #e0e0e0;
}
.markdown-table tr:not(:last-child) td {
border-bottom: 1px solid #e0e0e0;
}
} }

View File

@ -38,7 +38,7 @@ const options: Options = {
if (!text) return '' if (!text) return ''
if (!obj.useMd) return text if (!obj.useMd) return text
try { try {
// Configure marked renderer to add target="_blank" to links // Configure marked renderer to add target="_blank" to links and table classes
const renderer = { const renderer = {
link(token: Tokens.Link) { link(token: Tokens.Link) {
const href = token.href || '' const href = token.href || ''
@ -46,6 +46,13 @@ const options: Options = {
const text = token.text || '' const text = token.text || ''
return `<a href="${href}"${title} target="_blank">${text}</a>` return `<a href="${href}"${title} target="_blank">${text}</a>`
}, },
table(token: Tokens.Table) {
const header = token.header.map(cell => `<th>${cell.text}</th>`).join('')
const rows = token.rows.map(row =>
`<tr>${row.map(cell => `<td>${cell.text}</td>`).join('')}</tr>`
).join('')
return `<table class="markdown-table"><thead><tr>${header}</tr></thead><tbody>${rows}</tbody></table>`
},
} }
marked.use({ renderer, gfm: true }) marked.use({ renderer, gfm: true })

View File

@ -317,4 +317,58 @@
background: #4f90f22d; background: #4f90f22d;
border: 1px solid #4f90f2; border: 1px solid #4f90f2;
} }
/* Markdown表格样式 */
.markdown-table {
border-collapse: collapse;
width: 100%;
margin: 4px 0;
font-size: 11px;
border: 1px solid #e0e0e0;
border-radius: 6px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
background-color: #fafafa;
overflow: hidden;
white-space: normal !important; /* 覆盖MindElixir的pre-wrap */
}
.markdown-table th,
.markdown-table td {
border: 1px solid #e0e0e0;
padding: 8px 12px;
text-align: left;
vertical-align: top;
position: relative;
white-space: normal !important; /* 覆盖MindElixir的pre-wrap */
}
.markdown-table th {
background-color: #f5f5f5;
font-weight: 600;
color: #333;
text-align: center;
border-bottom: 1px solid #d0d0d0;
}
.markdown-table td {
background-color: #fff;
}
.markdown-table tr:nth-child(even) td {
background-color: #f8f8f8;
}
.markdown-table tr:hover td {
background-color: #f0f8ff;
}
/* 移除多余的边框,保持简洁 */
.markdown-table th:not(:last-child),
.markdown-table td:not(:last-child) {
border-right: 1px solid #e0e0e0;
}
.markdown-table tr:not(:last-child) td {
border-bottom: 1px solid #e0e0e0;
}
} }