DevToolBox無料
ブログ

Markdown to HTML コンバーター完全ガイド:コード例付き

13分by DevToolBox

MarkdownからHTMLへの変換は、現代のWeb開発において最も重要なワークフローの一つです。何百万もの開発者がMarkdownコンバーターを使用して、軽量なプレーンテキストを構造化されたHTMLに変換しています。ブログ記事やREADMEファイル、ドキュメントサイトの構築まで、このガイドがすべてをカバーします。無料のオンラインツールで、ライブプレビュー付きの即時変換が可能です。

ライブプレビュー付きの無料オンラインMarkdownからHTML変換ツールをお試しください。

TL;DR (要約)

Markdownは軽量なプレーンテキスト書式言語で、HTMLに変換できます。JavaScriptではmarkdown-it(拡張性・CommonMark準拠)またはmarked(軽量高速)を推奨。Pythonではmistune(最速)またはmarkdownライブラリ。CLIではpandoc。ReactプロジェクトではReact-markdownまたはMDX。GFMはテーブル、タスクリスト、取り消し線を追加。ユーザー入力のXSSサニタイズは必須。@tailwindcss/typographyのproseクラスでスタイリング。

重要ポイント

  • Markdownは3段階パイプライン(トークン化、AST構築、HTMLレンダリング)でHTMLに変換
  • CommonMarkが基本仕様で、GFMがテーブル、タスクリスト、取り消し線などを追加
  • 主要ライブラリ比較:marked(高速軽量)、markdown-it(拡張可能)、remark(AST駆動)、Goldmark(Go言語)
  • セキュリティ第一:ユーザー入力のMarkdownは必ずDOMPurifyでXSSサニタイズ
  • 高度な拡張:数式(KaTeX)、Mermaidダイアグラム、フロントマター、目次生成

Markdownとは?

Markdownは、2004年にJohn Gruberによって作成された軽量マークアップ言語です。読みやすく書きやすいプレーンテキスト書式構文で、有効なHTMLに変換できます。HTMLの冗長な角括弧タグとは異なり、Markdownは直感的な句読点文字を使用します。

Markdownは、Webコンテンツ作成のデファクトスタンダードとなりました。GitHubのREADMEファイル、Redditのコメント、Stack Overflowの質問と回答で使用されています。Hugo、Jekyll、Gatsby、AstroなどのSSGがMarkdownファイルからHTMLページを生成します。

Markdownの魅力はそのシンプルさと移植性にあります。.mdファイルはプレーンテキストで、あらゆるエディタやOSで使用できます。

Markdown構文リファレンス

Markdown構文の理解は、効果的な変換への第一歩です:

見出し#から######でレベル1から6を表します。

太字と斜体:アスタリスク1つで斜体、2つで太字、3つで太字斜体。

リンクと画像[テキスト](URL)でリンク、![代替テキスト](URL)で画像。

コード:バッククォート1つでインラインコード、3つでコードブロック。

リスト-*+で順序なしリスト、番号で順序付きリスト。

引用:行頭に>を付けて引用ブロックを作成。

テーブル:パイプ(|)とハイフン(-)でテーブルを作成。

水平線:ハイフン3つ(---)、アスタリスク(***)、アンダースコア(___)。

タスクリスト- [ ]で未チェック、- [x]でチェック済み。

MarkdownからHTMLへの変換の仕組み

Markdownパーサーは、3つの主要フェーズからなるマルチステージパイプラインでプレーンテキストをHTMLに変換します:

フェーズ1 - トークン化:パーサーがMarkdown文字列を読み取り、トークンストリームに分解します。

フェーズ2 - AST構築:トークンストリームが抽象構文木に組織化されます。

フェーズ3 - HTMLレンダリング:ASTを走査し、各ノードを対応するHTML要素に変換します。

CommonMark仕様は、600以上の例を含むMarkdown構文の厳密な定義を提供します。

コード例:MarkdownからHTML

JavaScript / Node.js(marked、markdown-it、showdown、remark)

JavaScriptはMarkdownからHTMLへの変換で最も豊かなエコシステムを提供します:

// ===== marked.js (fast, lightweight) =====
import { marked } from 'marked';

const markdown = '# Hello World\n\nThis is **bold** and *italic*.';
const html = marked(markdown);
// <h1>Hello World</h1>
// <p>This is <strong>bold</strong> and <em>italic</em>.</p>

// Configure marked options
marked.setOptions({
  gfm: true,          // GitHub Flavored Markdown
  breaks: true,       // Convert \n to <br>
  pedantic: false,     // Conform to CommonMark
  sanitize: false,     // Use DOMPurify instead
});

