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 relative links in same physical folder #234

Merged
merged 1 commit into from
Sep 18, 2019
Merged
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
18 changes: 13 additions & 5 deletions modules/gatsby/src/gatsby-node/pages/create-doc-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ const getPageTemplateUrl = require('./get-page-template-url');
// NOTE: gatsby does automatically build pages from **top level** `/pages`, folder
// but in ocular we keep those pages in the installed structure so gatsby can't see them

// if using simply path.relative(from, to) to files which are in the same folder, the resolved path is: '../to'.
// instead we do relative path between folders, then add the name of the target file in the end.
// in that same scenario, the relative path between folders will be '', and overall path just 'to'.

function linkFromFileToFile(sourceFile, targetFile) {
const relativePathFromDirToDir = path.relative(path.dirname(sourceFile), path.dirname(targetFile));
return path.join(relativePathFromDirToDir, path.basename(targetFile));
}

function addToRelativeLinks({source, target, rootFolder, edge, relativeLinks}) {
// what we are doing here: for each markdown file, we create a mapping of different ways to
// link to another markdown file that we will honor.
Expand Down Expand Up @@ -38,16 +47,15 @@ function addToRelativeLinks({source, target, rootFolder, edge, relativeLinks}) {
)();
return {};
}

const relativeToCurrentFile = path.relative(
const relativeToCurrentFile = linkFromFileToFile(
edge.node.fileAbsolutePath,
source
);
const relativeToRootFolder = rootFolder && path.relative(rootFolder, source);
const relativeToCurrentSlug = path.relative(edge.node.fields.path, target);
const relativeToRootFolder = rootFolder && linkFromFileToFile(rootFolder, source);
const relativeToCurrentSlug = linkFromFileToFile(edge.node.fields.path, target);

const absoluteTarget = `/${target}`;

return {
...relativeLinks,
[relativeToCurrentFile]: absoluteTarget,
Expand Down