home / skills / jimliu / baoyu-skills / baoyu-markdown-to-html

baoyu-markdown-to-html skill

/skills/baoyu-markdown-to-html

This skill converts Markdown to styled HTML tailored for WeChat themes, with code highlighting, math, diagrams, and support for footnotes and alerts.

npx playbooks add skill jimliu/baoyu-skills --skill baoyu-markdown-to-html

Review the files below or copy the command above to add this skill to your agents.

Files (21)
SKILL.md
7.7 KB
---
name: baoyu-markdown-to-html
description: Converts Markdown to styled HTML with WeChat-compatible themes. Supports code highlighting, math, PlantUML, footnotes, alerts, and infographics. Use when user asks for "markdown to html", "convert md to html", "md转html", or needs styled HTML output from markdown.
---

# Markdown to HTML Converter

Converts Markdown files to beautifully styled HTML with inline CSS, optimized for WeChat Official Account and other platforms.

## Script Directory

**Agent Execution**: Determine this SKILL.md directory as `SKILL_DIR`, then use `${SKILL_DIR}/scripts/<name>.ts`.

| Script | Purpose |
|--------|---------|
| `scripts/main.ts` | Main entry point |

## Preferences (EXTEND.md)

Use Bash to check EXTEND.md existence (priority order):

```bash
# Check project-level first
test -f .baoyu-skills/baoyu-markdown-to-html/EXTEND.md && echo "project"

# Then user-level (cross-platform: $HOME works on macOS/Linux/WSL)
test -f "$HOME/.baoyu-skills/baoyu-markdown-to-html/EXTEND.md" && echo "user"
```

┌──────────────────────────────────────────────────────────────┬───────────────────┐
│                             Path                             │     Location      │
├──────────────────────────────────────────────────────────────┼───────────────────┤
│ .baoyu-skills/baoyu-markdown-to-html/EXTEND.md               │ Project directory │
├──────────────────────────────────────────────────────────────┼───────────────────┤
│ $HOME/.baoyu-skills/baoyu-markdown-to-html/EXTEND.md         │ User home         │
└──────────────────────────────────────────────────────────────┴───────────────────┘

┌───────────┬───────────────────────────────────────────────────────────────────────────┐
│  Result   │                                  Action                                   │
├───────────┼───────────────────────────────────────────────────────────────────────────┤
│ Found     │ Read, parse, apply settings                                               │
├───────────┼───────────────────────────────────────────────────────────────────────────┤
│ Not found │ Use defaults                                                              │
└───────────┴───────────────────────────────────────────────────────────────────────────┘

**EXTEND.md Supports**: Default theme | Custom CSS variables | Code block style

## Workflow

### Step 0: Pre-check (Chinese Content)

**Condition**: Only execute if input file contains Chinese text.

**Detection**:
1. Read input markdown file
2. Check if content contains CJK characters (Chinese/Japanese/Korean)
3. If no CJK content → skip to Step 1

**Format Suggestion**:

If CJK content detected AND `baoyu-format-markdown` skill is available:

Use `AskUserQuestion` to ask whether to format first. Formatting can fix:
- Bold markers with punctuation inside causing `**` parse failures
- CJK/English spacing issues

**If user agrees**: Invoke `baoyu-format-markdown` skill to format the file, then use formatted file as input.

**If user declines**: Continue with original file.

### Step 1: Confirm Theme

Before converting, use AskUserQuestion to confirm the theme (unless user already specified):

| Theme | Description |
|-------|-------------|
| `default` (Recommended) | 经典主题 - 传统排版,标题居中带底边,二级标题白字彩底 |
| `grace` | 优雅主题 - 文字阴影,圆角卡片,精致引用块 |
| `simple` | 简洁主题 - 现代极简风,不对称圆角,清爽留白 |

### Step 2: Convert

```bash
npx -y bun ${SKILL_DIR}/scripts/main.ts <markdown_file> --theme <theme>
```

### Step 3: Report Result

Display the output path from JSON result. If backup was created, mention it.

## Usage

```bash
npx -y bun ${SKILL_DIR}/scripts/main.ts <markdown_file> [options]
```

**Options:**

| Option | Description | Default |
|--------|-------------|---------|
| `--theme <name>` | Theme name (default, grace, simple) | default |
| `--title <title>` | Override title from frontmatter | |
| `--keep-title` | Keep the first heading in content | false (removed) |
| `--help` | Show help | |

**Examples:**

```bash
# Basic conversion (uses default theme, removes first heading)
npx -y bun ${SKILL_DIR}/scripts/main.ts article.md

# With specific theme
npx -y bun ${SKILL_DIR}/scripts/main.ts article.md --theme grace

# Keep the first heading in content
npx -y bun ${SKILL_DIR}/scripts/main.ts article.md --keep-title

# Override title
npx -y bun ${SKILL_DIR}/scripts/main.ts article.md --title "My Article"
```

