Skip to content

Commit

Permalink
Add lint-teleport-docs-links
Browse files Browse the repository at this point in the history
  • Loading branch information
ptgott committed Dec 9, 2024
1 parent 574a2a3 commit c2e3127
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions server/lint-teleport-docs-links.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { lintRule } from "unified-lint-rule";
import { visit } from "unist-util-visit";
import type { Link as MdastLink } from "mdast";
import type { EsmNode, MdxAnyElement, MdxastNode } from "./types-unist";
import type { Node } from "unist";
import type { Transformer } from "unified";
import type { Root, Link as MdastLink } from "mdast";
import type { EsmNode, MdxAnyElement } from "./types-unist";

import { visit } from "unist-util-visit";
import { isExternalLink, isHash, isPage } from "../utils/url";

interface ObjectHref {
Expand All @@ -14,7 +14,7 @@ type Href = string | ObjectHref;

const mdxNodeTypes = new Set(["mdxJsxFlowElement", "mdxJsxTextElement"]);

const isMdxComponentWithHref = (node: Node): node is MdxAnyElement => {
const isMdxComponentWithHref = (node: MdxastNode): node is MdxAnyElement => {
return (
mdxNodeTypes.has(node.type) &&
(node as MdxAnyElement).attributes.some(
Expand All @@ -29,17 +29,13 @@ const isAnAbsoluteDocsLink = (href: string): boolean => {
);
};

export function remarkLintTeleportDocsLinks(): Transformer {
return (root: Root, vfile) => {
visit(root, (node: Node) => {
if (
node.type == "link" &&
isAnAbsoluteDocsLink((node as MdastLink).url)
) {
export const remarkLintTeleportDocsLinks = lintRule(
"remark-lint:absolute-docs-links",
(root: Node, vfile) => {
visit(root, undefined, (node: MdxastNode) => {
if (node.type == "link" && isAnAbsoluteDocsLink(node.url)) {
vfile.message(
`Link reference ${
(node as MdastLink).url
} must be a relative link to an *.mdx page`,
`Link reference ${node.url} must be a relative link to an *.mdx page`,
node.position
);
return;
Expand All @@ -58,5 +54,5 @@ export function remarkLintTeleportDocsLinks(): Transformer {
}
}
});
};
}
}
);

0 comments on commit c2e3127

Please sign in to comment.