Skip to content

Commit

Permalink
consistently add trailing slash to internal URLs
Browse files Browse the repository at this point in the history
Visiting a page directly already added this trailing slash.
(both locally and on Netlify)

Google also already preferred the pages with trailing slash.
  • Loading branch information
mistermicheels committed Aug 14, 2022
1 parent c7f44b9 commit bf7cd2b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
24 changes: 16 additions & 8 deletions _scripts/write-website-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,28 @@ function checkNoteImageUrl(originalUrl, relativeFilePath) {
}

function getInternalLinkNodeReplacement(node, relativePath) {
const isLinkWithinPage = node.url.startsWith("#");
return {
...node,
url: getNewInternalLinkUrl(node.url, relativePath)
};
}

function getNewInternalLinkUrl(oldUrl, relativePath) {
const isLinkWithinPage = oldUrl.startsWith("#");

if (isLinkWithinPage) {
return node;
return oldUrl;
}

const folderPath = path.dirname(relativePath);
const urlRelativeToRoot = path.join(folderPath, node.url);
const urlRelativeToRoot = path.join(folderPath, oldUrl);
const newUrl = "/" + normalizeUrl(removeMarkdownExtension(urlRelativeToRoot));

return {
...node,
url: newUrl
};

if (newUrl.includes("#")) {
return newUrl.replace("#", "/#");
} else {
return newUrl + "/";
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions _website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const config = {
tagline: 'Notes regarding things I have learned',
url: 'https://learning-notes.mistermicheels.com',
baseUrl: '/',
trailingSlash: true,
onBrokenLinks: 'ignore', // broken links are checked using other scripts
onBrokenMarkdownLinks: 'ignore', // broken links are checked using other scripts
favicon: 'favicon.ico',
Expand Down
2 changes: 1 addition & 1 deletion _website/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Redirect} from '@docusaurus/router';
import useBaseUrl from '@docusaurus/useBaseUrl';

function Home() {
return <Redirect to={useBaseUrl('/about/about')} />;
return <Redirect to={useBaseUrl('/about/about/')} />;
}

export default Home;

0 comments on commit bf7cd2b

Please sign in to comment.