// ===== markdown-it (extensible, plugin-based) =====
import MarkdownIt from 'markdown-it';
import markdownItAnchor from 'markdown-it-anchor';
import markdownItToc from 'markdown-it-toc-done-right';

const md = new MarkdownIt({
  html: true,          // Allow HTML tags in source
  linkify: true,       // Auto-convert URLs to links
  typographer: true,   // Smart quotes, dashes
})
  .use(markdownItAnchor)   // Add anchors to headings
  .use(markdownItToc);     // Generate table of contents

const result = md.render('# Hello\n\n${toc}\n\n## Section 1\n\nContent here.');

// ===== showdown (browser + Node.js) =====
import showdown from 'showdown';

const converter = new showdown.Converter({
  tables: true,
  tasklists: true,
  strikethrough: true,
  ghCodeBlocks: true,
});
const htmlOutput = converter.makeHtml('## Hello\n\n- [x] Task done');

// ===== unified / remark ecosystem (AST-based) =====
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkGfm from 'remark-gfm';
import remarkRehype from 'remark-rehype';
import rehypeStringify from 'rehype-stringify';
import rehypePrism from 'rehype-prism-plus';

const file = await unified()
  .use(remarkParse)         // Parse Markdown to mdast
  .use(remarkGfm)           // Support GFM (tables, etc.)
  .use(remarkRehype)        // Transform mdast to hast
  .use(rehypePrism)         // Syntax highlighting
  .use(rehypeStringify)     // Serialize hast to HTML
  .process('# Hello **World**');

console.log(String(file));
// <h1>Hello <strong>World</strong></h1>

Python(markdown、mistune、markdown2)

PythonにはMarkdownからHTMLへの変換用の優れたライブラリが複数あります:

# ===== markdown library (standard, extensible) =====
import markdown

# Basic conversion
md_text = "# Hello World\n\nThis is **bold** text."
html = markdown.markdown(md_text)
# <h1>Hello World</h1>
# <p>This is <strong>bold</strong> text.</p>

# With extensions
html = markdown.markdown(md_text, extensions=[
    'tables',           # GFM-style tables
    'fenced_code',      # Fenced code blocks
    'codehilite',       # Syntax highlighting (requires Pygments)
    'toc',              # Table of contents
    'footnotes',        # Footnote support
    'attr_list',        # Attribute lists for styling
    'md_in_html',       # Markdown inside HTML blocks
])

# ===== mistune (fastest Python Markdown parser) =====
import mistune

# Basic usage
html = mistune.html(md_text)

# Custom renderer for advanced control
renderer = mistune.create_markdown(
    escape=False,
    plugins=['table', 'footnotes', 'strikethrough', 'task_lists']
)
html = renderer(md_text)

# ===== markdown2 (simple API, many extras) =====
import markdown2

html = markdown2.markdown(md_text, extras=[
    "fenced-code-blocks",
    "tables",
    "strike",
    "task_list",
    "code-friendly",
    "cuddled-lists",
    "header-ids",
    "metadata",
])

# Convert a file
with open("README.md", "r") as f:
    html = markdown2.markdown(f.read(), extras=["fenced-code-blocks"])

# Python-Markdown extension: convert file to HTML file
import markdown
md = markdown.Markdown(extensions=['meta', 'toc'])
md.convertFile(input='doc.md', output='doc.html')

Bash / CLI(pandoc、cmark、grip)

コマンドラインツールでターミナルから直接MarkdownをHTMLに変換できます:

# ===== pandoc (universal document converter) =====
# Basic Markdown to HTML
pandoc README.md -o output.html

# With standalone HTML (includes <head>, <body>)
pandoc README.md -s -o output.html

# With table of contents and syntax highlighting
pandoc README.md -s --toc --highlight-style=tango -o output.html

# GFM input format
pandoc -f gfm -t html README.md -o output.html

# With custom CSS
pandoc README.md -s --css=style.css -o output.html

# Convert multiple files
pandoc ch1.md ch2.md ch3.md -s --toc -o book.html

# ===== cmark (reference CommonMark implementation) =====
# Basic conversion
cmark README.md > output.html

# With smart typography (quotes, dashes)
cmark --smart README.md > output.html

# GFM extensions (cmark-gfm)
cmark-gfm -e table -e strikethrough -e tasklist README.md

# ===== grip (GitHub-flavored preview) =====
# Preview README in browser (uses GitHub API)
grip README.md

# Export to HTML file
grip README.md --export output.html

# Preview on specific port
grip README.md 0.0.0.0:8080

