From 00879ec429f3e7f7ffe9481ffa3a9ef74b1c10c3 Mon Sep 17 00:00:00 2001 From: Evan Barger Date: Fri, 19 Mar 2021 15:47:23 -0400 Subject: [PATCH] Change algolia indexing to use latest paths; assign new pagePath attribute for deduplication usage Former-commit-id: 919b86ff9b5a85876b46df457f2a947bc3a1777d --- src/constants/algolia-indexing.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/constants/algolia-indexing.js b/src/constants/algolia-indexing.js index 949eab8222c..575df5b4bf6 100644 --- a/src/constants/algolia-indexing.js +++ b/src/constants/algolia-indexing.js @@ -2,16 +2,19 @@ const utf8Truncate = require('truncate-utf8-bytes'); const { mdxNodesToTree, computeFrontmatterForTreeNode, + buildProductVersions, + replacePathVersion, } = require('./gatsby-node-utils.js'); // this function is weird - note that it's modifying the node in place // NOT returning a copy of the node -const mdxNodeToAlgoliaNode = (node) => { +const mdxNodeToAlgoliaNode = (node, productVersions) => { let newNode = node; // base newNode['title'] = node.frontmatter.title; newNode['path'] = node.fields.path; + newNode['pagePath'] = node.fields.path; // optional if (node.frontmatter.product) { @@ -26,6 +29,15 @@ const mdxNodeToAlgoliaNode = (node) => { newNode['product'] = node.fields.product; newNode['version'] = node.fields.version; newNode['type'] = 'doc'; + + // switch path to latest (if applicable) to avoid redirects + const isLatest = + productVersions[node.fields.product][0] === node.fields.version; + if (isLatest) { + const latestPath = replacePathVersion(node.fields.path); + newNode['path'] = latestPath; + newNode['pagePath'] = latestPath; + } } else { newNode['type'] = 'guide'; } @@ -108,10 +120,10 @@ const trimSpaces = (str) => { return str.replace(/\s+/g, ' ').trim(); }; -const buildFinalAlgoliaNodes = (nodes) => { +const buildFinalAlgoliaNodes = (nodes, productVersions) => { const result = []; for (const node of nodes) { - const algoliaNode = mdxNodeToAlgoliaNode(node); + const algoliaNode = mdxNodeToAlgoliaNode(node, productVersions); // skip indexing this content for now if ( @@ -166,7 +178,9 @@ const algoliaTransformer = ({ data }) => { mdxNodes.push(curr.mdxNode); } - return buildFinalAlgoliaNodes(mdxNodes); + const productVersions = buildProductVersions(data.allMdx.nodes); + + return buildFinalAlgoliaNodes(mdxNodes, productVersions); }; module.exports = algoliaTransformer;