Skip to content

Markdown

Parse frontmatter, convert to HTML, and generate table of contents.

3 modules

ModuleDescription
解析 Frontmatter從 Markdown 內容中提取 YAML frontmatter
Markdown 轉 HTML將 Markdown 文字轉換為 HTML
生成目錄從 Markdown 標題生成目錄

Modules

解析 Frontmatter

markdown.parse_frontmatter

從 Markdown 內容中提取 YAML frontmatter

Parameters:

NameTypeRequiredDefaultDescription
textstringYes-包含 frontmatter 的 Markdown 內容

Output:

FieldTypeDescription
frontmatterobject解析後的 frontmatter 字典
contentstring不含 frontmatter 的 Markdown 內容

Example: Parse YAML frontmatter

yaml
text: ---
title: Hello World
date: 2024-01-01
tags:
  - python
  - markdown
---

# Hello World

Content here.

Markdown 轉 HTML

markdown.to_html

將 Markdown 文字轉換為 HTML

Parameters:

NameTypeRequiredDefaultDescription
textstringYes-要轉換的 Markdown 內容
extensionsarrayNo-要啟用的 Markdown 擴充功能(僅用於 markdown 函式庫)

Output:

FieldTypeDescription
htmlstring轉換後的 HTML 內容
word_countnumber輸入文字的字數

Example: Convert markdown to HTML

yaml
text: # Hello

This is **bold** and *italic*.

生成目錄

markdown.toc

從 Markdown 標題生成目錄

Parameters:

NameTypeRequiredDefaultDescription
textstringYes-要提取標題的 Markdown 內容
max_depthnumberNo3要包含的最大標題深度 (1-6)

Output:

FieldTypeDescription
tocarray包含層級、標題和 slug 的標題列表
toc_markdownstring格式化的 Markdown 目錄

Example: Generate TOC from markdown

yaml
text: # Introduction

## Getting Started

### Installation

### Configuration

## Usage

## API Reference
max_depth: 3

Released under the Apache 2.0 License.