Skip to content

Commit 9df00db

Browse files
authored
Merge pull request #1106 from EnterpriseDB/feature/evan/latest-paths-search-index
Use latest paths in algolia search Former-commit-id: 3fb9680
2 parents 6398ef5 + 00879ec commit 9df00db

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/constants/algolia-indexing.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ const utf8Truncate = require('truncate-utf8-bytes');
22
const {
33
mdxNodesToTree,
44
computeFrontmatterForTreeNode,
5+
buildProductVersions,
6+
replacePathVersion,
57
} = require('./gatsby-node-utils.js');
68

79
// this function is weird - note that it's modifying the node in place
810
// NOT returning a copy of the node
9-
const mdxNodeToAlgoliaNode = (node) => {
11+
const mdxNodeToAlgoliaNode = (node, productVersions) => {
1012
let newNode = node;
1113

1214
// base
1315
newNode['title'] = node.frontmatter.title;
1416
newNode['path'] = node.fields.path;
17+
newNode['pagePath'] = node.fields.path;
1518

1619
// optional
1720
if (node.frontmatter.product) {
@@ -26,6 +29,15 @@ const mdxNodeToAlgoliaNode = (node) => {
2629
newNode['product'] = node.fields.product;
2730
newNode['version'] = node.fields.version;
2831
newNode['type'] = 'doc';
32+
33+
// switch path to latest (if applicable) to avoid redirects
34+
const isLatest =
35+
productVersions[node.fields.product][0] === node.fields.version;
36+
if (isLatest) {
37+
const latestPath = replacePathVersion(node.fields.path);
38+
newNode['path'] = latestPath;
39+
newNode['pagePath'] = latestPath;
40+
}
2941
} else {
3042
newNode['type'] = 'guide';
3143
}
@@ -108,10 +120,10 @@ const trimSpaces = (str) => {
108120
return str.replace(/\s+/g, ' ').trim();
109121
};
110122

111-
const buildFinalAlgoliaNodes = (nodes) => {
123+
const buildFinalAlgoliaNodes = (nodes, productVersions) => {
112124
const result = [];
113125
for (const node of nodes) {
114-
const algoliaNode = mdxNodeToAlgoliaNode(node);
126+
const algoliaNode = mdxNodeToAlgoliaNode(node, productVersions);
115127

116128
// skip indexing this content for now
117129
if (
@@ -166,7 +178,9 @@ const algoliaTransformer = ({ data }) => {
166178
mdxNodes.push(curr.mdxNode);
167179
}
168180

169-
return buildFinalAlgoliaNodes(mdxNodes);
181+
const productVersions = buildProductVersions(data.allMdx.nodes);
182+
183+
return buildFinalAlgoliaNodes(mdxNodes, productVersions);
170184
};
171185

172186
module.exports = algoliaTransformer;

0 commit comments

Comments
 (0)