From be8ca148c1a7be86495096248cdefcd2b1a1e75d Mon Sep 17 00:00:00 2001 From: Fran McDade Date: Wed, 17 Jun 2020 15:18:27 +1000 Subject: [PATCH] Gatsby node refactor. Ordering metadata navigation by lowercase. #697. --- .../node/create-pages-navigation.service.js | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/utils/node/create-pages-navigation.service.js b/src/utils/node/create-pages-navigation.service.js index 7ec66c3c1..3d2ec33f3 100644 --- a/src/utils/node/create-pages-navigation.service.js +++ b/src/utils/node/create-pages-navigation.service.js @@ -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) => { @@ -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. */ @@ -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. *