## Output

**File location**: Same directory as input markdown file.
- Input: `/path/to/article.md`
- Output: `/path/to/article.html`

**Conflict handling**: If HTML file already exists, it will be backed up first:
- Backup: `/path/to/article.html.bak-YYYYMMDDHHMMSS`

**JSON output to stdout:**

```json
{
  "title": "Article Title",
  "author": "Author Name",
  "summary": "Article summary...",
  "htmlPath": "/path/to/article.html",
  "backupPath": "/path/to/article.html.bak-20260128180000",
  "contentImages": [
    {
      "placeholder": "MDTOHTMLIMGPH_1",
      "localPath": "/path/to/img.png",
      "originalPath": "imgs/image.png"
    }
  ]
}
```

## Themes

| Theme | Description |
|-------|-------------|
| `default` | 经典主题 - 传统排版,标题居中带底边,二级标题白字彩底 |
| `grace` | 优雅主题 - 文字阴影,圆角卡片,精致引用块 (by @brzhang) |
| `simple` | 简洁主题 - 现代极简风,不对称圆角,清爽留白 (by @okooo5km) |

## Supported Markdown Features

| Feature | Syntax |
|---------|--------|
| Headings | `# H1` to `###### H6` |
| Bold/Italic | `**bold**`, `*italic*` |
| Code blocks | ` ```lang ` with syntax highlighting |
| Inline code | `` `code` `` |
| Tables | GitHub-flavored markdown tables |
| Images | `![alt](src)` |
| Links | `[text](url)` with footnote references |
| Blockquotes | `> quote` |
| Lists | `-` unordered, `1.` ordered |
| Alerts | `> [!NOTE]`, `> [!WARNING]`, etc. |
| Footnotes | `[^1]` references |
| Ruby text | `{base|annotation}` |
| Mermaid | ` ```mermaid ` diagrams |
| PlantUML | ` ```plantuml ` diagrams |

## Frontmatter

Supports YAML frontmatter for metadata:

```yaml
---
title: Article Title
author: Author Name
description: Article summary
---
```

If no title is found, extracts from first H1/H2 heading or uses filename.

## Extension Support

Custom configurations via EXTEND.md. See **Preferences** section for paths and supported options.

Overview

This skill converts Markdown into styled, self-contained HTML optimized for WeChat Official Account and similar publishing targets. It produces inline CSS themes, handles code highlighting, math, PlantUML, footnotes, alerts, and infographics, and returns a JSON summary including output path and any backup created. Use it when you need ready-to-publish HTML from Markdown with consistent styling and extended features.

How this skill works

The converter reads the input Markdown, optionally runs a CJK-aware formatting step if Chinese (or other CJK) text is detected, and asks to confirm a theme when not specified. It renders extended Markdown features (syntax-highlighted code blocks, math, PlantUML, Mermaid, footnotes, alerts, ruby text, tables, and images) and writes a single HTML file next to the source. When an output file would overwrite an existing HTML, the tool creates a timestamped backup and prints a JSON result with metadata, paths, and extracted image placeholders.

When to use it

  • You need to convert Markdown to WeChat-compatible HTML with inline styling.
  • You want syntax-highlighted code, math rendering, or PlantUML/Mermaid diagrams in the output.
  • You need footnotes, alerts, ruby text, or advanced Markdown features preserved in HTML.
  • You want a single self-contained HTML file with theme options and predictable output path.
  • You need automatic backup of existing HTML files and a machine-readable JSON report.

Best practices

  • Choose a theme (default, grace, simple) before converting to match your target platform style.
  • If content contains Chinese or other CJK characters, allow the optional CJK-aware formatter to avoid spacing and emphasis issues.
  • Include YAML frontmatter (title, author, description) to control metadata and page title extraction.
  • Keep images referenced with relative paths; the tool reports placeholders and local paths in its JSON output.
  • Use the --keep-title option if you want the first heading retained in the content rather than used as the page title.

Example use cases

  • Convert a blog draft to WeChat-ready HTML with the default theme and inline CSS for direct upload.
  • Generate a standalone HTML export of technical notes with highlighted code blocks and math equations.
  • Produce documentation pages that include PlantUML or Mermaid diagrams rendered into the final HTML.
  • Batch-convert Markdown files while preserving existing HTML by creating timestamped backups automatically.
  • Create small infographic-style posts using the grace or simple themes for a cleaner presentation.

FAQ

What output location will the HTML file use?

The HTML is written to the same directory as the Markdown file, with the same base name and .html extension.

What happens if an HTML file already exists?

An existing HTML file is backed up with a .bak-YYYYMMDDHHMMSS suffix before writing the new output; the JSON report includes the backup path.

How do I control the page title?

Supply a title in YAML frontmatter or use the --title option. If none is provided, the tool extracts from the first H1/H2 or falls back to the filename.