feat: add eslint plugin no-html-links (#8156)

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
Co-authored-by: Viktor Malmedal <viktor.malmedal@eniro.com>
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
Co-authored-by: Sébastien Lorber <slorber@users.noreply.github.com>
This commit is contained in:
Viktor Malmedal 2022-12-14 18:28:29 +01:00 committed by GitHub
parent 81f30dd495
commit 4a448773b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 291 additions and 67 deletions

1
.eslintrc.js vendored
View file

@ -374,6 +374,7 @@ module.exports = {
// locals must be justified with a disable comment. // locals must be justified with a disable comment.
'@typescript-eslint/no-unused-vars': [ERROR, {ignoreRestSiblings: true}], '@typescript-eslint/no-unused-vars': [ERROR, {ignoreRestSiblings: true}],
'@typescript-eslint/prefer-optional-chain': ERROR, '@typescript-eslint/prefer-optional-chain': ERROR,
'@docusaurus/no-html-links': ERROR,
'@docusaurus/no-untranslated-text': [ '@docusaurus/no-untranslated-text': [
WARNING, WARNING,
{ {

View file

@ -8,22 +8,19 @@
import React from 'react'; import React from 'react';
import Translate from '@docusaurus/Translate'; import Translate from '@docusaurus/Translate';
import {ThemeClassNames} from '@docusaurus/theme-common'; import {ThemeClassNames} from '@docusaurus/theme-common';
import Link from '@docusaurus/Link';
import IconEdit from '@theme/Icon/Edit'; import IconEdit from '@theme/Icon/Edit';
import type {Props} from '@theme/EditThisPage'; import type {Props} from '@theme/EditThisPage';
export default function EditThisPage({editUrl}: Props): JSX.Element { export default function EditThisPage({editUrl}: Props): JSX.Element {
return ( return (
<a <Link to={editUrl} className={ThemeClassNames.common.editThisPage}>
href={editUrl}
target="_blank"
rel="noreferrer noopener"
className={ThemeClassNames.common.editThisPage}>
<IconEdit /> <IconEdit />
<Translate <Translate
id="theme.common.editThisPage" id="theme.common.editThisPage"
description="The link label to edit the current page"> description="The link label to edit the current page">
Edit this page Edit this page
</Translate> </Translate>
</a> </Link>
); );
} }

View file

@ -9,6 +9,7 @@ import React from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import {translate} from '@docusaurus/Translate'; import {translate} from '@docusaurus/Translate';
import {useThemeConfig} from '@docusaurus/theme-common'; import {useThemeConfig} from '@docusaurus/theme-common';
import Link from '@docusaurus/Link';
import type {Props} from '@theme/Heading'; import type {Props} from '@theme/Heading';
import styles from './styles.module.css'; import styles from './styles.module.css';
@ -34,16 +35,16 @@ export default function Heading({as: As, id, ...props}: Props): JSX.Element {
)} )}
id={id}> id={id}>
{props.children} {props.children}
<a <Link
className="hash-link" className="hash-link"
href={`#${id}`} to={`#${id}`}
title={translate({ title={translate({
id: 'theme.common.headingLinkTitle', id: 'theme.common.headingLinkTitle',
message: 'Direct link to heading', message: 'Direct link to heading',
description: 'Title for link to heading', description: 'Title for link to heading',
})}> })}>
&#8203; &#8203;
</a> </Link>
</As> </As>
); );
} }

View file

