关于 GitHub 文档内容检查工具
我们的内容检查工具在 Markdown 内容中强制执行风格指南规则。
检查工具使用 markdownlint 作为框架来执行检查、报告缺陷,并在可能的情况下自动修复内容。该框架灵活地运行特定规则,提供描述性的错误信息,并修复错误。GitHub 文档内容检查工具使用了多个现有的 markdownlint 规则以及额外的自定义规则来检查我们 content 和 data 目录中的 Markdown 内容。我们的自定义规则实现了在 markdownlint 框架中尚未提供或专属于 GitHub 文档内容的检查。规则同时检查 Markdown 和 Liquid 的语法。
运行 GitHub 文档内容检查工具
GitHub 文档内容检查工具会在 pre-commit 时自动运行,但您也可以手动运行它。
在 pre-commit 时自动运行检查工具
当您在本地编写内容并使用命令行提交文件时,这些已暂存的文件会自动由内容检查工具进行检查。警告和错误都会被报告,但只有错误会阻止提交完成。
如果报告了任何错误,您的提交将不会完成。您需要修复报告的错误、重新添加已更改的文件,然后再次提交。如果报告了错误,必须修复这些错误,以防止内容出现违反 GitHub 文档风格指南的错误。若报告了警告,您可以选择修复或不修复。
在本地编写内容时,您可以使用命令行自动修复若干规则。如果想自动修复可修复的错误,请参阅 自动修复可修复的错误。
如果您在 GitHub UI 中编辑文件,无法自动修复错误或在提交时运行检查工具,但如果内容违反了严重程度为 error 的任何规则,您将收到 CI 失败。
手动运行检查工具
在已暂存和已更改的文件上运行检查工具
使用以下命令在本地对已暂存和已更改的文件运行检查工具。它将输出 warning 和 error 严重程度的缺陷。
npm run lint-content
在已暂存和已更改的文件上运行检查工具,仅报告错误
使用以下命令在本地对已暂存和已更改的文件运行检查工具,并仅报告 error 严重程度的缺陷。
npm run lint-content -- --errors
在特定文件或目录上运行检查工具
使用以下命令在本地对特定文件或目录运行检查工具。使用空格分隔多个路径。您可以在同一命令中同时包含文件和目录。
npm run lint-content -- \ --paths content/FILENAME.md content/DIRECTORY
npm run lint-content -- \
--paths content/FILENAME.md content/DIRECTORY
自动修复可修复的错误
如果错误的描述中包含 fixable: true,您可以使用以下命令自动修复它们。
仅运行此命令以修复已暂存和已更改的文件
npm run lint-content -- --fix
运行此命令以修复特定文件或目录
npm run lint-content -- \
--fix --paths content/FILENAME.md content/DIRECTORY
运行特定的一组检查规则
使用以下命令运行一个或多个特定的检查规则。以下示例运行 heading-increment 和 code-fence-line-length 规则。将 heading-increment code-fence-line-length 替换为您想运行的一个或多个检查别名。要查看可传递给此选项的检查规则列表,请运行 npm run lint-content -- --help。您可以使用检查规则的短名称(例如 MD001)或长名称(例如 heading-increment)。
在所有已暂存和已更改的文件上运行指定的检查规则
npm run lint-content -- \
--rules heading-increment code-fence-line-length
在特定文件或目录上运行指定的检查规则
npm run lint-content -- \
--rules heading-increment code-fence-line-length \
--path content/FILENAME.md content/DIRECTORY
绕过提交钩子
如果检查工具捕获到您未引入的错误,您可以在提交更改时使用 --no-verify 选项来绕过 git 提交钩子。
git commit -m 'MESSAGE' --no-verify
显示内容检查工具脚本的帮助菜单
npm run lint-content -- --help
检查规则
每条规则都在 src/content-linter/style 目录下的文件中配置,规则的严重程度也在此定义。
在将更改合并到 main 分支之前,必须解决错误。警告应当处理,但不会阻止更改合并到 main 分支。大多数规则最终会升级为错误,一旦内容不再存在警告违规。
| 规则 ID | 规则名称 | 描述 | 严重程度 | 标签 |
|---|---|---|---|---|
| MD001 | heading-increment | 标题级别一次只能递增一级 | error | headings |
| MD011 | no-reversed-links | 反向链接语法 | error | links |
| MD014 | commands-show-output | 在命令前使用美元符号但未显示输出 | error | code |
| MD018 | no-missing-space-atx | ATX 样式标题后缺少空格 | error | headings, atx, spaces |
| MD019 | no-multiple-space-atx | ATX 样式标题后有多个空格 | error | headings, atx, spaces |
| MD023 | heading-start-left | 标题必须在行首开始 | error | headings, spaces |
| MD027 | no-multiple-space-blockquote | 引用符号后有多个空格 | error | blockquote, whitespace, indentation |
| MD029 | ol-prefix | 有序列表项前缀 | error | ol |
| MD030 | list-marker-space | 列表标记后面的空格 | error | ol, ul, whitespace |
| MD031 | blanks-around-fences | 代码块前后应有空行 | error | code, blank_lines |
| MD037 | no-space-in-emphasis | 强调标记内部的空格 | error | whitespace, emphasis |
| MD039 | no-space-in-links | 链接文本内部的空格 | error | whitespace, links |
| MD040 | fenced-code-language | 代码块应指定语言 | error | code, language |
| MD042 | no-empty-links | 禁止空链接 | error | links |
| MD050 | strong-style | 粗体样式 | error | emphasis |
| GH001 | no-default-alt-text | 图像应具有有意义的替代文本(alt 文本) | error | accessibility, images |
| GH002 | no-generic-link-text | 避免使用通用链接文本,如 了解更多 或 点击此处 | error | accessibility, links |
| GHD001 | link-punctuation | 内部链接标题不得包含标点符号 | error | links, url |
| GHD002 | internal-links-no-lang | 内部链接不得硬编码语言代码 | error | links, url |
| GHD003 | internal-links-slash | 内部链接必须以 / 开头 | error | links, url |
| GHD004 | image-file-kebab-case | 图像文件名必须使用 kebab-case(短横线小写) | error | images |
| GHD005 | hardcoded-data-variable | 包含 "personal access token" 的字符串应改用产品变量 | error | single-source |
| GHD006 | internal-links-old-version | 内部链接不得使用旧的版本语法硬编码版本号 | error | links, url, versioning |
| GHD007 | code-annotations | Markdown 中定义的代码注释必须包含特定的 layout frontmatter 属性 | error | code, feature, annotate, frontmatter |
| GHD008 | early-access-references | 非 Early Access 的文件不应引用 early-access 或 early-access 文件 | error | feature, early-access |
| GHD009 | frontmatter-early-access-references | 非 Early Access 的文件不应在 frontmatter 中引用 early-access | error | frontmatter, feature, early-access |
| GHD010 | frontmatter-hidden-docs | 具有 frontmatter 属性 hidden 的文章只能位于特定产品中 | error | frontmatter, feature, early-access |
| GHD012 | frontmatter-schema | Frontmatter 必须符合 schema | error | frontmatter, schema |
| GHD013 | github-owned-action-references | GitHub 所有的 Action 引用不应硬编码 | error | feature, actions |
| GHD014 | liquid-data-references-defined | 在内容中发现 Liquid 数据或缩进数据引用,但它们没有值或不存在于 data 目录中 | error | liquid |
| GHD015 | liquid-data-tag-format | Liquid 数据或缩进数据引用标签必须格式正确,并且参数数量和空格符合要求 | error | liquid, format |
| GHD016 | liquid-quoted-conditional-arg | Liquid 条件标签不应对条件参数加引号 | error | liquid, format |
| GHD017 | frontmatter-liquid-syntax | Frontmatter 属性必须使用有效的 Liquid 语法 | error | liquid, frontmatter |
| GHD018 | liquid-syntax | Markdown 内容必须使用有效的 Liquid 语法 | error | liquid |
| GHD019 | liquid-if-tags | 当参数是有效版本时,应使用 Liquid ifversion 标签而不是 if 标签 | error | liquid, versioning |
| GHD020 | liquid-ifversion-tags | Liquid ifversion 标签的参数应为有效的版本名称 | error | liquid, versioning |
| GHD021 | yaml-scheduled-jobs | 包含计划工作流的 YAML 代码段不得在整点运行,并且必须唯一 | error | feature, actions |
| GHD022 | liquid-ifversion-versions | Liquid 的 ifversion、elsif 和 else 标签应有效且不包含不受支持的版本。 | error | liquid, versioning |
| GHD031 | image-alt-text-exclude-words | 图像的替代文本不应以 "image" 或 "graphic" 等词开头 | error | accessibility, images |
| GHD032 | image-alt-text-end-punctuation | 图像的替代文本应以标点符号结束 | error | accessibility, images |
| GHD033 | incorrect-alt-text-length | 图像的替代文本应为 40-150 个字符之间 | error | accessibility, images |
| GHD034 | frontmatter-curly-quotes | Frontmatter 中的标题和简介不应包含花体引号 | error | frontmatter, format |
| GHD036 | image-no-gif | 图像不得为 GIF,参见样式指南:contributing/style-guide-and-content-model/style-guide.md#images | error | images |
| GHD038 | expired-content | 已过期的内容必须进行整改。 | warning | expired |
| GHD039 | expiring-soon | 即将过期的内容应主动处理。 | warning | expired |
| GHD040 | table-liquid-versioning | 表格必须使用正确的 Liquid 版本化格式 | error | tables |
| GHD041 | third-party-action-pinning | 使用第三方 Action 的代码示例必须始终固定到完整的提交 SHA | error | feature, actions |
| GHD042 | liquid-tag-whitespace | Liquid 标签应在开始和结束时各有一个空格。Liquid 标签参数之间只能有一个空格分隔。 | error | liquid, format |
| GHD043 | link-quotation | 内部链接标题不得被引号包裹 | error | links, url |
| GHD045 | code-annotation-comment-spacing | 注释块中的代码注释后必须恰好有一个空格 | error | code, comments, annotate, spacing |
| GHD046 | outdated-release-phase-terminology | 过时的发布阶段术语应替换为当前 GitHub 术语 | error | terminology, consistency, release-phases |
| GHD047 | table-column-integrity | 表格所有行的列数必须保持一致 | error | tables, accessibility, formatting |
| GHD051 | frontmatter-versions-whitespace | 版本的 frontmatter 不应包含多余的空格 | error | frontmatter, versions |
| GHD054 | third-party-actions-reusable | 使用第三方 Action 的代码示例必须包含可复用的免责声明 | error | actions, reusable, third-party |
| GHD056 | frontmatter-landing-carousels | 仅登录页可以使用轮播图,且不得有重复文章,所有文章必须存在 | error | frontmatter, landing, carousels |
| GHD057 | ctas-schema | CTA URL 必须符合 schema | error | ctas, schema, urls |
| GHD058 | journey-tracks-liquid | 旅程跟踪属性必须使用有效的 Liquid 语法 | error | frontmatter, journey-tracks, liquid |
| GHD059 | journey-tracks-guide-path-exists | 旅程跟踪指南路径必须引用已存在的内容文件 | error | frontmatter, journey-tracks |
| GHD060 | journey-tracks-unique-ids | 旅程跟踪 ID 在同一页面内必须唯一 | error | frontmatter, journey-tracks, unique-ids |
| GHD061 | frontmatter-hero-image | Hero 图像路径必须是绝对路径、无扩展名,并指向 /assets/images/banner-images/ 中有效的图像 | error | frontmatter, images |
| GHD062 | frontmatter-intro-links | introLinks 键必须是 data/ui.yml 中 product_landing 下定义的有效键 | error | frontmatter, single-source |
| GHD063 | frontmatter-children | 子页面的 frontmatter 路径必须存在。支持相对路径和跨产品包含的绝对 /content/ 路径。 | error | frontmatter, children |
| GHD065 | frontmatter-content-type | content-type 目录中的内容文件必须具有与父目录匹配的 contentType frontmatter 属性。 | error | frontmatter, content-type |
| GHD066 | frontmatter-docs-team-metrics | 路径中包含 docsTeamMetrics 值的文章必须在其 docsTeamMetrics frontmatter 属性中包含该值。 | error | frontmatter, docs-team-metrics |
| search-replace | 已弃用的 liquid 语法:octicon- | 使用的 octicon liquid 语法已弃用。请改用以下格式 octicon "<octicon-name>" aria-label="<Octicon aria label>" | error | |
| search-replace | 已弃用的 liquid 语法:site.data | 捕获已弃用的 liquid 数据语法的出现。 | error | |
| search-replace | developer-domain | 捕获 developer.github.com 域名的出现。 | error | |
| search-replace | docs-domain | 捕获 docs.github.com 域名的出现。 | error | |
| search-replace | help-domain | 捕获 help.github.com 域名的出现。 | error | |
| search-replace | todocs-placeholder | 捕获 TODOCS 占位符的出现。 | error |
检查规则的语法
某些检查规则会根据您在文章中添加的 HTML 注释返回警告或错误。
过期和已过期内容的语法
规则 GHD038 和 GHD039 检查已手动设定过期日期的内容。在指定日期前 14 天,内容检查工具会返回警告,提示内容即将过期。从指定日期起,内容检查工具会返回警告并标记内容需整改。
您可以通过在内容外包裹包含过期日期的 HTML 标签来添加过期日期,格式如下:<!-- expires yyyy-mm-dd --> <!-- end expires yyyy-mm-dd -->
使用
This content does not expire.
<!-- expires 2022-01-28 -->
This content expires on January 28, 2022.
<!-- end expires 2022-01-28 -->
This content also does not expire.
注意,如果您将过期标签放在 HTML table 元素中,请确保标签包裹整个行而不是仅单元格。例如
<!-- expires 2024-06-28 -->
<tr>
<td>
macOS
</td>
<td>
The <code>macos-11</code> label is closing down and will no longer be available after 28 June 2024.
</td>
</tr>
<!-- end expires 2024-06-28 -->
抑制检查规则
在极少数情况下,您可能需要记录违反一个或多个检查规则的内容。此时,您可以通过在 Markdown 文件中添加注释来抑制规则。您可以禁用所有规则或特定规则。尽量只禁用尽可能少的规则。您可以对整个文件、Markdown 文件的某个区段、特定行或下一行禁用规则。
例如,如果您在撰写包含正则表达式 (^|/)[Cc]+odespace/(检查反向链接语法)的文章,它会触发检查反向链接的 MD011 规则。您可以通过添加以下注释在该行禁用 MD011 规则。
(^|/)[Cc]+odespace/ <!-- markdownlint-disable-line MD011 -->
如果您要忽略的行位于代码块中,您可以通过在代码块前后添加以下注释来忽略它。
<!-- markdownlint-disable MD011 -->
```
(^|/)[Cc]+odespace/
```
<!-- markdownlint-enable MD011 -->
您可以使用这些注释来启用或禁用规则。
| 注释 | 效果 |
|---|---|
<!-- markdownlint-disable --> | 禁用所有规则 |
<!-- markdownlint-enable --> | 启用所有规则 |
<!-- markdownlint-disable-line --> | 为当前行禁用所有规则 |
<!-- markdownlint-disable-next-line --> | 为下一行禁用所有规则 |
<!-- markdownlint-disable RULE-ONE RULE-TWO --> | |
<!-- markdownlint-enable RULE-ONE RULE-TWO --> | 通过名称启用一个或多个规则 |
<!-- markdownlint-disable-line RULE-NAME --> | 为当前行禁用一个或多个规则 |
<!-- markdownlint-disable-next-line RULE-NAME --> | 为下一行禁用一个或多个规则 |