前端:修复markdown样式

This commit is contained in:
lsy 2024-12-10 19:35:25 +08:00
parent e66f7b902d
commit fde98a5032
2 changed files with 212 additions and 120 deletions

View File

@ -29,10 +29,6 @@ const mockPost: PostDisplay = {
id: 1, id: 1,
title: "Markdown 完全指南:从基础到高级排版", title: "Markdown 完全指南:从基础到高级排版",
content: ` content: `
# Markdown
Markdown
## 1. ## 1.
### 1.1 ### 1.1
@ -195,11 +191,79 @@ function greet(user: User): string {
:smile: :heart: :star: :rocket: :smile: :heart: :star: :rocket:
## 2. ### 1.14
\`\`\`markdown
<details>
<summary >
🎯
</summary>
1. **** - 访
2. **** - Markdown
3. **** -
4. **** -
</details>
\`\`\`
<details>
<summary>
🎯
</summary>
1. **** - 访
2. **** - Markdown
3. **** -
4. **** -
</details>
### 1.15
\`\`\`markdown
> 📌 ****
>
>
>
> 1.
> 2.
> 3.
>
> * *
\`\`\`
> 📌 ****
>
>
>
> 1.
> 2.
> 3.
>
> * *
### 1.16
\`\`\`markdown
[^1]
[^2]:
\`\`\`
[^1]
[^1]:
## 2. HTML排版
### 2.1 ### 2.1
<pre> \`\`\`markdown
<div class="flex items-center gap-6 my-8"> <div class="flex items-center gap-6 my-8">
<img src="https://images.unsplash.com/photo-1516116216624-53e697fedbea?w=400&h=400" <img src="https://images.unsplash.com/photo-1516116216624-53e697fedbea?w=400&h=400"
alt="写作工具" alt="写作工具"
@ -209,7 +273,7 @@ function greet(user: User): string {
<p>使使</p> <p>使使</p>
</div> </div>
</div> </div>
</pre> \`\`\`
<div class="flex items-center gap-6 my-8"> <div class="flex items-center gap-6 my-8">
<img src="https://images.unsplash.com/photo-1516116216624-53e697fedbea?w=400&h=400" <img src="https://images.unsplash.com/photo-1516116216624-53e697fedbea?w=400&h=400"
@ -221,39 +285,11 @@ function greet(user: User): string {
</div> </div>
</div> </div>
### 2.2
<pre>
<details class="my-4">
<summary class="cursor-pointer p-4 bg-gray-100 rounded-lg font-medium hover:bg-gray-200 transition-colors">
🎯
</summary>
### 2.2
1. **** - 访 \`\`\`markdown
2. **** - Markdown
3. **** -
4. **** -
</details>
</pre>
<details class="my-4">
<summary class="cursor-pointer p-4 bg-gray-100 rounded-lg font-medium hover:bg-gray-200 transition-colors">
🎯
</summary>
1. **** - 访
2. **** - Markdown
3. **** -
4. **** -
</details>
### 2.3
<pre>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8"> <div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8">
<div class="p-6 bg-gray-100 rounded-lg"> <div class="p-6 bg-gray-100 rounded-lg">
<h4 class="text-lg font-bold mb-2">🚀 </h4> <h4 class="text-lg font-bold mb-2">🚀 </h4>
@ -264,7 +300,7 @@ function greet(user: User): string {
<p></p> <p></p>
</div> </div>
</div> </div>
</pre> \`\`\`
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8"> <div class="grid grid-cols-1 md:grid-cols-2 gap-6 my-8">
<div class="p-6 bg-gray-100 rounded-lg"> <div class="p-6 bg-gray-100 rounded-lg">
@ -279,12 +315,12 @@ function greet(user: User): string {
### 2.4 ### 2.4
<pre> \`\`\`markdown
<div class="p-6 bg-blue-50 border-l-4 border-blue-500 rounded-lg my-8"> <div class="p-6 bg-blue-50 border-l-4 border-blue-500 rounded-lg my-8">
<h4 class="text-lg font-bold text-blue-700 mb-2">💡 </h4> <h4 class="text-lg font-bold text-blue-700 mb-2">💡 </h4>
<p class="text-blue-600"></p> <p class="text-blue-600"></p>
</div> </div>
</pre> \`\`\`
<div class="p-6 bg-blue-50 border-l-4 border-blue-500 rounded-lg my-8"> <div class="p-6 bg-blue-50 border-l-4 border-blue-500 rounded-lg my-8">
<h4 class="text-lg font-bold text-blue-700 mb-2"></h4> <h4 class="text-lg font-bold text-blue-700 mb-2"></h4>
@ -293,7 +329,7 @@ function greet(user: User): string {
### 2.5 线 ### 2.5 线
<pre> \`\`\`markdown
<div class="relative pl-8 my-8 border-l-2 border-gray-200"> <div class="relative pl-8 my-8 border-l-2 border-gray-200">
<div class="mb-8 relative"> <div class="mb-8 relative">
<div class="absolute -left-10 w-4 h-4 bg-blue-500 rounded-full"></div> <div class="absolute -left-10 w-4 h-4 bg-blue-500 rounded-full"></div>
@ -307,7 +343,7 @@ function greet(user: User): string {
<p>广</p> <p>广</p>
</div> </div>
</div> </div>
</pre> \`\`\`
<div class="relative pl-8 my-8 border-l-2 border-gray-200"> <div class="relative pl-8 my-8 border-l-2 border-gray-200">
<div class="mb-8 relative"> <div class="mb-8 relative">
@ -323,53 +359,9 @@ function greet(user: User): string {
</div> </div>
</div> </div>
### 2.6
<pre>
> 📌 ****
>
>
>
> 1.
> 2.
> 3.
>
> * *
</pre>
> 📌 **** ## 3.
>
>
>
> 1.
> 2.
> 3.
>
> * *
## 3.
### 3.1
<pre>
[^1]
[^1]:
</pre>
[^1]
[^1]:
### 3.2
<pre>
:smile: :heart: :star: :rocket:
</pre>
:smile: :heart: :star: :rocket:
## 4.
Markdown Markdown
@ -701,6 +693,7 @@ export default new Template({}, ({ http, args }) => {
const match = /language-(\w+)/.exec(className || ''); const match = /language-(\w+)/.exec(className || '');
const code = String(children).replace(/\n$/, ''); const code = String(children).replace(/\n$/, '');
// 如果是内联代码
if (!className || inline) { if (!className || inline) {
return ( return (
<code <code
@ -714,6 +707,9 @@ export default new Template({}, ({ http, args }) => {
const language = match ? match[1] : ''; const language = match ? match[1] : '';
// 特殊处理表格语法
const isTable = code.includes('|') && code.includes('\n') && code.includes('---');
return ( return (
<div className="my-4 sm:my-6"> <div className="my-4 sm:my-6">
<div className="flex justify-between items-center h-9 sm:h-10 px-4 sm:px-6 <div className="flex justify-between items-center h-9 sm:h-10 px-4 sm:px-6
@ -730,36 +726,50 @@ export default new Template({}, ({ http, args }) => {
<div className="border border-[--gray-6] rounded-b-lg bg-white dark:bg-[--gray-1]"> <div className="border border-[--gray-6] rounded-b-lg bg-white dark:bg-[--gray-1]">
<div className="overflow-x-auto"> <div className="overflow-x-auto">
<div className="p-4 sm:p-6"> <div className="p-4 sm:p-6">
<SyntaxHighlighter {isTable ? (
language={language || "text"} // 对表格使用 pre 标签保持格式
style={{ <pre
...oneLight, className="m-0 p-0 bg-transparent font-mono text-[0.9rem] leading-relaxed overflow-x-auto"
'punctuation': { style={{
color: 'var(--gray-12)' color: 'inherit',
}, whiteSpace: 'pre'
'operator': { }}
color: 'var(--gray-12)' >
}, {code}
'symbol': { </pre>
color: 'var(--gray-12)' ) : (
} // 其他代码使用 SyntaxHighlighter
}} <SyntaxHighlighter
customStyle={{ language={language || "text"}
margin: 0, style={{
padding: 0, ...oneLight,
background: "none", 'punctuation': {
fontSize: "0.9rem", color: 'var(--gray-12)'
lineHeight: 1.6, },
}} 'operator': {
codeTagProps={{ color: 'var(--gray-12)'
className: "dark:text-[--gray-12]", },
style: { 'symbol': {
color: "inherit" color: 'var(--gray-12)'
} }
}} }}
> customStyle={{
{code} margin: 0,
</SyntaxHighlighter> padding: 0,
background: "none",
fontSize: "0.9rem",
lineHeight: 1.6,
}}
codeTagProps={{
className: "dark:text-[--gray-12]",
style: {
color: "inherit"
}
}}
>
{code}
</SyntaxHighlighter>
)}
</div> </div>
</div> </div>
</div> </div>
@ -837,7 +847,8 @@ export default new Template({}, ({ http, args }) => {
<summary <summary
className="px-4 py-3 cursor-pointer hover:bg-[--gray-3] transition-colors className="px-4 py-3 cursor-pointer hover:bg-[--gray-3] transition-colors
text-[--gray-12] font-medium select-none text-[--gray-12] font-medium select-none
marker:text-[--gray-11]" marker:text-[--gray-11]
"
{...props} {...props}
/> />
), ),

View File

@ -316,3 +316,84 @@ html[class~="dark"] .scroll-container::-webkit-scrollbar-thumb:hover {
.scroll-hidden::-webkit-scrollbar { .scroll-hidden::-webkit-scrollbar {
display: none; /* Chrome, Safari and Opera */ display: none; /* Chrome, Safari and Opera */
} }
/* 可折叠块样式 */
details {
transition: all 0.2s ease;
background-color: transparent;
border: 1px solid var(--gray-6);
border-radius: 8px;
overflow: hidden;
}
/* 亮色模式样式 */
details summary {
padding: 1rem;
background-color: var(--gray-2);
font-weight: 500;
color: var(--gray-12);
border-bottom: 1px solid transparent; /* 添加透明边框 */
transition: all 0.2s ease; /* 添加过渡效果 */
}
details summary:hover {
background-color: var(--gray-4);
}
details[open] {
background-color: var(--gray-2);
}
/* 暗色模式样式覆盖 */
html[class~="dark"] details {
background-color: var(--gray-3);
background-color: transparent;
}
html[class~="dark"] details summary {
background-color: var(--gray-3);
color: var(--gray-12);
}
html[class~="dark"] details summary:hover {
background-color: var(--gray-5);
}
html[class~="dark"] details[open] {
background-color: var(--gray-3);
}
/* 内容区域样式 */
details > *:not(summary) {
padding: 1rem;
color: var(--gray-11);
}
/* 移除默认的三角形标记 */
details summary::-webkit-details-marker,
details summary::marker {
display: none;
}
/* 自定义展开/折叠指示器 */
details summary {
position: relative;
padding-right: 2.5rem;
cursor: pointer;
user-select: none;
}
details summary::after {
position: absolute;
right: 1rem;
top: 50%;
transform: translateY(-50%);
transition: transform 0.2s ease;
color: var(--gray-11);
}
/* 暗色模式下的指示器颜色 */
html[class~="dark"] details summary::after {
color: var(--gray-10);
}