Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit the absolute link linter #126

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions server/lint-teleport-docs-links.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, test } from "@jest/globals";
import { remarkLintTeleportDocsLinks } from "./lint-teleport-docs-links";
import { VFile } from "vfile";
import { remark } from "remark";
import mdx from "remark-mdx";

const getReasons = (value: string) => {
return remark()
.use(mdx as any)
.use(remarkLintTeleportDocsLinks as any)
.processSync(new VFile({ value, path: "mypath.mdx" }) as any)
.messages.map((m) => m.reason);
};

describe("server/lint-absolute-docs-links", () => {
interface testCase {
description: string;
input: string;
expected: Array<string>;
}

const testCases: Array<testCase> = [
{
description: "absolute docs path in a Markdown link href",
input: "This is a [link](https://goteleport.com/docs/installation)",
expected: [
"Link reference https://goteleport.com/docs/installation must be a relative link to an *.mdx page",
],
},
{
description: "absolute docs path in an a tag",
input: "<a href='https://goteleport.com/docs/installation'>here</a>",
expected: [
"Component href https://goteleport.com/docs/installation must be a relative link to an *.mdx page",
],
},
{
description: "JavaScript identifier as href value",
input:
"You can <a href={BotLogo} download>download</a> our avatar to set as your Bot Icon.",
expected: [],
},
];

test.each(testCases)("$description", (tc) => {
expect(getReasons(tc.input)).toEqual(tc.expected);
});
});
9 changes: 8 additions & 1 deletion server/lint-teleport-docs-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const isMdxComponentWithHref = (node: Node): node is MdxAnyElement => {
};

const isAnAbsoluteDocsLink = (href: string): boolean => {
// The href is not a string value, and is likely an AST node object. Skip
// the absolute link check.
if (typeof href != "string") {
return false;
}
return (
Comment on lines 26 to 32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if typescript is doing its job, this should only be able to be called with a string anyway. but, i suppose it doesnt hurt to be extra safe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if typescript is doing its job, this should only be able to be called with a string anyway. but, i suppose it doesnt hurt to be extra safe

👍 For some reason, TypeScript is letting remark call the link linter on non-strings, I guess because it doesn't check the site where remark calls its plugins.

href.startsWith("/docs") || href.startsWith("https://goteleport.com/docs")
);
Expand All @@ -35,7 +40,9 @@ export const remarkLintTeleportDocsLinks = lintRule(
visit(root, undefined, (node: Node) => {
if (node.type == "link" && isAnAbsoluteDocsLink((node as Link).url)) {
vfile.message(
`Link reference ${(node as Link).url} must be a relative link to an *.mdx page`,
`Link reference ${
(node as Link).url
} must be a relative link to an *.mdx page`,
node.position
);
return;
Expand Down
Loading