markdown

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 33 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Markdown 导入

解析链路为 goldmark AST → creator/flow.Document → OFD,不使用独立的 Markdown 排版引擎。

import _ "github.com/zc310/ofd/pkg/converter/import/markdown"

err := converter.Convert(ctx, "markdown", "ofd", input, output)

// 允许下载公网 HTTP(S) 图片并内嵌:
err = converter.Convert(ctx, "markdown", "ofd", input, output,
    converter.WithAllowRemoteResources(true))

input 支持路径、字节和 io.Reader。本地图片和内联 base64 PNG/JPEG 的访问规则见下文; 被拒绝、超限或无法读取的图片记录警告并跳过,保留替代文本。 原始 HTML 不渲染,混在文字中的图片使用替代文本。

图片资源访问策略

  • 路径输入以 Markdown 文件所在目录为基准,只允许该目录及子目录中的相对图片路径。 禁止绝对路径、file: URI 和 ../ 越界;普通图片支持 PNG、JPEG。
  • 目录访问使用 os.Root 限制符号链接解析。允许指向基准目录内的相对符号链接, 越界链接及绝对符号链接均拒绝;检查和打开之间更换链接也不能绕过目录边界。
  • 字节和 io.Reader 输入没有基准目录,默认只允许内联图片,不读取当前工作目录或绝对路径。 默认不下载 HTTP、HTTPS、FTP、协议相对地址等外部资源,其他 URI scheme 也不加载。
  • converter.Convert 配合 converter.WithAllowRemoteResources(true) 时,Markdown 导入会下载 http://、https:// 图片并内嵌;FTP、协议相对地址及其他 scheme 仍不下载。下载受连接 10s、整体 30s 超时和最多 3 次重定向限制,只跟随 http(s) 跳转;失败或超时记录警告并跳过, 保留替代文本。直接调用 markdown.Convert 不下载远程图片。 CLI 可使用 ofd-converter --allow-remote input.md output.ofd 启用相同策略。 为防 SSRF,拨号前解析目标地址并拒绝环回、私网、共享地址段 100.64.0.0/10 (含云元数据 100.100.100.200)、链路本地(含云元数据 169.254.169.254)、 未指定及组播地址;受限地址一律降级跳过。 远程图片直连目标地址,不读取 HTTP_PROXY 等环境代理,避免代理绕过地址校验。
  • 本地文件按文件字节数、base64 图片按解码后原始 PNG/JPEG 字节数限制: 单张最多 16 MiB,每篇累计最多 64 MiB。所有引用共享预算,包括列表、引用块和 Alerts 中的图片,以及启用远程下载后的 http(s) 图片;重复引用及读到的无效图片字节也计入总量。 超限图片跳过,继续处理正文。 读取和 base64 解码均有流式上限,不按声明长度一次性读取或分配完整图片缓冲。

内容与版式

  • 支持六级标题、段落、粗斜体、删除线、链接、嵌套列表、任务标记、引用、分隔线和 GFM 表格。
  • 代码块使用等宽字体和浅灰圆角背景,保留缩进和内部空行;每页/栏一块背景,语言只标在首段。 围栏标注已知语言(如 go、python、js、json、yaml、sh)时,使用 Chroma v3 的 GitHub 浅色主题进行语法高亮; 支持语言别名(如 golang、javascript),跨行注释和字符串保持颜色。 未标注语言、未知语言、缩进代码块及 text/plaintext 默认灰底灰字,不自动猜测语言;分词失败时也退回灰色纯文本。 高亮仅改变文字颜色,不改变代码内容、字号、缩进或分页。行内代码有底色,片段不可拆分,不做语法高亮。 超长代码行按面板宽度自动折行,优先在 Unicode 断点处换行;超长命令、URL 或字符串按字素折行,不裁剪原有空白。
  • 表格使用自动列宽、单元格换行、跨页拆行和重复表头,不自动旋转宽表格。
  • 默认 A4、Material 印刷主题,采用 flow 的标题跟随与孤行控制。图片按比例适配宽度; 超高图片、无法放入版心的单个代码字素,或正文中不可断开的长词/URL 会返回错误,需要调整尺寸、字号或显式换行。
  • 链接目标原样保留;空目标和纯 #锚点 只着色,不生成动作。尚未生成 Markdown 标题锚点 ID。

