From f53b56e765cd9635674822cb812708e482bfbd49 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Thu, 14 Mar 2024 04:56:51 +1100 Subject: [PATCH] (fix): getReferencedMarkdownFiles should ignore http/https links (#3169) --- .../src/__test__/valid-markdown-link.test.ts | 49 +++++++++++++++++++ .../valid-markdown-link.ts | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/cli/yaml/docs-validator/src/__test__/valid-markdown-link.test.ts diff --git a/packages/cli/yaml/docs-validator/src/__test__/valid-markdown-link.test.ts b/packages/cli/yaml/docs-validator/src/__test__/valid-markdown-link.test.ts new file mode 100644 index 00000000000..77f157b3a12 --- /dev/null +++ b/packages/cli/yaml/docs-validator/src/__test__/valid-markdown-link.test.ts @@ -0,0 +1,49 @@ +import { AbsoluteFilePath } from "@fern-api/fs-utils"; +import { getReferencedMarkdownFiles } from "../rules/valid-markdown-link/valid-markdown-link"; + +describe("getReferencedMarkdownFiles", () => { + it("should match on .md and .mdx", () => { + const content = ` + [Link to a markdown file](./file1.md) + [Link to an mdx file](./file2.mdx) + `; + + const links = getReferencedMarkdownFiles({ + content, + absoluteFilepath: AbsoluteFilePath.of("/path/to/fern/file0.md") + }); + + expect(links).toEqual([ + { path: "./file1.md", absolutePath: AbsoluteFilePath.of("/path/to/fern/file1.md") }, + { path: "./file2.mdx", absolutePath: AbsoluteFilePath.of("/path/to/fern/file2.mdx") } + ]); + }); + + it("should not match on non-markdown files", () => { + const content = ` + [Link to a non-markdown file](./file1.txt) + [Link to a non-markdown file](./file2.js) + `; + + const links = getReferencedMarkdownFiles({ + content, + absoluteFilepath: AbsoluteFilePath.of("/path/to/fern/file0.md") + }); + + expect(links).toEqual([]); + }); + + it("should not match on http or https links", () => { + const content = ` + [Link to a http file](http://example.com/file1.md) + [Link to a https file](https://example.com/file2.md) + `; + + const links = getReferencedMarkdownFiles({ + content, + absoluteFilepath: AbsoluteFilePath.of("/path/to/fern/file0.md") + }); + + expect(links).toEqual([]); + }); +}); diff --git a/packages/cli/yaml/docs-validator/src/rules/valid-markdown-link/valid-markdown-link.ts b/packages/cli/yaml/docs-validator/src/rules/valid-markdown-link/valid-markdown-link.ts index 33547ae249a..13064258448 100644 --- a/packages/cli/yaml/docs-validator/src/rules/valid-markdown-link/valid-markdown-link.ts +++ b/packages/cli/yaml/docs-validator/src/rules/valid-markdown-link/valid-markdown-link.ts @@ -1,7 +1,7 @@ import { AbsoluteFilePath, dirname, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils"; import { Rule, RuleViolation } from "../../Rule"; -const LINK_PATTERN = /\[([^\]]+)\]\((.*?)\)/g; +const LINK_PATTERN = /\[([^\]]+)\]\(((?!http:\/\/|https:\/\/).*?)\)/gi; export const ValidMarkdownLinks: Rule = { name: "valid-markdown-links",