Skip to content

Commit

Permalink
Gatsby node refactor. Ordering metadata navigation by lowercase. #697.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran McDade authored and MillenniumFalconMechanic committed Jun 17, 2020
1 parent 8fbc028 commit be8ca14
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/utils/node/create-pages-navigation.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
*/
const buildMetadataKeysByTitle = function buildMetadataKeysByTitle(metadataSchema) {

const metadataNodes = metadataSchema.edges.map(n => n.node);
const metadataNodes = metadataSchema.edges
.map(n => n.node)
.sort(function(node0, node1) {

const title0 = node0.title.toLowerCase();
const title1 = node1.title.toLowerCase();

return compareDataValues(title0, title1);
});

return metadataNodes.reduce((acc, node) => {

Expand Down Expand Up @@ -48,13 +56,7 @@ const buildMetadataLinks = function buildMetadataLinks(metadataPostsKeysByTitle)
/* Sort the primary link keys alphabetically. */
const sortedPrimaryLinks = [...primaryLinks].sort(function (link0, link1) {

if (link0 < link1) {
return -1;
}
if (link0 > link1) {
return 1;
}
return 0;
return compareDataValues(link0, link1);
});

/* Build the primary and secondary links. */
Expand Down Expand Up @@ -346,6 +348,29 @@ function buildPostTabs(tabKey, siteMap) {
}, []);
}

/**
* A simple comparison between two variables, returning a value to indicate an order of the variables in relation to each other.
* Used by the sort function.
*
* @param value0
* @param value1
* @returns {number}
*/
function compareDataValues(value0, value1) {

if ( value0 < value1 ) {

return -1;
}

if ( value0 > value1) {

return 1;
}

return 0;
}

/**
* Returns the site map path for the specified link.
*
Expand Down

0 comments on commit be8ca14

Please sign in to comment.