@ -7,7 +7,6 @@
import React from 'react'; import React from 'react';
import {SkipToContentLink} from '@docusaurus/theme-common'; import {SkipToContentLink} from '@docusaurus/theme-common';
import styles from './styles.module.css'; import styles from './styles.module.css';
export default function SkipToContent(): JSX.Element { export default function SkipToContent(): JSX.Element {

View file

@ -6,6 +6,7 @@
*/ */
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link';
import type {Props} from '@theme/TOCItems/Tree'; import type {Props} from '@theme/TOCItems/Tree';
// Recursive component rendering the toc tree // Recursive component rendering the toc tree
@ -22,12 +23,10 @@ function TOCItemTree({
<ul className={isChild ? undefined : className}> <ul className={isChild ? undefined : className}>
{toc.map((heading) => ( {toc.map((heading) => (
<li key={heading.id}> <li key={heading.id}>
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */} <Link
<a to={`#${heading.id}`}
href={`#${heading.id}`}
className={linkClassName ?? undefined} className={linkClassName ?? undefined}
// Developer provided the HTML, so assume it's safe. // Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{__html: heading.value}} dangerouslySetInnerHTML={{__html: heading.value}}
/> />
<TOCItemTree <TOCItemTree

View file

@ -90,6 +90,7 @@ export function SkipToContentLink(props: SkipToContentLinkProps): JSX.Element {
ref={containerRef} ref={containerRef}
role="region" role="region"
aria-label={DefaultSkipToContentLabel}> aria-label={DefaultSkipToContentLabel}>
{/* eslint-disable-next-line @docusaurus/no-html-links */}
<a <a
{...props} {...props}
// Note this is a fallback href in case JS is disabled // Note this is a fallback href in case JS is disabled

View file

@ -426,10 +426,8 @@ function SearchPageContent(): JSX.Element {
'text--right', 'text--right',
styles.searchLogoColumn, styles.searchLogoColumn,
)}> )}>
<a <Link
target="_blank" to="https://www.algolia.com/"
rel="noopener noreferrer"
href="https://www.algolia.com/"
aria-label={translate({ aria-label={translate({
id: 'theme.SearchPage.algoliaLabel', id: 'theme.SearchPage.algoliaLabel',
message: 'Search by Algolia', message: 'Search by Algolia',
@ -451,7 +449,7 @@ function SearchPageContent(): JSX.Element {
/> />
</g> </g>
</svg> </svg>
</a> </Link>
</div> </div>
</div> </div>

View file

@ -148,7 +148,7 @@ function Link(
} }
return isRegularHtmlLink ? ( return isRegularHtmlLink ? (
// eslint-disable-next-line jsx-a11y/anchor-has-content // eslint-disable-next-line jsx-a11y/anchor-has-content, @docusaurus/no-html-links
<a <a
ref={innerRef} ref={innerRef}
href={targetLink} href={targetLink}

View file

@ -14,6 +14,7 @@ export = {
plugins: ['@docusaurus'], plugins: ['@docusaurus'],
rules: { rules: {
'@docusaurus/string-literal-i18n-messages': 'error', '@docusaurus/string-literal-i18n-messages': 'error',
'@docusaurus/no-html-links': 'warn',
}, },
}, },
all: { all: {
@ -21,6 +22,7 @@ export = {
rules: { rules: {
'@docusaurus/string-literal-i18n-messages': 'error', '@docusaurus/string-literal-i18n-messages': 'error',
'@docusaurus/no-untranslated-text': 'warn', '@docusaurus/no-untranslated-text': 'warn',
'@docusaurus/no-html-links': 'warn',
}, },
}, },
}, },

View file

@ -0,0 +1,90 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import rule from '../no-html-links';
import {RuleTester} from './testUtils';
const errorsJSX = [{messageId: 'link'}] as const;
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run('prefer-docusaurus-link', rule, {
valid: [
{
code: '<Link to="/test">test</Link>',
},
{
code: '<Link to="https://twitter.com/docusaurus">Twitter</Link>',
},
{
code: '<a href="https://twitter.com/docusaurus">Twitter</a>',
options: [{ignoreFullyResolved: true}],
},
{
code: '<a href={`https://twitter.com/docusaurus`}>Twitter</a>',
options: [{ignoreFullyResolved: true}],
},
{
code: '<a href="mailto:viktor@malmedal.dev">Contact</a> ',
options: [{ignoreFullyResolved: true}],
},
{
code: '<a href="tel:123456789">Call</a>',
options: [{ignoreFullyResolved: true}],
},
],
invalid: [
{
code: '<a href="/test">test</a>',
errors: errorsJSX,
},
{
code: '<a href="https://twitter.com/docusaurus" target="_blank">test</a>',
errors: errorsJSX,
},
{
code: '<a href="https://twitter.com/docusaurus" target="_blank" rel="noopener noreferrer">test</a>',
errors: errorsJSX,
},
{
code: '<a href="mailto:viktor@malmedal.dev">Contact</a> ',
errors: errorsJSX,
},
{
code: '<a href="tel:123456789">Call</a>',
errors: errorsJSX,
},
{
code: '<a href={``}>Twitter</a>',
errors: errorsJSX,
},
{
code: '<a href={`https://www.twitter.com/docusaurus`}>Twitter</a>',
errors: errorsJSX,
},
{
code: '<a href="www.twitter.com/docusaurus">Twitter</a>',
options: [{ignoreFullyResolved: true}],
errors: errorsJSX,
},
{
// TODO we might want to make this test pass
// Can template literals be statically pre-evaluated? (Babel can do it)
// eslint-disable-next-line no-template-curly-in-string
code: '<a href={`https://twitter.com/${"docu" + "saurus"} ${"rex"}`}>Twitter</a>',
options: [{ignoreFullyResolved: true}],
errors: errorsJSX,
},
],
});

View file

@ -5,10 +5,12 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import noHtmlLinks from './no-html-links';
import noUntranslatedText from './no-untranslated-text'; import noUntranslatedText from './no-untranslated-text';
import stringLiteralI18nMessages from './string-literal-i18n-messages'; import stringLiteralI18nMessages from './string-literal-i18n-messages';
export default { export default {
'no-untranslated-text': noUntranslatedText, 'no-untranslated-text': noUntranslatedText,
'string-literal-i18n-messages': stringLiteralI18nMessages, 'string-literal-i18n-messages': stringLiteralI18nMessages,
'no-html-links': noHtmlLinks,
}; };

View file

@ -0,0 +1,103 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {createRule} from '../util';
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree';
const docsUrl = 'https://docusaurus.io/docs/docusaurus-core#link';
type Options = [
{
ignoreFullyResolved: boolean;
},
];
type MessageIds = 'link';
function isFullyResolvedUrl(urlString: string): boolean {
try {
// href gets coerced to a string when it gets rendered anyway
const url = new URL(String(urlString));
if (url.protocol) {
return true;
}
} catch (e) {}
return false;
}
export default createRule<Options, MessageIds>({
name: 'no-html-links',
meta: {
type: 'problem',
docs: {
description: 'enforce using Docusaurus Link component instead of <a> tag',
recommended: false,
},
schema: [
{
type: 'object',
properties: {
ignoreFullyResolved: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
link: `Do not use an \`<a>\` element to navigate. Use the \`<Link />\` component from \`@docusaurus/Link\` instead. See: ${docsUrl}`,
},
},
defaultOptions: [
{
ignoreFullyResolved: false,
},
],
create(context, [options]) {
const {ignoreFullyResolved} = options;
return {
JSXOpeningElement(node) {
if ((node.name as TSESTree.JSXIdentifier).name !== 'a') {
return;
}
if (ignoreFullyResolved) {
const hrefAttr = node.attributes.find(
(attr): attr is TSESTree.JSXAttribute =>
attr.type === 'JSXAttribute' && attr.name.name === 'href',
);
if (hrefAttr?.value?.type === 'Literal') {
if (isFullyResolvedUrl(String(hrefAttr.value.value))) {
return;
}
}
if (hrefAttr?.value?.type === 'JSXExpressionContainer') {
const container: TSESTree.JSXExpressionContainer = hrefAttr.value;
const {expression} = container;
if (expression.type === 'TemplateLiteral') {
// Simple static string template literals
if (
expression.expressions.length === 0 &&
expression.quasis.length === 1 &&
expression.quasis[0]?.type === 'TemplateElement' &&
isFullyResolvedUrl(String(expression.quasis[0].value.raw))
) {
return;
}
// TODO add more complex TemplateLiteral cases here
}
}
}
context.report({node, messageId: 'link'});
},
};
},
});

View file

@ -6,28 +6,17 @@
*/ */
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link';
import Layout from '@theme/Layout'; import Layout from '@theme/Layout';
// Repro for hydration issue https://github.com/facebook/docusaurus/issues/5617 // Repro for hydration issue https://github.com/facebook/docusaurus/issues/5617
function BuggyText() { function BuggyText() {
return ( return (
<span> <span>
Built using the{' '} Built using the <Link to="https://www.electronjs.org/">Electron</Link> ,
<a href="https://www.electronjs.org/" target="_blank" rel="noreferrer"> based on <Link to="https://www.chromium.org/">Chromium</Link>, and written
Electron using <Link to="https://www.typescriptlang.org/">TypeScript</Link> ,
</a>{' '} Xplorer promises you an unprecedented experience.
, based on{' '}
<a href="https://www.chromium.org/" target="_blank" rel="noreferrer">
Chromium
</a>
, and written using{' '}
<a
href="https://www.typescriptlang.org/"
target="_blank"
rel="noreferrer">
TypeScript
</a>{' '}
, Xplorer promises you an unprecedented experience.
</span> </span>
); );
} }

View file

@ -52,6 +52,7 @@ For more fine-grained control, you can also enable the plugin manually and confi
| --- | --- | --- | | --- | --- | --- |
| [`@docusaurus/no-untranslated-text`](./no-untranslated-text.md) | Enforce text labels in JSX to be wrapped by translate calls | | | [`@docusaurus/no-untranslated-text`](./no-untranslated-text.md) | Enforce text labels in JSX to be wrapped by translate calls | |
| [`@docusaurus/string-literal-i18n-messages`](./string-literal-i18n-messages.md) | Enforce translate APIs to be called on plain text labels | ✅ | | [`@docusaurus/string-literal-i18n-messages`](./string-literal-i18n-messages.md) | Enforce translate APIs to be called on plain text labels | ✅ |
| [`@docusaurus/no-html-links`](./no-html-links.md) | Ensures @docusaurus/Link is used instead of `<a>` tags | ✅ |
✅ = recommended ✅ = recommended

View file

@ -0,0 +1,45 @@
---
slug: /api/misc/@docusaurus/eslint-plugin/no-html-links
---
# no-html-links
Ensure that the Docusaurus [`<Link>`](../../../docusaurus-core.md#link) component is used instead of `<a>` tags.
The `<Link>` component has prefetching and preloading built-in. It also does build-time broken link detection, and helps Docusaurus understand your site's structure better.
## Rule Details {#details}
Examples of **incorrect** code for this rule:
```html
<a href="/page">go to page!</a>
<a href="https://twitter.com/docusaurus" target="_blank">Twitter</a>
```
Examples of **correct** code for this rule:
```js
import Link from '@docusaurus/Link'
<Link to="/page">go to page!</Link>
<Link to="https://twitter.com/docusaurus">Twitter</Link>
```
## Rule Configuration {#configuration}
Accepted fields:
```mdx-code-block
<APITable>
```
| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `ignoreFullyResolved` | `boolean` | `false` | Set to true will not report any `<a>` tags with absolute URLs including a protocol. |
```mdx-code-block
</APITable>
```

View file

@ -6,6 +6,7 @@
*/ */
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link';
export default function HackerNewsIcon({ export default function HackerNewsIcon({
size = 54, size = 54,
@ -13,10 +14,8 @@ export default function HackerNewsIcon({
size?: number; size?: number;
}): JSX.Element { }): JSX.Element {
return ( return (
<a <Link
href="https://news.ycombinator.com/item?id=32303052" to="https://news.ycombinator.com/item?id=32303052"
target="_blank"
rel="noreferrer"
style={{display: 'block', width: size, height: size}}> style={{display: 'block', width: size, height: size}}>
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
@ -30,6 +29,6 @@ export default function HackerNewsIcon({
d="M23 32h2v-6l5.5-10h-2.1L24 24.1 19.6 16h-2.1L23 26z" d="M23 32h2v-6l5.5-10h-2.1L24 24.1 19.6 16h-2.1L23 26z"
/> />
</svg> </svg>
</a> </Link>
); );
} }

View file

@ -7,6 +7,7 @@
import type {ComponentProps} from 'react'; import type {ComponentProps} from 'react';
import React from 'react'; import React from 'react';
import Link from '@docusaurus/Link';
export default function ProductHuntCard({ export default function ProductHuntCard({
className, className,
@ -16,10 +17,8 @@ export default function ProductHuntCard({
style?: ComponentProps<'a'>['style']; style?: ComponentProps<'a'>['style'];
}): JSX.Element { }): JSX.Element {
return ( return (
<a <Link
href="https://www.producthunt.com/posts/docusaurus-2-0?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-docusaurus-2-0" to="https://www.producthunt.com/posts/docusaurus-2-0?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-docusaurus-2-0"
target="_blank"
rel="noreferrer"
className={className} className={className}
style={{display: 'block', width: 250, height: 54, ...style}}> style={{display: 'block', width: 250, height: 54, ...style}}>
<img <img
@ -29,6 +28,6 @@ export default function ProductHuntCard({
width={250} width={250}
height={54} height={54}
/> />
</a> </Link>
); );
} }

View file

@ -53,14 +53,14 @@ function TeamProfileCard({
<div className="card__footer"> <div className="card__footer">
<div className="button-group button-group--block"> <div className="button-group button-group--block">
{githubUrl && ( {githubUrl && (
<a className="button button--secondary" href={githubUrl}> <Link className="button button--secondary" href={githubUrl}>
GitHub GitHub
</a> </Link>
)} )}
{twitterUrl && ( {twitterUrl && (
<a className="button button--secondary" href={twitterUrl}> <Link className="button button--secondary" href={twitterUrl}>
Twitter Twitter
</a> </Link>
)} )}
</div> </div>
</div> </div>

View file

@ -9,6 +9,7 @@ import React, {type ReactNode} from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import Link from '@docusaurus/Link';
import styles from './styles.module.css'; import styles from './styles.module.css';
export interface Props { export interface Props {
@ -50,9 +51,9 @@ export default function Tweet({
<div className={clsx('card__body', styles.tweet)}>{content}</div> <div className={clsx('card__body', styles.tweet)}>{content}</div>
<div className="card__footer"> <div className="card__footer">
<a className={clsx(styles.tweetMeta, styles.tweetDate)} href={url}> <Link className={clsx(styles.tweetMeta, styles.tweetDate)} to={url}>
{date} {date}
</a> </Link>
</div> </div>
</div> </div>
); );

View file

@ -9,6 +9,7 @@ import React, {type ReactNode} from 'react';
import clsx from 'clsx'; import clsx from 'clsx';
import Link from '@docusaurus/Link';
import styles from './styles.module.css'; import styles from './styles.module.css';
export interface Props { export interface Props {
@ -31,12 +32,10 @@ export default function TweetQuote({
return ( return (
<figure className={styles.tweetQuote}> <figure className={styles.tweetQuote}>
<blockquote> <blockquote>
<a href={url} target="_blank" rel="noreferrer nofollow"> <Link to={url}>{children}</Link>
{children}
</a>
</blockquote> </blockquote>
<figcaption> <figcaption>
<a href={profileUrl} target="_blank" rel="noreferrer nofollow"> <Link to={profileUrl} rel="nofollow">
<div className="avatar"> <div className="avatar">
<img <img
alt={name} alt={name}
@ -53,7 +52,7 @@ export default function TweetQuote({
</small> </small>
</div> </div>
</div> </div>
</a> </Link>
</figcaption> </figcaption>
</figure> </figure>
); );

View file

@ -15,6 +15,7 @@ import React, {
import {useDocsPreferredVersion} from '@docusaurus/theme-common'; import {useDocsPreferredVersion} from '@docusaurus/theme-common';
import {useVersions} from '@docusaurus/plugin-content-docs/client'; import {useVersions} from '@docusaurus/plugin-content-docs/client';
import Translate from '@docusaurus/Translate'; import Translate from '@docusaurus/Translate';
import Link from '@docusaurus/Link';
import CodeBlock from '@theme/CodeBlock'; import CodeBlock from '@theme/CodeBlock';
type ContextValue = { type ContextValue = {
@ -113,9 +114,9 @@ export function StableMajorVersion(): JSX.Element {
function GitBranchLink({branch}: {branch: string}): JSX.Element { function GitBranchLink({branch}: {branch: string}): JSX.Element {
return ( return (
<a href={`https://github.com/facebook/docusaurus/tree/${branch}`}> <Link to={`https://github.com/facebook/docusaurus/tree/${branch}`}>
<code>{branch}</code> <code>{branch}</code>
</a> </Link>
); );
} }

View file

@ -12,6 +12,7 @@ import Translate, {translate} from '@docusaurus/Translate';
import {useHistory, useLocation} from '@docusaurus/router'; import {useHistory, useLocation} from '@docusaurus/router';
import {usePluralForm} from '@docusaurus/theme-common'; import {usePluralForm} from '@docusaurus/theme-common';
import Link from '@docusaurus/Link';
import Layout from '@theme/Layout'; import Layout from '@theme/Layout';
import FavoriteIcon from '@site/src/components/svgIcons/FavoriteIcon'; import FavoriteIcon from '@site/src/components/svgIcons/FavoriteIcon';
import { import {
@ -123,15 +124,11 @@ function ShowcaseHeader() {
<section className="margin-top--lg margin-bottom--lg text--center"> <section className="margin-top--lg margin-bottom--lg text--center">
<h1>{TITLE}</h1> <h1>{TITLE}</h1>
<p>{DESCRIPTION}</p> <p>{DESCRIPTION}</p>
<a <Link className="button button--primary" to={SUBMIT_URL}>
className="button button--primary"
href={SUBMIT_URL}
target="_blank"
rel="noreferrer">
<Translate id="showcase.header.button"> <Translate id="showcase.header.button">
🙏 Please add your site 🙏 Please add your site
</Translate> </Translate>
</a> </Link>
</section> </section>
); );
} }

View file

@ -81,9 +81,9 @@ export default function Version(): JSX.Element {
</Link> </Link>
</td> </td>
<td> <td>
<a href={`${repoUrl}/releases/tag/v${latestVersion.name}`}> <Link to={`${repoUrl}/releases/tag/v${latestVersion.name}`}>
<ReleaseNotesLabel /> <ReleaseNotesLabel />
</a> </Link>
</td> </td>
</tr> </tr>
</tbody> </tbody>