forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add linter check that fails for nondescript link labels (withastro#2128)
Co-authored-by: Ben Myers <ben@benmyers.dev>
- Loading branch information
Showing
5 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { DomUtils } from 'htmlparser2'; | ||
import kleur from 'kleur'; | ||
import { dedentMd } from '../../output.mjs'; | ||
import { CheckBase, CheckHtmlPageContext } from '../base/check'; | ||
import { IssueType } from '../base/issue'; | ||
|
||
/** List of labels that are insufficiently descriptive for a link. */ | ||
const blocklist = new Set(['read more', 'click here', 'here', 'more']); | ||
|
||
export class GoodLabels extends CheckBase { | ||
private static readonly BadLabel = new IssueType({ | ||
title: 'link(s) with vague or nondescript labels', | ||
prefix: kleur.gray(`[${kleur.yellow().bold('lbl')}]`), | ||
sortOrder: 1000, | ||
}); | ||
|
||
checkHtmlPage(context: CheckHtmlPageContext) { | ||
// Skip all checks if the current page is a language fallback page | ||
// to avoid reporting duplicates for all missing translations | ||
if (context.page.isLanguageFallback) return; | ||
|
||
context.page.anchors.forEach((anchor) => { | ||
const linkLabel = DomUtils.innerText(anchor) | ||
.replace(/[\n\s\t]+/g, ' ') | ||
.trim(); | ||
|
||
if (!blocklist.has(linkLabel.toLowerCase())) return; | ||
|
||
context.report({ | ||
type: GoodLabels.BadLabel, | ||
linkHref: anchor.attribs.href, | ||
annotationText: dedentMd`Found link label “${linkLabel}”. | ||
Please use descriptive accessible text for labels instead | ||
of short undescriptive labels like “here” or “read more”.`, | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters