@@ -2,16 +2,19 @@ const utf8Truncate = require('truncate-utf8-bytes');
2
2
const {
3
3
mdxNodesToTree,
4
4
computeFrontmatterForTreeNode,
5
+ buildProductVersions,
6
+ replacePathVersion,
5
7
} = require ( './gatsby-node-utils.js' ) ;
6
8
7
9
// this function is weird - note that it's modifying the node in place
8
10
// NOT returning a copy of the node
9
- const mdxNodeToAlgoliaNode = ( node ) => {
11
+ const mdxNodeToAlgoliaNode = ( node , productVersions ) => {
10
12
let newNode = node ;
11
13
12
14
// base
13
15
newNode [ 'title' ] = node . frontmatter . title ;
14
16
newNode [ 'path' ] = node . fields . path ;
17
+ newNode [ 'pagePath' ] = node . fields . path ;
15
18
16
19
// optional
17
20
if ( node . frontmatter . product ) {
@@ -26,6 +29,15 @@ const mdxNodeToAlgoliaNode = (node) => {
26
29
newNode [ 'product' ] = node . fields . product ;
27
30
newNode [ 'version' ] = node . fields . version ;
28
31
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
+ }
29
41
} else {
30
42
newNode [ 'type' ] = 'guide' ;
31
43
}
@@ -108,10 +120,10 @@ const trimSpaces = (str) => {
108
120
return str . replace ( / \s + / g, ' ' ) . trim ( ) ;
109
121
} ;
110
122
111
- const buildFinalAlgoliaNodes = ( nodes ) => {
123
+ const buildFinalAlgoliaNodes = ( nodes , productVersions ) => {
112
124
const result = [ ] ;
113
125
for ( const node of nodes ) {
114
- const algoliaNode = mdxNodeToAlgoliaNode ( node ) ;
126
+ const algoliaNode = mdxNodeToAlgoliaNode ( node , productVersions ) ;
115
127
116
128
// skip indexing this content for now
117
129
if (
@@ -166,7 +178,9 @@ const algoliaTransformer = ({ data }) => {
166
178
mdxNodes . push ( curr . mdxNode ) ;
167
179
}
168
180
169
- return buildFinalAlgoliaNodes ( mdxNodes ) ;
181
+ const productVersions = buildProductVersions ( data . allMdx . nodes ) ;
182
+
183
+ return buildFinalAlgoliaNodes ( mdxNodes , productVersions ) ;
170
184
} ;
171
185
172
186
module . exports = algoliaTransformer ;
0 commit comments