Skip to content

Commit

Permalink
(fix): getReferencedMarkdownFiles should ignore http/https links (#3169)
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity authored Mar 13, 2024
1 parent 2a09428 commit f53b56e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit f53b56e

Please sign in to comment.