Skip to content

Commit

Permalink
fix: do not transform links outside of a collection directory (vernak…
Browse files Browse the repository at this point in the history
  • Loading branch information
techfg committed Apr 28, 2024
1 parent ea8486e commit f5877c4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 55 additions & 2 deletions src/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ import astroRehypeRelativeMarkdownLinks from "./index.mjs";
- https://github.com/nodejs/node/issues/51164#issuecomment-2034518078
*/

/** @param {Record<string, { currentFilePath?: string }} options */
function testSetupRehype(options = {}) {
return (tree, file) => {
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);
Expand Down Expand Up @@ -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 = '<a href="./fixtures/test.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, {
contentPath: "src/fixtures/dir-test",
})
.process(input);

const expected =
'<html><head></head><body><a href="./fixtures/test.md">foo</a></body></html>';

assert.equal(actual, expected);
});

test("should not transform path outside of a content collection directory and in the content directory", async () => {
const input = '<a href="../test.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype, {
currentFilePath: "./src/fixtures/dir-test/index.md",
})
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.process(input);

const expected =
'<html><head></head><body><a href="../test.md">foo</a></body></html>';

assert.equal(actual, expected);
});
});

describe("config option validation", () => {
const runValidationTest = async (context, options) => {
const validateOptionsMock = context.mock.fn(validateOptionsOriginal);
Expand Down Expand Up @@ -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 = '<a href="../test.md">foo</a>';
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 =
'<html><head></head><body><a href="/test">foo</a></body></html>';

assert.equal(actual, expected);
});
});

describe("config option - trailingSlash", () => {
Expand Down

0 comments on commit f5877c4

Please sign in to comment.