Chroma v3 的具体依赖版本以根目录 go.mod 为准。

代码块示例(高亮也适用于 Alerts 内的围栏代码):

```go
func main() {
    println("hello", 42)
}
```

```
未标记语言的代码默认灰底灰字。
```

Alerts(GitHub 风格提示)

> [!NOTE]
> 这里是提示正文,可以包含 **粗体**、链接、列表、表格和代码块。

支持 NOTE、TIP、IMPORTANT、WARNING、CAUTION,分别显示 Note/Tip/Important/Warning/Caution 标题及蓝/绿/紫/黄褐/红色左侧色条。 标题前分别显示信息圈、灯泡、提示气泡、警告三角形、八角停止标志。 图标采用内嵌 SVG 原创轮廓,转换为同色 OFD 矢量路径,随标题字号缩放,不依赖图标字体或网络资源。 色条中段平直,顶部和底部向右微弯,呈细括号状;连续段落与表格行之间不重复绘制弧形。 正文保留原有 Markdown 内容,不整段染色;标题尽量与正文开头同页,长提示可跨页/栏,续页保留色条。 图标仅在标题处绘制一次,不在续页重复;类型标题始终写入可提取的文字层。

标记须大写、独占引用块第一行;空提示只显示标题。未知类型、小写标记、转义标记、 行内/围栏代码中的标记及自定义同行标题仍按普通 Markdown 处理。

字体与自定义 flow 选项

默认显式启用 AllowLogicalFonts,维持中文输入无需提供字体文件的用法。 输出只声明逻辑字体,由阅读端回退;本机候选字体仅用于度量,不被嵌入。 相同环境下采用确定性 ZIP 输出,不保证不同机器的排版一致。

直接调用可传一份完整 flow.Options,替代默认页面、主题和字体设置:

opts := flow.DefaultOptions()
opts.Theme = flow.Bootstrap5Theme()
opts.GenerateOutlines = true
err := markdown.Convert(ctx, input, output, opts)
// 按路径写出:markdown.ConvertFile(ctx, mdPath, ofdPath, opts)

此时沿用 flow 的严格字体检查。中文需注册实际中文字体及回退字体;代码另需覆盖内容的等宽字体。 完整配置见 flow 字体说明。 注册导入器的 converter.Convert 保持默认选项;CLI 的 -paper、-landscape 不作用于 Markdown。

公文 front matter

开头 YAML 支持 letterhead、sign、footer、colophon,内容字段沿用现有名称。 存在版头、落款或版记时使用 flow 的 GB/T 9704 基础配置:A4、37/26/35/28mm 页边距、 三号正文及公文装饰;页码仍需 footer.page_number: true 显式开启。 仅设置 footer 时保留普通版式,使用 flow 的页码格式。

letterhead 支持 org、doc_no、signatory、serial_no、security、urgency、 height、org_size、doc_no_size;sign 支持 org、date、size; footer 支持 page_number、size;colophon 支持 cc、issued_by、issued_date、size。 长度为毫米,字号为磅。机关标志默认 32pt;修改显式尺寸后需自行验收公文版式。 首页版头必须能同时容纳标题和正文;单行公文要素过宽会报错。 显式传入严格字体选项时,应使用 flow.GBT9704Options 并注册五类实际公文字体。

取消返回原始 ctx.Err();读取、排版或写出失败时,不应使用部分输出。

Documentation

Overview

Package markdown 为 converter 注册 Markdown→OFD 导入器(输入格式 "markdown")。

