Skip to content

Commit

Permalink
fix relative links in same physical folder (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerome Cukier authored Sep 18, 2019
1 parent 2661d57 commit acb4b83
Showing 1 changed file with 13 additions and 5 deletions.
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

0 comments on commit acb4b83

Please sign in to comment.