= (props) => {
@@ -231,14 +228,14 @@ const Search: React.FC = (props) => {
diff --git a/src/utils/algolia-queries.js b/src/utils/algolia-queries.js
index 625c136f9..b3bc0383a 100644
--- a/src/utils/algolia-queries.js
+++ b/src/utils/algolia-queries.js
@@ -2,6 +2,7 @@ const { JSDOM } = require('jsdom');
const { htmlToText } = require('html-to-text');
const config = require('../configs/doc-configs');
const { getAlgoliaIndex } = require('../configs/algolia-search-config');
+const { getDocLinkFromEdge } = require('./gatsby-utils.js');
const getPathPrefix = () => {
return config.SITE_PREFIX;
@@ -48,6 +49,13 @@ query {
description
}
html
+ parent {
+ ... on File {
+ name
+ sourceInstanceName
+ relativePath
+ }
+ }
}
}
}
@@ -78,7 +86,13 @@ function splitStringIntoChunks(input, n) {
return result;
}
-const pageToAlgoliaRecordForASCII = (ele, type, node) => {
+const getDocLink = (edge) => {
+ const link = getDocLinkFromEdge(edge);
+ return `/${config.SITE_PREFIX}${link}`;
+};
+
+const pageToAlgoliaRecordForASCII = (ele, type, edge) => {
+ const node = edge.node;
const pageid = node.pageAttributes.pageid;
let sectionId;
let sectionTitle;
@@ -98,6 +112,8 @@ const pageToAlgoliaRecordForASCII = (ele, type, node) => {
const numberOfChunks = len / 8000 + 1;
const chunks = splitStringIntoChunks(body, numberOfChunks);
+ const docLink = getDocLink(edge);
+
return chunks.map((chunk, i) => ({
objectID: `${sectionId}_chunk_${i}`,
sectionId,
@@ -106,7 +122,7 @@ const pageToAlgoliaRecordForASCII = (ele, type, node) => {
pageid,
type: 'ASCII',
title: node.document.title,
- link: `/${config.SITE_PREFIX}/${pageid}`,
+ link: docLink,
}));
};
@@ -129,7 +145,7 @@ const queries = [
? pageToAlgoliaRecordForASCII(
preambleEle,
'preamble',
- edge.node,
+ edge,
)
: null;
const sections = Array.prototype.map.call(
@@ -138,7 +154,7 @@ const queries = [
pageToAlgoliaRecordForASCII(
sect,
'section',
- edge.node,
+ edge,
),
);
if (preamble) {
diff --git a/src/utils/aloglia.test.ts b/src/utils/aloglia.test.ts
index e25eb2fcc..ed5b8b585 100644
--- a/src/utils/aloglia.test.ts
+++ b/src/utils/aloglia.test.ts
@@ -53,6 +53,11 @@ const htmlForPreambleEle = `
const divForpreamble = new JSDOM(htmlForPreambleEle).window.document;
const dummyPreambleEle = divForpreamble.querySelector('#preamble');
+const parentNode = {
+ name: 'testname',
+ sourceInstanceName: 'testsource',
+ relativePath: 'testpath',
+};
const asciiNode = {
id: 'fa556896-4e38-5e7a-ab35-a45ee93d58ee',
pageAttributes: {
@@ -61,6 +66,7 @@ const asciiNode = {
document: {
title: 'About REST APIs',
},
+ parent: parentNode,
};
const asciiAlgoliaObj = {
@@ -114,7 +120,7 @@ describe('test cases from algolia search', () => {
algoliaSearch.pageToAlgoliaRecordForASCII(
dummySectionEle,
'section',
- asciiNode,
+ { node: asciiNode },
),
).toStrictEqual([asciiAlgoliaObj]);
});
@@ -124,7 +130,7 @@ describe('test cases from algolia search', () => {
algoliaSearch.pageToAlgoliaRecordForASCII(
dummyPreambleEle,
'preamble',
- asciiNode,
+ { node: asciiNode },
),
).toStrictEqual([asciiPremableAlgoliaObj]);
});
diff --git a/src/utils/gatsby-utils.js b/src/utils/gatsby-utils.js
new file mode 100644
index 000000000..869decd02
--- /dev/null
+++ b/src/utils/gatsby-utils.js
@@ -0,0 +1,35 @@
+const getTutorialLinkFromEdge = (edge) => {
+ // Tutorials module pageids follow pattern {subdirectory}_{final_url_stub}
+ // to give unique IDs in system but allow directory structure in URL
+ const { pageid: pageId } = edge.node.pageAttributes;
+ const { relativePath: relPath } = edge.node.parent;
+ // One-level of subdirectory part of stub
+ const relPathSplit = relPath.split('/');
+ const pageIdSplit = pageId.split('__');
+ let finalPageId = pageId;
+ if (pageIdSplit.length > 1) {
+ finalPageId = pageIdSplit[1];
+ }
+
+ let finalPath = `/tutorials/${finalPageId}`;
+ if (relPathSplit.length > 1) {
+ finalPath = `/tutorials/${relPathSplit[0]}/${finalPageId}`;
+ }
+
+ return finalPath;
+};
+
+const getDocLinkFromEdge = (edge) => {
+ const { pageid: pageId } = edge.node.pageAttributes;
+ const { sourceInstanceName: sourceName } = edge.node.parent;
+
+ if (sourceName === 'tutorials') {
+ return getTutorialLinkFromEdge(edge);
+ }
+
+ return `/${pageId}`;
+};
+
+module.exports = {
+ getDocLinkFromEdge,
+};