现在只支持了提示框语法,别急。
AI 使用声明:本文使用 Gemini 辅助编写代码。
下载插件 Markdown Preview Enhanced。
在 settings.json 中添加代码:
"markdown-preview-enhanced.enableScriptExecution": true,
"markdown-preview-enhanced.previewTheme": "atom-light.css",
"markdown-preview-enhanced.mathRenderingOption": "MathJax",
"markdown-preview-enhanced.codeBlockTheme": "auto.css",在控制面板中输入命令 Markdown Preview Enhanced: Extend Parser (Workspace)(Global 也行)来打开 parser.js,输入代码:
({
onWillParseMarkdown: function(markdown) {
return new Promise((resolve) => {
const lines = markdown.split('\n');
const result = [];
const stack = [];
let hasMathInTitle = false;
const defaultTitles = {
'info': '提示', 'success': '成功', 'warning': '警告',
'danger': '错误', 'error': '错误'
};
// 增强版行内解析:增加数学公式保护逻辑
const miniParse = (str) => {
if (!str) return str;
// 1. 识别并提取数学公式,用占位符替代,防止其中的 _ 或 * 被误标记
const mathBlocks = [];
let processedStr = str.replace(/\$(.*?)\$/g, (match) => {
hasMathInTitle = true;
mathBlocks.push(match);
return `__MPE_MATH_PLACEHOLDER_${mathBlocks.length - 1}__`;
});
// 2. 在没有公式干扰的文本上解析 Markdown 标记
processedStr = processedStr
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/\_\_(.*?)\_\_/g, '<strong>$1</strong>')
.replace(/\*(.*?)\*/g, '<em>$1</em>')
.replace(/\_(.*?)\_/g, '<em>$1</em>')
.replace(/~~(.*?)~~/g, '<del>$1</del>')
.replace(/`(.*?)`/g, '<code>$1</code>');
// 3. 将数学公式还原回标题中
return processedStr.replace(/__MPE_MATH_PLACEHOLDER_(\d+)__/g, (match, index) => {
return mathBlocks[index];
});
};
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const trimmed = line.trim();
const startMatch = trimmed.match(/^(:{3,})\s*(\w+)(?:\s*\[(.*?)\])?(?:\s*\{(open)\})?\s*$/);
const endMatch = trimmed.match(/^:{3,}\s*$/);
if (startMatch) {
const type = startMatch[2].toLowerCase();
const capturedTitle = startMatch[3];
const isOpen = startMatch[4] === 'open' ? 'open' : '';
let displayTitle = capturedTitle !== undefined ? capturedTitle : (defaultTitles[type] || type.toUpperCase());
displayTitle = miniParse(displayTitle);
stack.push(true);
result.push(`<details ${isOpen} class="mpe-container ${type}"><summary class="mpe-container-title"><span>${displayTitle}</span></summary>\n`);
} else if (endMatch && stack.length > 0) {
stack.pop();
result.push(`\n</details>\n`);
} else {
result.push(line);
}
}
// 如果标题有公式,注入隐藏公式唤醒 KaTeX 引擎
if (hasMathInTitle) {
result.push('\n\n$\\,$');
}
resolve(result.join('\n'));
});
}
})打开同目录下的 style.less 文件(或输入命令 Markdown Preview Enhanced: Customize CSS (Workspace)),输入代码:
/* -----------------------------------------------------------
自定义折叠框 (Container) 最终修复版
----------------------------------------------------------- */
/* 基础容器 */
.mpe-container {
margin: 1.2em 0;
border-left: 5px solid #dfe2e5;
border-radius: 0; /* 直角 */
padding: 0 16px 12px 16px;
box-shadow: none;
transition: all 0.2s ease;
overflow: hidden;
display: block;
}
/* 标题栏基础样式 */
.mpe-container-title {
display: flex;
align-items: center;
list-style: none;
cursor: pointer;
outline: none;
padding: 12px 0;
font-size: 0.9em; /* 调小标题字体 */
font-weight: 700;
user-select: none;
position: relative;
}
/* 隐藏原生箭头 */
.mpe-container-title::-webkit-details-marker { display: none; }
/* 图标圆圈基础样式 */
.mpe-container-title::before {
display: inline-flex;
align-items: center;
justify-content: center;
width: 17px;
height: 17px;
border-radius: 50%;
margin-right: 12px;
color: #ffffff !important; /* 图标符号永远是白色 */
font-size: 13px;
font-family: "Arial", sans-serif;
flex-shrink: 0;
font-weight: bold;
}
/* 右侧箭头 (小三角形) */
.mpe-container-title::after {
content: '';
display: block;
width: 0; height: 0;
margin-left: auto;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 6px solid currentColor; /* 箭头颜色跟随标题 */
transition: transform 0.2s ease;
opacity: 0.7;
}
/* 展开状态下的箭头 */
.mpe-container[open] .mpe-container-title::after {
transform: rotate(90deg);
}
/* -----------------------------------------------------------
内容区域颜色控制:确保只有内容是黑色的
----------------------------------------------------------- */
/* 针对 details 内部除了 summary 以外的所有元素 */
.mpe-container > *:not(.mpe-container-title) {
color: #000000;
font-size: 0.95em;
line-height: 1.6;
}
/* -----------------------------------------------------------
四种类型的配色方案(背景、边框、标题文字、图标背景)
----------------------------------------------------------- */
/* 1. Info (蓝色) */
.mpe-container.info {
border-left-color: #3498db;
// background-color: #f0f7ff; /* 浅蓝色背景 */
}
.mpe-container.info > .mpe-container-title {
color: #3498db; /* 标题文字蓝色 */
}
.mpe-container.info > .mpe-container-title::before {
content: 'i';
background-color: #3498db; /* 图标圆圈蓝色 */
font-family: "Georgia", serif;
// font-style: italic;
}
/* 2. Success (绿色) */
.mpe-container.success {
border-left-color: #42b983;
// background-color: #f3faf4; /* 浅绿色背景 */
}
.mpe-container.success > .mpe-container-title {
color: #42b983;
}
.mpe-container.success > .mpe-container-title::before {
content: '✓';
background-color: #42b983;
}
/* 3. Warning (橙色) */
.mpe-container.warning {
border-left-color: #f39c12;
// background-color: #fffdf3; /* 浅橙色背景 */
}
.mpe-container.warning > .mpe-container-title {
color: #f39c12;
}
.mpe-container.warning > .mpe-container-title::before {
content: '!';
background-color: #f39c12;
}
/* 4. Danger/Error (红色) */
.mpe-container.danger, .mpe-container.error {
border-left-color: #e74c3c;
// background-color: #fff5f5; /* 浅红色背景 */
}
.mpe-container.danger > .mpe-container-title,
.mpe-container.error > .mpe-container-title {
color: #e74c3c;
}
.mpe-container.danger > .mpe-container-title::before,
.mpe-container.error > .mpe-container-title::before {
content: '✕';
background-color: #e74c3c;
font-size: 11px;
}
/* -----------------------------------------------------------
KaTeX 数学公式适配
----------------------------------------------------------- */
.mpe-container-title .katex {
font-weight: normal !important;
color: inherit; /* 标题里的公式颜色跟随标题 */
}
.mpe-container-title strong,
.mpe-container-title del,
.mpe-container-title em,
.mpe-container-title code,
.mpe-container-title .katex {
/* 关键:强制继承父级(即 .mpe-container-title)的颜色 */
color: inherit !important;
/* 确保公式和加粗的对比度 */
opacity: 1;
}
/* 针对删除线的特殊处理 */
.mpe-container-title del {
text-decoration: line-through;
opacity: 0.8; /* 删除线通常稍微淡一点,如果不想要淡化可以删掉这行 */
}
/* 针对加粗的特殊处理 */
.mpe-container-title strong {
font-weight: 1000;
}
/* 针对代码块的特殊处理 */
.mpe-container-title code {
background-color: rgba(255, 255, 255, 0.4); /* 半透明背景,适应不同主题色 */
padding: 1px 4px;
border-radius: 3px;
font-family: Consolas, Monaco, monospace;
}
/* 确保内部正文仍然是黑色 (防止颜色溢出) */
.mpe-container > *:not(.mpe-container-title) {
color: #555555 !important;
}
/* -----------------------------------------------------------
配色方案 (确保颜色应用到 title,以便被子元素继承)
----------------------------------------------------------- */
.mpe-container.info > .mpe-container-title { color: #3498db; }
.mpe-container.success > .mpe-container-title { color: #42b983; }
.mpe-container.warning > .mpe-container-title { color: #f39c12; }
.mpe-container.danger > .mpe-container-title,
.mpe-container.error > .mpe-container-title { color: #e74c3c; }