markdownlint 前端元数据实战:front-matter-title-spaces 场景测试与 Front Matter 标题识别机制
markdownlint 前端元数据实战:front-matter-title-spaces 场景测试与 Front Matter 标题识别机制
本指南以 markdownlint 仓库中的 front-matter-title-spaces.md 场景测试为切入点,系统讲解 Markdownlint 如何识别 YAML/TOML/JSON 前端元数据(Front Matter)、如何从其中提取 title 属性以豁免 MD041/MD025 标题规则,以及 markdownlint-configure-file 注释如何按文件定制规则配置。读完你将掌握 Front Matter 标题匹配的默认正则规则、front_matter_title 参数的自定义方法与真实修复效果。
场景样本:带空格的 Front Matter 标题
仓库 test 目录存放了大量 Markdown 场景测试样本,每个 .md 文件都对应 markdownlint-test-scenarios.mjs 中的一个自动化用例:该脚本读取 ./test 下所有 .md 文件,逐个执行 lint({ "files": <a href="https://link.gitcode.com/i/af9396ca1f7d7565d5ca643a4a760373" target="_blank">file] }),再调用 applyFixes 得到修复结果,并与 [markdownlint-test-scenarios.mjs.snapshot 中的快照逐项比对。
其中 front-matter-title-spaces.md 完整内容如下(注意第 1 行 --- 后带一个空格、第 6 行 --- 后带三个空格):
---
layout: post
title: "Welcome to Jekyll!"
date: 2015-11-17 16:16:01 -0600
categories: jekyll update
---
Front matter from [Jekyll documentation](https://jekyllrb.com/docs/posts/#a-typical-post).
<!-- markdownlint-configure-file {
"line-length": false
} -->
这是一个典型的 Jekyll 博客文章头块:layout 指定模板、title 声明文章标题、date 与 categories 提供发布元数据。它同时验证了三件事:
- 带尾随空格/前缀空格的 Front Matter 分隔符(
---与---)仍能被正确识别为元数据块,而非普通内容; title键与冒号之间有多个空格(title: "Welcome to Jekyll!")时,Front Matter 标题依然能被匹配,从而让文档免于 MD041/MD025 的标题类告警;markdownlint-configure-file注释可以针对该文件关闭line-length(MD013)。
Front Matter 的默认识别正则:YAML / TOML / JSON 三合一
Markdownlint 识别 Front Matter 的默认正则定义在 helpers/helpers.cjs(frontMatterRe),同一正则也在 README.md 的 options.frontMatter 中作为默认值公开:
/((^---[^\S\r\n\u2028\u2029]*$[\s\S]+?^---\s*)|
(^\+\+\+[^\S\r\n\u2028\u2029]*$[\s\S]+?^(\+\+\+|\.\.\.)\s*)|
(^\{[^\S\r\n\u2028\u2029]*$[\s\S]+?^\}\s*))(\r\n|\r|\n|$)/m
从正则结构可以推断出它对三种元数据格式的兼容策略:
- YAML:以
^---开头、以^---结束。由于[^\S\r\n\u2028\u2029]*允许分隔符行中携带水平空白,---与---这类写法仍可命中; - TOML:以
^+++开头,可用+++或...结束; - JSON:以
^{开头、以^}结束,适配{ ... }形式的 JSON 头块。
正则整体还要求元数据块后跟一个换行((\r\n|\r|\n|$)),且使用 m 多行标志按行锚定。仓库中 front-matter-title-toml-spaces.md 就是对应的 TOML 变体样本:
+++
date = "2017-01-26T22:17:00+02:00"
title = "My document title and heading"
+++
Some plain text here.
如果项目使用其他形式的元数据头,可以通过 options.frontMatter 传入自定义 RegExp;传 null 则完全关闭 Front Matter 识别。
title 提取的核心函数:frontMatterHasTitle
frontMatterLines 是 Markdownlint 在解析阶段从文件头剥离 Front Matter 后得到的行数组(见 lib/markdownlint.mjs 的 removeFrontMatter 实现),而判断其中是否包含标题的核心逻辑在 helpers/helpers.cjs:
function frontMatterHasTitle(frontMatterLines, frontMatterTitlePattern) {
const ignoreFrontMatter =
(frontMatterTitlePattern !== undefined) && !frontMatterTitlePattern;
const frontMatterTitleRe =
new RegExp(
String(frontMatterTitlePattern || "^\\s*\"?title\"?\\s*[:=]"),
"i"
);
return !ignoreFrontMatter &&
frontMatterLines.some((line) => frontMatterTitleRe.test(line));
}
要点如下:
- 默认匹配正则为
^\s*"?title"?\s*[:=](忽略大小写):允许行首空白、title两侧可有可无的双引号、冒号或等号前后可带空白。这正是样本中title: "Welcome to Jekyll!"(title与冒号之间有两个空格)依然能被识别的原因; - 逐行扫描:只要
frontMatterLines中任意一行命中该正则即视为有标题,因此 YAML 的title: xxx与 TOML 的title = "xxx"两种风格都能覆盖; front_matter_title传""可关闭 Front Matter 标题豁免;传自定义正则则可匹配任意属性名(见下文)。
MD041 与 MD025:Front Matter 标题如何豁免标题规则
frontMatterHasTitle 的调用方是两条标题类规则:
MD041 / first-line-heading(first-line-h1):要求文件首行为一级标题。其实现 lib/md041.mjs 中,规则函数开头即判断 frontMatterHasTitle(params.frontMatterLines, params.config.front_matter_title),若返回 true 则直接跳过后续报错逻辑。也就是说,只要 Front Matter 中有 title,文档正文即使没有 # 一级标题 也不会触发 MD041。该规则还支持 allow_preamble(允许标题前存在目录等文本)与 level(自定义期望的标题级别)参数,详见 doc/md041.md。
MD025 / single-title(single-h1):要求文档只有一个一级标题。其实现 lib/md025.mjs 中,foundFrontMatterTitle 同样来自 frontMatterHasTitle,且注释明确指出“Front matter title counts as a top-level heading if present”——Front Matter 中的 title 会被视作文档的第一个一级标题,正文中再出现的首个一级标题因此不再被当作违规,只有后续重复的一级标题才会被标记。
两条规则的 front_matter_title 参数说明见 doc/md041.md 与 doc/md025.md:
- 默认值:
^\s*title\s*[:=]; - 若要匹配其他属性名(如
subject),传入对应正则即可; - 若要完全禁用 Front Matter 标题豁免,传
""。
快照验证:修复输出与规则豁免的实测结果
front-matter-title-spaces.md 在 markdownlint-test-scenarios.mjs.snapshot 中的快照显示:errors 为空数组,即 lint 结果没有任何告警——这直接印证了带空格写法的 Front Matter 标题确实豁免了 MD041/MD025,同时 line-length 也被 markdownlint-configure-file 注释关闭。
同一快照还记录了修复后的文本:
---
layout: post
title: "Welcome to Jekyll!"
date: 2015-11-17 16:16:01 -0600
categories: jekyll update
---
Front matter from [Jekyll documentation](https://jekyllrb.com/docs/posts/#a-typical-post).
<!-- markdownlint-configure-file {
"line-length": false
} -->
可见在该场景中 applyFixes 没有改动任何字符(fixed 与原文一致),属于“本就有意构造的合规样本”。与之形成对照的是仓库中 front-matter-with-dashes.md 等样本会产出 MD010 等真实告警,说明不同 Front Matter 场景样本覆盖了“合规”与“违规”两个方向,共同保证规则行为可回归验证。测试脚本 markdownlint-test-scenarios.mjs 还会用 {MD\d+...} 行内标记机制断言告警行号,并二次 lint 修复结果,确保修复后不再残留可修复错误。
markdownlint-configure-file:按文件覆盖规则配置
样本末尾的注释:
<!-- markdownlint-configure-file {
"line-length": false
} -->
是 Markdownlint 的文件级内联配置语法,官方说明见 README.md。其行为要点:
- 注释内容是 JSON,
"line-length": false即对整个文件关闭 MD013; - 配置对整个文件生效,与注释所在位置无关;同文件出现多个此类注释时按自上而下的顺序依次应用;
- 默认只支持 JSON,可通过
options.configParsers扩展其他格式(如 YAML、TOML); - 与
markdownlint-disable系列注释不同,markdownlint-configure-file修改的是规则参数而非开关状态,因此适合“这个文件行宽不受限”这类场景化定制。
在本样本中,正文引用链接一行明显超过默认 80 字符的 line_length(见 doc/md013.md 的参数说明),若没有这行注释,MD013 必然告警;关闭后整个文件即可零告警通过,与快照中 errors: [] 完全对应。
实战总结
- 识别范围:默认 Front Matter 正则同时支持 YAML(
---)、TOML(+++/...)与 JSON({...}),且允许分隔符行带空白; - 标题豁免:
frontMatterHasTitle用^\s*"?title"?\s*[:=]逐行匹配 Front Matter,命中即豁免 MD041/MD025;如需自定义属性名用front_matter_title正则,传""可关闭; - 文件级配置:
markdownlint-configure-file注释以 JSON 按文件覆盖规则参数,是处理长链接、特殊排版文件的轻量手段; - 验证方式:所有样本均由 markdownlint-test-scenarios.mjs 自动 lint 并比对快照,仓库中 front-matter-title-spaces.md 与 front-matter-title-toml-spaces.md 是可直接复用的参考样本。