# ===== Simple pipe conversion with Node.js =====
echo "# Hello **World**" | npx marked
# <h1>Hello <strong>World</strong></h1>

cat README.md | npx marked > output.html

React / Next.js(react-markdown、MDX、rehype)

ReactとNext.jsでは、変換はコンポーネントレベルで行われます:

// ===== react-markdown (React component) =====
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import rehypeHighlight from 'rehype-highlight';

function MarkdownPreview({ content }: { content: string }) {
  return (
    <ReactMarkdown
      remarkPlugins={[remarkGfm]}
      rehypePlugins={[rehypeHighlight]}
      components={{
        // Custom component overrides
        h1: ({ children }) => (
          <h1 className="text-3xl font-bold mb-4">{children}</h1>
        ),
        a: ({ href, children }) => (
          <a href={href} className="text-blue-600 hover:underline"
             target="_blank" rel="noopener noreferrer">
            {children}
          </a>
        ),
        code: ({ className, children, ...props }) => {
          const isInline = !className;
          return isInline
            ? <code className="bg-gray-100 px-1 rounded">{children}</code>
            : <code className={className} {...props}>{children}</code>;
        },
      }}
    >
      {content}
    </ReactMarkdown>
  );
}

// ===== MDX (Markdown + JSX) in Next.js =====
// next.config.js
import createMDX from '@next/mdx';

const withMDX = createMDX({
  extension: /\.mdx?$/,
  options: {
    remarkPlugins: [remarkGfm],
    rehypePlugins: [rehypeHighlight],
  },
});

export default withMDX({
  pageExtensions: ['ts', 'tsx', 'md', 'mdx'],
});

// app/blog/[slug]/page.tsx
import { MDXRemote } from 'next-mdx-remote/rsc';

export default async function BlogPost({ params }) {
  const source = await getMarkdownContent(params.slug);
  return <MDXRemote source={source} />;
}

// ===== Syntax highlighting with Shiki =====
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkRehype from 'remark-rehype';
import rehypeShiki from '@shikijs/rehype';
import rehypeStringify from 'rehype-stringify';

const html = await unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypeShiki, { theme: 'github-dark' })
  .use(rehypeStringify)
  .process(markdownContent);

GitHub Flavored Markdown(GFM)

GitHub Flavored Markdown(GFM)はGitHubが開発したCommonMarkのスーパーセットで、ソフトウェア開発に必要な機能を追加します。

テーブル:GFMテーブルはコロンによる列の配置をサポートします。

タスクリスト:GitHubのIssueやPRでチェックボックスとしてレンダリングされます。

取り消し線:二重チルダ(~~削除~~)で<del>タグを使用。

オートリンク:GFMはURLを自動的にクリック可能なリンクに変換します。

脚注とアラート:拡張GFMは脚注とアラートブロックをサポートします。

<!-- GFM Examples and their HTML output -->

<!-- Table with alignment -->
| Feature       | CommonMark | GFM   |
|:--------------|:----------:|------:|
| Tables        |     No     |  Yes  |
| Task Lists    |     No     |  Yes  |
| Strikethrough |     No     |  Yes  |
| Autolinks     |     No     |  Yes  |

<!-- Converts to: -->
<table>
  <thead>
    <tr>
      <th style="text-align:left">Feature</th>
      <th style="text-align:center">CommonMark</th>
      <th style="text-align:right">GFM</th>
    </tr>
  </thead>
  <tbody>...</tbody>
</table>

<!-- Task list -->
- [x] Write the documentation
- [x] Add code examples
- [ ] Publish the article

<!-- Strikethrough -->
~~This text is deleted~~

<!-- Autolink -->
Visit https://example.com for details.

<!-- Footnote -->
Here is a statement[^1] with a footnote.
[^1]: This is the footnote content.

<!-- Alert blocks (GitHub-specific) -->
> [!NOTE]
> This is a note callout.

> [!WARNING]
> This is a warning callout.

高度なMarkdown機能

Markdownエコシステムは拡張とプラグインを通じて多くの高度な機能をサポートします:

カスタムコンテナ:markdown-it-containerやremark-directiveでカスタムブロック要素を定義できます。

数式表記(KaTeX / MathJax):科学技術文書に不可欠な数式レンダリング。

Mermaidによるダイアグラム:テキスト構文からダイアグラムを生成。

フロントマター:YAMLメタデータでタイトル、日付、著者、タグを格納。

目次生成:プラグインが見出しから自動的に目次を生成します。

<!-- Custom container (markdown-it-container) -->
::: warning
This is a warning message. Be careful!
:::

