From f5877c4054c381e145650eccaf1650fc3e7756a6 Mon Sep 17 00:00:00 2001 From: techfg Date: Sat, 27 Apr 2024 22:31:10 -0700 Subject: [PATCH] fix: do not transform links outside of a collection directory (#57) --- src/index.mjs | 7 ++++++ src/index.test.mjs | 57 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index e102014..3062cef 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -112,6 +112,13 @@ function astroRehypeRelativeMarkdownLinks(opts = {}) { const collectionName = path .dirname(relativeToContentPath) .split(FILE_PATH_SEPARATOR)[0]; + // if linked file is outside of a collection directory + if ( + collectionName === ".." || + (collectionPathMode !== "root" && collectionName === ".") + ) { + return; + } const collectionPathSegment = collectionPathMode === "root" ? PATH_SEGMENT_EMPTY : collectionName; // determine the path of the target file relative to the collection diff --git a/src/index.test.mjs b/src/index.test.mjs index 5c4f7cb..57663bf 100644 --- a/src/index.test.mjs +++ b/src/index.test.mjs @@ -31,10 +31,13 @@ import astroRehypeRelativeMarkdownLinks from "./index.mjs"; - https://github.com/nodejs/node/issues/51164#issuecomment-2034518078 */ +/** @param {Record { - visit(tree, "element", (node) => { - const fileInHistory = path.resolve(__dirname, __filename); + visit(tree, "element", () => { + const fileInHistory = options.currentFilePath + ? path.resolve(options.currentFilePath) + : path.resolve(__dirname, __filename); if (!file.history.includes(fileInHistory)) { file.history.push(fileInHistory); @@ -322,6 +325,38 @@ describe("astroRehypeRelativeMarkdownLinks", () => { }); }); + describe("collection names", () => { + test("should not transform path outside of a content collection directory and the content directory", async () => { + const input = 'foo'; + const { value: actual } = await rehype() + .use(testSetupRehype) + .use(astroRehypeRelativeMarkdownLinks, { + contentPath: "src/fixtures/dir-test", + }) + .process(input); + + const expected = + 'foo'; + + assert.equal(actual, expected); + }); + + test("should not transform path outside of a content collection directory and in the content directory", async () => { + const input = 'foo'; + const { value: actual } = await rehype() + .use(testSetupRehype, { + currentFilePath: "./src/fixtures/dir-test/index.md", + }) + .use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" }) + .process(input); + + const expected = + 'foo'; + + assert.equal(actual, expected); + }); + }); + describe("config option validation", () => { const runValidationTest = async (context, options) => { const validateOptionsMock = context.mock.fn(validateOptionsOriginal); @@ -482,6 +517,24 @@ describe("astroRehypeRelativeMarkdownLinks", () => { assert.equal(actual, expected); }); + + test("should transform path in content directory when content path same as collection path", async () => { + const input = 'foo'; + const { value: actual } = await rehype() + .use(testSetupRehype, { + currentFilePath: "./src/fixtures/dir-test/index.md", + }) + .use(astroRehypeRelativeMarkdownLinks, { + contentPath: "src/fixtures", + collectionPathMode: "root", + }) + .process(input); + + const expected = + 'foo'; + + assert.equal(actual, expected); + }); }); describe("config option - trailingSlash", () => {