marktoimage

package module
v2.0.0-...-1a793be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 30 Imported by: 0

README

mdrender

一个纯 Go 的 Markdown 转图片示例项目:

  • Markdown 解析基于 github.com/yuin/goldmark
  • 绘图基于 github.com/FloatTech/gg
  • 内置 default 和 github-dark 两套主题,可通过配置切换

项目目标:

  • 尽量遵循 CommonMark 常见语法
  • 通过“两阶段布局 + 一次性绘制”提升性能与稳定性
  • 保持结构简单,便于继续扩展

快速运行:

go run ./cmd

渲染指定 Markdown 文件:

go run ./cmd -in ./example.md -out ./output/example.png

切换到 GitHub 风格深色主题:

go run ./cmd -in ./example.md -out ./output/example-dark.png -theme github-dark

字体说明:

  • v2/font 中已经静态嵌入了一份中文字体,CLI 默认会启用它。
  • 如果留空 -embed-font,程序会回退到内置 Go 字体;英文可以正常渲染,但中文可能无法正常显示。
  • 如果你想覆盖内置字体,也可以显式指定外部字体文件:
go run ./cmd -in ./example.md -out ./output/example.png -font ../cmd/MaokenZhuyuanTi.ttf

如果你在代码里初始化渲染器,也可以直接传入主题名:

import (
	"github.com/lianhong2758/marktoimage/v2"
	"github.com/lianhong2758/marktoimage/v2/font"
)

r, err := marktoimage.New(renderer.Options{
	ThemeName: marktoimage.ThemeGitHubDark,
	Width:     1200,
	Fonts:     [][]byte{font.TTF},
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ThemeNames

func ThemeNames() []string

ThemeNames 返回所有可用的内置主题名,方便给 CLI 或上层配置展示帮助信息。

Types

type FontFamily

type FontFamily string

FontFamily 表示要使用的字体族。 项目直接使用 Go 自带字体字节,这样不依赖系统字体文件。

const (
	FontRegular    FontFamily = "regular"
	FontBold       FontFamily = "bold"
	FontItalic     FontFamily = "italic"
	FontBoldItalic FontFamily = "bolditalic"
	FontMono       FontFamily = "mono"
)

type FontSet

type FontSet struct {
	Regular []byte
}

FontSet 仅保留给旧调用做兼容,新的字体入口统一使用 [][]byte。

type Options

type Options struct {
	Theme     Theme
	ThemeName ThemeName
	Width     int
	Fonts     [][]byte
	BaseDir   string
}

Options 是渲染器初始化参数。

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer 负责 Markdown 的解析、布局与绘制。

为了性能更稳定,这里采用“三段式”流程: 1. Markdown AST 转内部结构 2. 根据目标宽度做布局 3. 一次性绘制到图片

func New

func New(opts Options) (*Renderer, error)

New 创建渲染器实例。

func (*Renderer) Render

func (r *Renderer) Render(markdown []byte) (image.Image, error)

Render 把 Markdown 渲染为内存中的图片对象。

func (*Renderer) RenderToFile

func (r *Renderer) RenderToFile(markdown []byte, outputPath string) error

RenderToFile 直接把 Markdown 输出为 PNG 文件。

type Theme

type Theme struct {
	Width              int
	Padding            float64
	BlockGap           float64
	ParagraphGap       float64
	ListItemGap        float64
	QuotePaddingX      float64
	QuotePaddingY      float64
	QuoteBarWidth      float64
	ListIndent         float64
	CodePaddingX       float64
	CodePaddingY       float64
	InlineCodePaddingX float64
	InlineCodePaddingY float64
	RuleSpacing        float64
	BaseFontSize       float64
	BaseLineHeight     float64
	CodeLineHeight     float64
	HeadingScale       [6]float64
	Radius             float64

	Background color.Color
	Text       color.Color
	MutedText  color.Color
	Link       color.Color
	Rule       color.Color
	QuoteBar   color.Color
	QuoteFill  color.Color
	CodeFill   color.Color
	CodeText   color.Color
	InlineCode color.Color
}

Theme 定义整体视觉风格、间距和颜色方案。

func DefaultTheme

func DefaultTheme(width int) Theme

DefaultTheme 保留旧接口,默认返回浅色主题。

func GitHubDarkTheme

func GitHubDarkTheme(width int) Theme

GitHubDarkTheme 提供偏 GitHub Markdown 的深色主题。

func ThemeByName

func ThemeByName(name ThemeName, width int) Theme

ThemeByName 根据内置主题名构造完整主题配置。

type ThemeName

type ThemeName string

ThemeName 是内置主题的配置名。

const (
	ThemeDefault    ThemeName = "default"
	ThemeGitHubDark ThemeName = "github-dark"
)

func ParseThemeName

func ParseThemeName(name string) (ThemeName, error)

ParseThemeName 把外部输入规范化成受支持的主题名。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址