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

fix: do not transform links outside of a content collection directory #58

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
7 changes: 7 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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 @@ -373,6 +376,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 @@ -549,6 +584,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