Markdown Preview
编写 Markdown 并实时预览渲染效果。
Hello, Markdown!
This is bold and this is italic text.
Features
- Live preview as you type
- Syntax support for common elements
- Code blocks with
inline code
const greet = (name) => `Hello, ${name}!`;
console.log(greet("World"));
Blockquotes look like this.
Markdown 预览将 Markdown 格式的文本实时渲染为样式化的 HTML,让您在发布之前准确看到文档的外观。Markdown 是 GitHub README 文件、使用 MkDocs 或 Docusaurus 等工具构建的文档站点以及 Dev.to 和 Hashnode 等博客平台的标准格式语言。
在左侧面板中编写 Markdown,格式化的输出会立即显示在右侧面板中。渲染器支持完整的 CommonMark 规范和常见扩展:标题、粗体、斜体、行内代码、代码块、引用、列表、表格、任务列表、链接和图片等。
Markdown 故意设计得很简单,但某些格式选择存在特定于平台的细微差别。例如,GitHub Flavored Markdown (GFM) 添加了删除线(~~text~~)和任务列表。如果您是为特定平台写作,请查看其文档以了解它使用的 Markdown 方言。
常见问题
代码实现
import re
def simple_markdown_to_html(text: str) -> str:
"""Convert basic Markdown to HTML."""
lines = text.split("\n")
html_lines = []
in_code_block = False
for line in lines:
# Fenced code block
if line.startswith("```"):
if in_code_block:
html_lines.append("</code></pre>")
in_code_block = False
else:
lang = line[3:].strip()
html_lines.append(f'<pre><code class="language-{lang}">')
in_code_block = True
continue
if in_code_block:
html_lines.append(line)
continue
# Headings
if line.startswith("### "):
html_lines.append(f"<h3>{line[4:]}</h3>")
elif line.startswith("## "):
html_lines.append(f"<h2>{line[3:]}</h2>")
elif line.startswith("# "):
html_lines.append(f"<h1>{line[2:]}</h1>")
elif line.startswith("- "):
html_lines.append(f"<li>{line[2:]}</li>")
elif line.strip() == "":
html_lines.append("<br>")
else:
# Bold and italic
line = re.sub(r'\*\*(.+?)\*\*', r'<strong>\1</strong>', line)
line = re.sub(r'\*(.+?)\*', r'<em>\1</em>', line)
# Links
line = re.sub(r'\[(.+?)\]\((.+?)\)', r'<a href="\2">\1</a>', line)
# Inline code
line = re.sub(r'`(.+?)`', r'<code>\1</code>', line)
html_lines.append(f"<p>{line}</p>")
return "\n".join(html_lines)
md = "# Hello\n**Bold** and *italic* text\n[Link](https://example.com)"
print(simple_markdown_to_html(md))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.