<!-- Renders as: -->
<div class="warning">
  <p>This is a warning message. Be careful!</p>
</div>

<!-- Math with KaTeX -->
Inline math: $E = mc^2$
Display math:
$$
\sum_{i=1}^{n} x_i = \frac{n(n+1)}{2}
$$

<!-- Mermaid diagram -->
```mermaid
graph TD
    A[Markdown Source] --> B[Tokenizer]
    B --> C[AST Builder]
    C --> D[HTML Renderer]
    D --> E[HTML Output]
```

<!-- Front matter (YAML) -->
---
title: "My Blog Post"
date: 2024-01-15
author: "John Doe"
tags: [markdown, html, tutorial]
---

# My Blog Post
Content starts here...

<!-- Table of contents (remark-toc) -->
## Table of Contents

<!-- Auto-generated from headings -->
- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [Advanced Usage](#advanced-usage)

Markdownのベストプラクティス

ベストプラクティスに従うことで、一貫性がありアクセシブルなMarkdownファイルを確保します:

一貫した見出し階層:H1から始め、後続のレベルを順番に使用してください。

画像の説明的な代替テキスト:常に意味のある代替テキストを提供してください。

セマンティック改行:文の境界で改行し、差分を読みやすくします。

markdownlintによるリント:markdownlintで一貫したスタイルを強制します。

アクセシビリティ:正しい見出し構造と説明的なリンクテキストを確保してください。

逆変換にはHTMLからMarkdownへの変換ツール、コンテンツの長さチェックには文字数カウンターもご覧ください。

よくある質問

MarkdownをHTMLに変換するには?

JavaScriptではmarked.js、markdown-it、showdown、Pythonではmarkdownやmistune、コマンドラインではpandocやcmarkを使用します。変換プロセスはトークン化、AST構築、HTMLレンダリングで構成されます。

最良のMarkdownからHTMLへのライブラリは?

JavaScriptではmarkdown-itが速度とプラグインエコシステムで最も人気。Reactではreact-markdownが標準。Pythonではmistuneが最速。pandocは万能なコマンドラインツールです。

GFMとCommonMarkの違いは?

CommonMarkはオリジナルのMarkdown構文の厳密な仕様です。GFMはテーブル、タスクリスト、取り消し線、オートリンクを追加したスーパーセットです。ほとんどのモダンパーサーが両方をサポートしています。

ReactやNext.jsでMarkdownを使えますか?

はい。最も人気のあるアプローチはreact-markdownです。MDXではMarkdown内にJSXコンポーネントを埋め込めます。Next.jsは@next/mdxやremark/rehypeプラグインをサポートしています。

Markdownコードブロックにシンタックスハイライトを追加するには?

Prism.js(rehype-prism)、Shiki、highlight.jsを使用します。パーサーが言語識別子を保持し、ハイライトライブラリがコードを処理します。

MarkdownはXSS攻撃から安全ですか?

生のMarkdown変換はXSSに脆弱な場合があります。DOMPurifyを使用するか、パーサーで生HTMLを無効にしてください。ユーザー生成のMarkdownは常にサニタイズしてください。

Markdownから生成されたHTMLにスタイルを適用するには?

Tailwind CSSの@tailwindcss/typographyプラグインのproseクラス、github-markdown-css、またはreact-markdownのカスタムレンダラーコンポーネントを使用します。

まとめ

MarkdownからHTMLへの変換はすべてのWeb開発者にとって基本的なスキルです。無料オンラインツールで即座に変換できます。

無料オンラインツールでMarkdownをHTMLに即座に変換。

𝕏 Twitterin LinkedIn
この記事は役に立ちましたか?

最新情報を受け取る

毎週の開発ヒントと新ツール情報。

スパムなし。いつでも解除可能。

Try These Related Tools

MDMarkdown to HTMLMDMarkdown PreviewJSXHTML to JSXHMHTML to Markdown

Related Articles

GitHub README用Markdownチートシート:テーブル、バッジ、折りたたみセクション

GitHub README究極のMarkdownチートシート。テーブル、バッジ、折りたたみ、タスクリスト、GitHub固有機能の構文。

HTML から JSX へ:React 移行に必要なすべて

HTML を React 用の JSX に変換するための包括的ガイド。className、style オブジェクト、自己閉じタグ、イベントハンドラー、よくある落とし穴を解説。

すべてのWebサイトに必要なメタタグ:HTML メタタグ完全ガイド

SEO、Open Graph、Twitter Cards、セキュリティ、パフォーマンスに必須のHTMLメタタグ。コピペ可能な完全テンプレート付き。