Adding custom rule for alt text 40-150 characters (#37421)

Co-authored-by: Rachael Sewell <rachmari@github.com>
This commit is contained in:
Grace Park 2023-06-02 13:10:15 -07:00 коммит произвёл GitHub
Родитель 90d5e0db80
Коммит 01ed475082
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 222 добавлений и 0 удалений

140
package-lock.json сгенерированный
Просмотреть файл

@ -144,6 +144,8 @@
"kill-port": "2.0.1",
"lint-staged": "^13.0.1",
"make-promises-safe": "^5.1.0",
"markdownlint": "^0.28.2",
"markdownlint-rule-helpers": "^0.19.0",
"mdast-util-gfm-table": "^1.0.7",
"micromark-extension-gfm-table": "^1.0.5",
"mkdirp": "^3.0.0",
@ -12791,6 +12793,15 @@
"dev": true,
"license": "MIT"
},
"node_modules/linkify-it": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-4.0.1.tgz",
"integrity": "sha512-C7bfi1UZmoj8+PQx22XyeXCuBlokoyWQL5pWSP+EI6nzRylyThouddufc2c1NDIcP9k5agmN9fLpA7VNJfIiqw==",
"dev": true,
"dependencies": {
"uc.micro": "^1.0.1"
}
},
"node_modules/lint-staged": {
"version": "13.0.3",
"dev": true,
@ -13238,6 +13249,34 @@
"url": "https://github.com/sponsors/wooorm"
}
},
"node_modules/markdown-it": {
"version": "13.0.1",
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-13.0.1.tgz",
"integrity": "sha512-lTlxriVoy2criHP0JKRhO2VDG9c2ypWCsT237eDiLqi09rmbKoUetyGHq2uOIRoRS//kfoJckS0eUzzkDR+k2Q==",
"dev": true,
"dependencies": {
"argparse": "^2.0.1",
"entities": "~3.0.1",
"linkify-it": "^4.0.1",
"mdurl": "^1.0.1",
"uc.micro": "^1.0.5"
},
"bin": {
"markdown-it": "bin/markdown-it.js"
}
},
"node_modules/markdown-it/node_modules/entities": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/entities/-/entities-3.0.1.tgz",
"integrity": "sha512-WiyBqoomrwMdFG1e0kqvASYfnlb0lp8M5o5Fw2OFq1hNZxxcNk8Ik0Xm7LxzBhuidnZB/UtBqVCgUz3kBOP51Q==",
"dev": true,
"engines": {
"node": ">=0.12"
},
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/markdown-table": {
"version": "3.0.1",
"license": "MIT",
@ -13251,6 +13290,40 @@
"resolved": "https://registry.npmjs.org/markdown-table-ts/-/markdown-table-ts-1.0.3.tgz",
"integrity": "sha512-lYrp7FXmBqpmGmsEF92WnSukdgYvLm15FPIODZOx9+3nobkxJxjBYcszqZf5VqTjBtISPSNC7zjU9o3zwpL6AQ=="
},
"node_modules/markdownlint": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/markdownlint/-/markdownlint-0.28.2.tgz",
"integrity": "sha512-yYaQXoKKPV1zgrFsyAuZPEQoe+JrY9GDag9ObKpk09twx4OCU5lut+0/kZPrQ3W7w82SmgKhd7D8m34aG1unVw==",
"dev": true,
"dependencies": {
"markdown-it": "13.0.1",
"markdownlint-micromark": "0.1.2"
},
"engines": {
"node": ">=14.18.0"
}
},
"node_modules/markdownlint-micromark": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/markdownlint-micromark/-/markdownlint-micromark-0.1.2.tgz",
"integrity": "sha512-jRxlQg8KpOfM2IbCL9RXM8ZiYWz2rv6DlZAnGv8ASJQpUh6byTBnEsbuMZ6T2/uIgntyf7SKg/mEaEBo1164fQ==",
"dev": true,
"engines": {
"node": ">=14.18.0"
}
},
"node_modules/markdownlint-rule-helpers": {
"version": "0.19.0",
"resolved": "https://registry.npmjs.org/markdownlint-rule-helpers/-/markdownlint-rule-helpers-0.19.0.tgz",
"integrity": "sha512-hXx0ZsXtNwyeFg02ImUhGjBmv/MDILKtcgpU+ur3WazJBv6dxaVzq9GEjVb+u3zoFE9+PURRIttVUIPA43dLpQ==",
"dev": true,
"dependencies": {
"markdownlint-micromark": "0.1.2"
},
"engines": {
"node": ">=14.18.0"
}
},
"node_modules/matcher-collection": {
"version": "2.0.1",
"license": "ISC",
@ -17693,6 +17766,12 @@
"node": ">=12.20"
}
},
"node_modules/uc.micro": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz",
"integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==",
"dev": true
},
"node_modules/unbox-primitive": {
"version": "1.0.2",
"dev": true,
@ -27396,6 +27475,15 @@
"version": "1.2.4",
"dev": true
},
"linkify-it": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/linkify-it/-/linkify-it-4.0.1.tgz",
"integrity": "sha512-C7bfi1UZmoj8+PQx22XyeXCuBlokoyWQL5pWSP+EI6nzRylyThouddufc2c1NDIcP9k5agmN9fLpA7VNJfIiqw==",
"dev": true,
"requires": {
"uc.micro": "^1.0.1"
}
},
"lint-staged": {
"version": "13.0.3",
"dev": true,
@ -27659,6 +27747,27 @@
"markdown-escapes": {
"version": "1.0.4"
},
"markdown-it": {
"version": "13.0.1",
"resolved": "https://registry.npmjs.org/markdown-it/-/markdown-it-13.0.1.tgz",
"integrity": "sha512-lTlxriVoy2criHP0JKRhO2VDG9c2ypWCsT237eDiLqi09rmbKoUetyGHq2uOIRoRS//kfoJckS0eUzzkDR+k2Q==",
"dev": true,
"requires": {
"argparse": "^2.0.1",
"entities": "~3.0.1",
"linkify-it": "^4.0.1",
"mdurl": "^1.0.1",
"uc.micro": "^1.0.5"
},
"dependencies": {
"entities": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/entities/-/entities-3.0.1.tgz",
"integrity": "sha512-WiyBqoomrwMdFG1e0kqvASYfnlb0lp8M5o5Fw2OFq1hNZxxcNk8Ik0Xm7LxzBhuidnZB/UtBqVCgUz3kBOP51Q==",
"dev": true
}
}
},
"markdown-table": {
"version": "3.0.1"
},
@ -27667,6 +27776,31 @@
"resolved": "https://registry.npmjs.org/markdown-table-ts/-/markdown-table-ts-1.0.3.tgz",
"integrity": "sha512-lYrp7FXmBqpmGmsEF92WnSukdgYvLm15FPIODZOx9+3nobkxJxjBYcszqZf5VqTjBtISPSNC7zjU9o3zwpL6AQ=="
},
"markdownlint": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/markdownlint/-/markdownlint-0.28.2.tgz",
"integrity": "sha512-yYaQXoKKPV1zgrFsyAuZPEQoe+JrY9GDag9ObKpk09twx4OCU5lut+0/kZPrQ3W7w82SmgKhd7D8m34aG1unVw==",
"dev": true,
"requires": {
"markdown-it": "13.0.1",
"markdownlint-micromark": "0.1.2"
}
},
"markdownlint-micromark": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/markdownlint-micromark/-/markdownlint-micromark-0.1.2.tgz",
"integrity": "sha512-jRxlQg8KpOfM2IbCL9RXM8ZiYWz2rv6DlZAnGv8ASJQpUh6byTBnEsbuMZ6T2/uIgntyf7SKg/mEaEBo1164fQ==",
"dev": true
},
"markdownlint-rule-helpers": {
"version": "0.19.0",
"resolved": "https://registry.npmjs.org/markdownlint-rule-helpers/-/markdownlint-rule-helpers-0.19.0.tgz",
"integrity": "sha512-hXx0ZsXtNwyeFg02ImUhGjBmv/MDILKtcgpU+ur3WazJBv6dxaVzq9GEjVb+u3zoFE9+PURRIttVUIPA43dLpQ==",
"dev": true,
"requires": {
"markdownlint-micromark": "0.1.2"
}
},
"matcher-collection": {
"version": "2.0.1",
"requires": {
@ -30363,6 +30497,12 @@
"integrity": "sha512-wVORMBGO/FAs/++blGNeAVdbNKtIh1rbBL2EyQ1+J9lClJ93KiiKe8PmFIVdXhHcyv44SL9oglmfeSsndo0jRw==",
"dev": true
},
"uc.micro": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/uc.micro/-/uc.micro-1.0.6.tgz",
"integrity": "sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==",
"dev": true
},
"unbox-primitive": {
"version": "1.0.2",
"dev": true,

Просмотреть файл

@ -146,6 +146,8 @@
"kill-port": "2.0.1",
"lint-staged": "^13.0.1",
"make-promises-safe": "^5.1.0",
"markdownlint": "^0.28.2",
"markdownlint-rule-helpers": "^0.19.0",
"mdast-util-gfm-table": "^1.0.7",
"micromark-extension-gfm-table": "^1.0.5",
"mkdirp": "^3.0.0",

Просмотреть файл

@ -0,0 +1,25 @@
import { addError, forEachInlineChild } from 'markdownlint-rule-helpers'
import renderContent from '../../../../lib/render-content/index.js'
export const incorrectAltTextLength = {
names: ['MD111', 'incorrect-alt-text-length'],
description: 'Images alternate text should be between 40-150 characters',
tags: ['accessibility', 'images'],
function: function MD111(params, onError) {
forEachInlineChild(params, 'image', async function forToken(token) {
let renderedString = token.content
if (token.content.includes('{%') || token.content.includes('{{')) {
const context = { currentLanguage: 'en' }
renderedString = await renderContent.liquid.parseAndRender(token.content, context)
}
if (renderedString.length < 40 || renderedString.length > 150) {
addError(
onError,
token.lineNumber,
`The alt text: ${renderedString}, is ${renderedString.length} characters long`
)
}
})
},
}

Просмотреть файл

@ -0,0 +1,55 @@
#!/usr/bin/env node
import { program } from 'commander'
import markdownlint from 'markdownlint'
import { applyFixes } from 'markdownlint-rule-helpers'
import { readFile, writeFile } from 'fs/promises'
import walkFiles from '../../../script/helpers/walk-files.js'
import { incorrectAltTextLength } from '../lib/linting-rules/image-alt-text-length.js'
program
.description('Run markdownlint.')
.option('-p, --path <path>', 'Specify filepaths to include.')
.option('-f, --fix', 'Fix linting errors.')
.parse(process.argv)
const { path, fix } = program.opts()
main()
async function main() {
const start = Date.now()
const config = {
default: false,
MD001: true,
// MD041: { level: 2 },
MD111: true,
}
const files = walkFiles(path, ['.md'], { includeBasePath: true })
const options = {
files,
customRules: [incorrectAltTextLength],
config,
}
const result = markdownlint.sync(options)
if (fix) {
for (const file of files) {
const content = await readFile(file, 'utf8')
const applied = applyFixes(content, result[file])
await writeFile(file, applied)
}
}
Object.keys(result).forEach((key) => {
if (result[key].length > 0) {
console.log(key, result[key])
}
})
const end = Date.now()
console.log(`markdownlint finished in ${(end - start) / 1000} s`)
}