Skip to content

Commit 93a1667

Browse files
rachmariPeter Bengtsson
andauthored
replace annotations jest test with linter rule (#43673)
Co-authored-by: Peter Bengtsson <[email protected]>
1 parent cbf8079 commit 93a1667

File tree

5 files changed

+63
-15
lines changed

5 files changed

+63
-15
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { addError, filterTokens } from 'markdownlint-rule-helpers'
2+
import matter from 'gray-matter'
3+
4+
export const annotateFrontmatter = {
5+
names: ['GHD040', 'annotate-frontmatter'],
6+
description:
7+
'Annotations defined in Markdown must contain a specific layout frontmatter property.',
8+
tags: ['code', 'annotate'],
9+
information: new URL('https://github.com/github/docs/blob/main/src/content-linter/README.md'),
10+
function: function GHD040(params, onError) {
11+
filterTokens(params, 'fence', (token) => {
12+
if (!token.info.includes('annotate')) return
13+
const fm = matter(params.frontMatterLines.join('\n')).data
14+
if (fm.layout && fm.layout === 'inline') return
15+
16+
addError(
17+
onError,
18+
token.lineNumber,
19+
`When you include 'annotate' in a code fence, you must also include 'layout: inline' in the frontmatter.`,
20+
token.line,
21+
[1, token.line.length],
22+
null, // No fix possible
23+
)
24+
})
25+
},
26+
}

src/content-linter/lib/linting-rules/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { internalLinksOldVersion } from './internal-links-old-version.js'
1616
import { hardcodedDataVariable } from './hardcoded-data-variable.js'
1717
import { githubOwnedActionReferences } from './github-owned-action-references.js'
1818
import { liquidQuotedConditionalArg } from './liquid-quoted-conditional-arg.js'
19+
import { annotateFrontmatter } from './annotate-frontmatter.js'
1920

2021
const noDefaultAltText = markdownlintGitHub.find((elem) =>
2122
elem.names.includes('no-default-alt-text'),
@@ -44,5 +45,6 @@ export const gitHubDocsMarkdownlint = {
4445
hardcodedDataVariable,
4546
githubOwnedActionReferences,
4647
liquidQuotedConditionalArg,
48+
annotateFrontmatter,
4749
],
4850
}

src/content-linter/style/github-docs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ const githubDocsConfig = {
7272
severity: 'error',
7373
'partial-markdown-files': true,
7474
},
75+
'annotate-frontmatter': {
76+
// GH040
77+
severity: 'error',
78+
'partial-markdown-files': false,
79+
},
7580
}
7681

7782
const githubMarkdownlintConfig = {

src/content-linter/tests/lint-annotate.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { runRule } from '../../lib/init-test.js'
2+
import { annotateFrontmatter } from '../../lib/linting-rules/annotate-frontmatter.js'
3+
4+
describe(annotateFrontmatter.names.join(' - '), () => {
5+
test('No layout property fails', async () => {
6+
const markdown = ['---', 'title: Title', '---', '```shell annotate', 'hello', '```'].join('\n')
7+
const result = await runRule(annotateFrontmatter, { strings: { markdown } })
8+
const errors = result.markdown
9+
expect(errors.length).toBe(1)
10+
expect(errors[0].lineNumber).toBe(4)
11+
expect(errors[0].errorRange).toEqual([1, 17])
12+
expect(errors[0].fixInfo).toBeNull()
13+
})
14+
test('Incorrect layout property fails', async () => {
15+
const markdown = ['---', 'layout: default', '---', '```shell annotate', 'hello', '```'].join(
16+
'\n',
17+
)
18+
const result = await runRule(annotateFrontmatter, { strings: { markdown } })
19+
const errors = result.markdown
20+
expect(errors.length).toBe(1)
21+
})
22+
test('Correct layout property passes', async () => {
23+
const markdown = ['---', 'layout: inline', '---', '```shell annotate', 'hello', '```'].join(
24+
'\n',
25+
)
26+
const result = await runRule(annotateFrontmatter, { strings: { markdown } })
27+
const errors = result.markdown
28+
expect(errors.length).toBe(0)
29+
})
30+
})

0 commit comments

Comments
 (0)