该包依赖 creator/flow 与 goldmark,单独成包是为了让只做 OFD→X 的使用方 不必链接这些依赖;需要 Markdown→OFD 时按空白导入启用:

import _ "github.com/zc310/ofd/pkg/converter/import/markdown"

之后即可使用 converter.Convert(ctx, "md", "ofd", ...)。出于安全和确定性考虑, 本地图片限于 Markdown 所在目录内的相对路径;远程图片默认不下载,仅在 converter 通过 WithAllowRemoteResources 显式允许时才下载 http(s) 图片。 越界、无法读取或超出字节预算的图片只在日志中给出警告并跳过。

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(ctx context.Context, input any, output io.Writer, options ...flow.Options) error

Convert 将 Markdown 输入转换为 OFD,input 支持文件路径、[]byte 或 io.Reader。 路径输入只加载文件所在目录内的相对图片,禁止绝对路径和越界符号链接; 字节和 Reader 输入只支持内联图片。本地文件和 base64 图片的解码后原始数据 单张最多 16 MiB,每次引用累计计入每篇 64 MiB 预算;超限图片警告并跳过。 解析和流式排版阶段响应取消。 缺省使用逻辑字体;可传入一份 flow.Options 指定实际字体、主题和页面。 直接调用不下载远程图片;converter.Convert 配合 WithAllowRemoteResources(true) 才会下载 http(s) 图片。

Example
package main

import (
	"bytes"
	"context"
	"fmt"

	markdown "github.com/zc310/ofd/pkg/converter/import/markdown"
)

func main() {
	var output bytes.Buffer
	err := markdown.Convert(context.Background(), []byte("# 标题\n\n正文"), &output)
	fmt.Println(err == nil)
	fmt.Println(bytes.HasPrefix(output.Bytes(), []byte("PK")))
}
Output:
true
true
Example (Alerts)
package main

import (
	"bytes"
	"context"
	"fmt"

	markdown "github.com/zc310/ofd/pkg/converter/import/markdown"
	"github.com/zc310/ofd/pkg/creator/flow"
)

func main() {
	var output bytes.Buffer
	err := markdown.Convert(context.Background(), []byte("> [!NOTE]\n> Useful information.\n"), &output, flow.DefaultOptions())
	fmt.Println(err == nil)
}
Output:
true
Example (CodeHighlight)
package main

import (
	"bytes"
	"context"
	"fmt"

	markdown "github.com/zc310/ofd/pkg/converter/import/markdown"
	"github.com/zc310/ofd/pkg/creator/flow"
)

func main() {
	// 指定语言启用语法高亮;无语言的围栏仍为灰底灰字。
	source := "```go\nfunc main() {\n    println(\"hello\", 42)\n}\n```\n\n```\nplain code\n```\n"
	var output bytes.Buffer
	err := markdown.Convert(context.Background(), []byte(source), &output, flow.DefaultOptions())
	fmt.Println(err == nil)
	fmt.Println(bytes.HasPrefix(output.Bytes(), []byte("PK")))
}
Output:
true
true
Example (FlowOptions)
package main

import (
	"bytes"
	"context"
	"fmt"

	markdown "github.com/zc310/ofd/pkg/converter/import/markdown"
	"github.com/zc310/ofd/pkg/creator/flow"
)

func main() {
	opts := flow.DefaultOptions()
	opts.Theme = flow.Bootstrap5Theme()
	opts.GenerateOutlines = true
	var output bytes.Buffer
	err := markdown.Convert(context.Background(), []byte("# Report\n\nText and `code`."), &output, opts)
	fmt.Println(err == nil)
	fmt.Println(bytes.HasPrefix(output.Bytes(), []byte("PK")))
}
Output:
true
true

func ConvertFile

func ConvertFile(ctx context.Context, mdPath, ofdPath string, options ...flow.Options) (err error)

ConvertFile 是 Convert 的按路径便捷形式。

Types

This section is empty.

Jump to

Keyboard shortcuts

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