<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML 转 Markdown 转换器</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
color: #333;
}
h1 {
color: #2c3e50;
text-align: center;
margin-bottom: 30px;
}
.container {
display: flex;
flex-direction: column;
gap: 20px;
}
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}
.editor-container, .preview-container {
flex: 1;
display: flex;
flex-direction: column;
}
textarea {
width: 100%;
min-height: 300px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: 'Consolas', 'Monaco', monospace;
resize: vertical;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin: 10px 0;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
label {
font-weight: bold;
margin-bottom: 5px;
display: block;
}
.preview-area {
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
min-height: 300px;
background-color: #f9f9f9;
white-space: pre-wrap;
overflow-y: auto;
}
.footer {
text-align: center;
margin-top: 30px;
color: #7f8c8d;
font-size: 14px;
}
a {
color: #3498db;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>HTML 转 Markdown 转换器</h1>
<div class="container">
<div class="editor-container">
<label for="html-input">HTML 输入:</label>
<textarea id="html-input" placeholder="在此粘贴HTML代码..."></textarea>
<button id="convert-btn">转换为 Markdown ↓</button>
</div>
<div class="preview-container">
<label for="markdown-output">Markdown 输出:</label>
<div id="markdown-output" class="preview-area"></div>
<button id="copy-btn">复制 Markdown</button>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const htmlInput = document.getElementById('html-input');
const markdownOutput = document.getElementById('markdown-output');
const convertBtn = document.getElementById('convert-btn');
const copyBtn = document.getElementById('copy-btn');
// 转换按钮点击事件
convertBtn.addEventListener('click', function() {
const html = htmlInput.value.trim();
if (html) {
const markdown = htmlToMarkdown(html);
markdownOutput.textContent = markdown;
} else {
markdownOutput.textContent = '请输入HTML内容';
}
});
// 复制按钮点击事件
copyBtn.addEventListener('click', function() {
const range = document.createRange();
range.selectNode(markdownOutput);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
// 显示复制成功提示
const originalText = copyBtn.textContent;
copyBtn.textContent = '已复制!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
});
// HTML 转 Markdown 函数
function htmlToMarkdown(html) {
// 创建一个临时div来解析HTML
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
// 递归处理所有节点
function processNode(node) {
if (node.nodeType === Node.TEXT_NODE) {
return node.textContent;
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return '';
}
const tagName = node.tagName.toLowerCase();
const children = Array.from(node.childNodes);
const content = children.map(processNode).join('');
switch (tagName) {
case 'h1':
return `# ${content}\n\n`;
case 'h2':
return `## ${content}\n\n`;
case 'h3':
return `### ${content}\n\n`;
case 'h4':
return `#### ${content}\n\n`;
case 'h5':
return `##### ${content}\n\n`;
case 'h6':
return `###### ${content}\n\n`;
case 'p':
return `${content}\n\n`;
case 'br':
return '\n';
case 'hr':
return '---\n\n';
case 'strong':
case 'b':
return `**${content}**`;
case 'em':
case 'i':
return `*${content}*`;
case 'code':
return `\`${content}\``;
case 'pre':
return `\`\`\`\n${content}\n\`\`\`\n\n`;
case 'blockquote':
return `> ${content.replace(/\n/g, '\n> ')}\n\n`;
case 'ul':
const ulItems = Array.from(node.querySelectorAll('li'));
return ulItems.map(li => `- ${processNode(li).trim()}`).join('\n') + '\n\n';
case 'ol':
const olItems = Array.from(node.querySelectorAll('li'));
return olItems.map((li, i) => `${i + 1}. ${processNode(li).trim()}`).join('\n') + '\n\n';
case 'li':
return content;
case 'a':
const href = node.getAttribute('href') || '';
return `[${content}](${href})`;
case 'img':
const src = node.getAttribute('src') || '';
const alt = node.getAttribute('alt') || '';
return ``;
case 'div':
case 'span':
return content;
default:
return content;
}
}
// 处理所有子节点
const result = Array.from(tempDiv.childNodes)
.map(processNode)
.join('')
.replace(/\n{3,}/g, '\n\n') // 移除多余的空行
.trim();
return result;
}
});
</script>
</body>
</html>