Skip to content

Commit e8e120c

Browse files
authored
Bringing in forInlineEachChild and filterTokens from markdownlint-rule-helpers (#50895)
1 parent 3540a08 commit e8e120c

17 files changed

+47
-25
lines changed

package-lock.json

+8-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
"lodash-es": "^4.17.21",
248248
"lowdb": "7.0.1",
249249
"lowlight": "3.1.0",
250+
"markdownlint-rule-helpers": "^0.25.0",
250251
"mdast-util-from-markdown": "^2.0.0",
251252
"mdast-util-to-hast": "^13.1.0",
252253
"mdast-util-to-markdown": "2.1.0",
@@ -329,7 +330,6 @@
329330
"kill-port": "2.0.1",
330331
"lint-staged": "^15.2.2",
331332
"markdownlint": "^0.34.0",
332-
"markdownlint-rule-helpers": "^0.19.0",
333333
"markdownlint-rule-search-replace": "^1.2.0",
334334
"mdast-util-gfm-table": "^2.0.0",
335335
"micromark-extension-gfm-table": "^2.0.0",

src/content-linter/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ See the [custom rules](https://github.com/DavidAnson/markdownlint/blob/main/doc/
5151

5252
### Helper utilities
5353

54-
Markdownlint provides several helper functions. Take a look at the many exports in [markdownlint-rule-helpers](https://github.com/DavidAnson/markdownlint/blob/main/helpers/helpers.js).
54+
Markdownlint provides several helper functions. Take a look at the many exports in [markdownlint-rule-helpers](https://github.com/DavidAnson/markdownlint/blob/main/helpers/helpers.js). Note, this is unsupported and may stop being published to in the future.
5555

5656
We've also written a few of our own:
5757

src/content-linter/lib/helpers/utils.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { addError } from 'markdownlint-rule-helpers'
1+
import { addError, filterTokens } from 'markdownlint-rule-helpers'
22
import matter from 'gray-matter'
33

44
// Adds an error object with details conditionally via the onError callback
55
export function addFixErrorDetail(onError, lineNumber, expected, actual, range, fixInfo) {
66
addError(onError, lineNumber, `Expected: ${expected}`, ` Actual: ${actual}`, range, fixInfo)
77
}
88

9+
export function forEachInlineChild(params, type, handler) {
10+
filterTokens(params, 'inline', (token) => {
11+
for (const child of token.children.filter((c) => c.type === type)) {
12+
handler(child, token)
13+
}
14+
})
15+
}
16+
917
export function getRange(line, content) {
1018
if (content.length === 0) {
1119
// This function assumes that the content is something. If it's an

src/content-linter/lib/linting-rules/code-annotations.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { addError, filterTokens } from 'markdownlint-rule-helpers'
2+
23
import { getFrontmatter } from '../helpers/utils.js'
34

45
export const codeAnnotations = {
56
names: ['GHD007', 'code-annotations'],
67
description:
78
'Code annotations defined in Markdown must contain a specific layout frontmatter property',
89
tags: ['code', 'feature', 'annotate', 'frontmatter'],
10+
parser: 'markdownit',
911
function: (params, onError) => {
1012
filterTokens(params, 'fence', (token) => {
1113
if (!token.info.includes('annotate')) return

src/content-linter/lib/linting-rules/code-fence-line-length.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const codeFenceLineLength = {
44
names: ['GHD030', 'code-fence-line-length'],
55
description: 'Code fence lines should not exceed a maximum length',
66
tags: ['code', 'accessibility'],
7+
parser: 'markdownit',
78
function: (params, onError) => {
89
const MAX_LINE_LENGTH = String(params.config.maxLength || 60)
910
filterTokens(params, 'fence', (token) => {

src/content-linter/lib/linting-rules/image-alt-text-end-punctuation.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { forEachInlineChild } from 'markdownlint-rule-helpers'
2-
31
import {
42
addFixErrorDetail,
3+
forEachInlineChild,
54
getRange,
65
isStringQuoted,
76
isStringPunctuated,
@@ -11,6 +10,7 @@ export const imageAltTextEndPunctuation = {
1110
names: ['GHD032', 'image-alt-text-end-punctuation'],
1211
description: 'Alternate text for images should end with punctuation',
1312
tags: ['accessibility', 'images'],
13+
parser: 'markdownit',
1414
function: (params, onError) => {
1515
forEachInlineChild(params, 'image', function forToken(token) {
1616
const imageAltText = token.content.trim()

src/content-linter/lib/linting-rules/image-alt-text-exclude-start-words.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { addError, forEachInlineChild } from 'markdownlint-rule-helpers'
1+
import { addError } from 'markdownlint-rule-helpers'
22

3-
import { getRange } from '../helpers/utils.js'
3+
import { forEachInlineChild, getRange } from '../helpers/utils.js'
44

55
const excludeStartWords = ['image', 'graphic']
66

@@ -12,6 +12,7 @@ export const imageAltTextExcludeStartWords = {
1212
names: ['GHD031', 'image-alt-text-exclude-words'],
1313
description: 'Alternate text for images should not begin with words like "image" or "graphic"',
1414
tags: ['accessibility', 'images'],
15+
parser: 'markdownit',
1516
function: (params, onError) => {
1617
forEachInlineChild(params, 'image', function forToken(token) {
1718
const imageAltText = token.content.trim()

src/content-linter/lib/linting-rules/image-alt-text-length.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { addError, forEachInlineChild } from 'markdownlint-rule-helpers'
1+
import { addError } from 'markdownlint-rule-helpers'
22

33
import { liquid } from '#src/content-render/index.js'
44
import { allVersions } from '#src/versions/lib/all-versions.js'
5-
import { getRange } from '../helpers/utils.js'
5+
import { forEachInlineChild, getRange } from '../helpers/utils.js'
66

77
export const incorrectAltTextLength = {
88
names: ['GHD033', 'incorrect-alt-text-length'],
99
description: 'Images alternate text should be between 40-150 characters',
1010
tags: ['accessibility', 'images'],
11+
parser: 'markdownit',
1112
asynchronous: true,
1213
function: (params, onError) => {
1314
forEachInlineChild(params, 'image', async function forToken(token) {

src/content-linter/lib/linting-rules/image-file-kebab-case.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { forEachInlineChild } from 'markdownlint-rule-helpers'
2-
3-
import { addFixErrorDetail } from '../helpers/utils.js'
1+
import { addFixErrorDetail, forEachInlineChild } from '../helpers/utils.js'
42

53
export const imageFileKebabCase = {
64
names: ['GHD004', 'image-file-kebab-case'],
75
description: 'Image file names must use kebab-case',
86
tags: ['images'],
7+
parser: 'markdownit',
98
function: (params, onError) => {
109
forEachInlineChild(params, 'image', async function forToken(token) {
1110
const imageFileName = token.attrs[0][1].split('/').pop().split('.')[0]

src/content-linter/lib/linting-rules/image-no-gif.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { addError, forEachInlineChild } from 'markdownlint-rule-helpers'
1+
import { addError } from 'markdownlint-rule-helpers'
2+
3+
import { forEachInlineChild } from '../helpers/utils.js'
24

35
export const imageNoGif = {
46
names: ['GHD036', 'image-no-gif'],
57
description:
68
'Image must not be a gif, styleguide reference: contributing/style-guide-and-content-model/style-guide.md#images',
79
tags: ['images'],
10+
parser: 'markdownit',
811
function: (params, onError) => {
912
forEachInlineChild(params, 'image', function forToken(token) {
1013
const imageFileName = token.attrs[0][1]

src/content-linter/lib/linting-rules/internal-links-no-lang.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { filterTokens } from 'markdownlint-rule-helpers'
2+
23
import { addFixErrorDetail, getRange } from '../helpers/utils.js'
34
import { allLanguageKeys } from '#src/languages/lib/languages.js'
45

56
export const internalLinksNoLang = {
67
names: ['GHD002', 'internal-links-no-lang'],
78
description: 'Internal links must not have a hardcoded language code',
89
tags: ['links', 'url'],
10+
parser: 'markdownit',
911
function: (params, onError) => {
1012
filterTokens(params, 'inline', (token) => {
1113
for (const child of token.children) {

src/content-linter/lib/linting-rules/internal-links-old-version.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { filterTokens, addError } from 'markdownlint-rule-helpers'
1+
import { addError, filterTokens } from 'markdownlint-rule-helpers'
2+
23
import { getRange } from '../helpers/utils.js'
34

45
export const internalLinksOldVersion = {
56
names: ['GHD006', 'internal-links-old-version'],
67
description: 'Internal links must not have a hardcoded version using old versioning syntax',
78
tags: ['links', 'url', 'versioning'],
9+
parser: 'markdownit',
810
function: (params, onError) => {
911
filterTokens(params, 'inline', (token) => {
1012
if (

src/content-linter/lib/linting-rules/internal-links-slash.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const internalLinksSlash = {
66
names: ['GHD003', 'internal-links-slash'],
77
description: 'Internal links must start with a /',
88
tags: ['links', 'url'],
9+
parser: 'markdownit',
910
function: (params, onError) => {
1011
filterTokens(params, 'inline', (token) => {
1112
for (const child of token.children) {

src/content-linter/lib/linting-rules/link-punctuation.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { filterTokens, addError } from 'markdownlint-rule-helpers'
1+
import { addError, filterTokens } from 'markdownlint-rule-helpers'
22

3-
import { getRange, isStringQuoted, doesStringEndWithPeriod } from '../helpers/utils.js'
3+
import { doesStringEndWithPeriod, getRange, isStringQuoted } from '../helpers/utils.js'
44

55
export const linkPunctuation = {
66
names: ['GHD001', 'link-punctuation'],
77
description: 'Internal link titles must not contain punctuation',
88
tags: ['links', 'url'],
9+
parser: 'markdownit',
910
function: (params, onError) => {
1011
filterTokens(params, 'inline', (token) => {
1112
const { children, line } = token

src/content-linter/lib/linting-rules/yaml-scheduled-jobs.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import yaml from 'js-yaml'
2+
import { addError, filterTokens } from 'markdownlint-rule-helpers'
23

34
import { liquid } from '#src/content-render/index.js'
45
import { allVersions } from '#src/versions/lib/all-versions.js'
5-
import { addError, filterTokens } from 'markdownlint-rule-helpers'
66

77
const scheduledYamlJobs = []
88

@@ -11,6 +11,7 @@ export const yamlScheduledJobs = {
1111
description:
1212
'YAML snippets that include scheduled workflows must not run on the hour and must be unique',
1313
tags: ['feature', 'actions'],
14+
parser: 'markdownit',
1415
asynchronous: true,
1516
function: (params, onError) => {
1617
filterTokens(params, 'fence', async (token) => {

src/content-linter/scripts/lint-content.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import fs from 'fs'
33
import path from 'path'
44
import { execSync } from 'child_process'
5-
65
import { program, Option } from 'commander'
76
import markdownlint from 'markdownlint'
87
import { applyFixes } from 'markdownlint-rule-helpers'

0 commit comments

Comments
 (0)