Updated algolia for the order.

This commit is contained in:
Satbir Singh 2020-05-29 12:42:35 +05:30
parent 2fb29290f1
commit 26717a14a6

View File

@ -1,8 +1,8 @@
var https = require("https"); var https = require("https");
const algoliasearch = require("algoliasearch"); const algoliasearch = require('algoliasearch');
const client = algoliasearch("AZ2Z9CJSJ0", "e92300d4e8dbaf2cbaa9ebbbeb4e06e6"); const client = algoliasearch('AZ2Z9CJSJ0', 'e92300d4e8dbaf2cbaa9ebbbeb4e06e6');
const index = client.initIndex("test_appsmith"); const index = client.initIndex('test_appsmith');
var options = { var options = {
headers: { headers: {
@ -14,32 +14,28 @@ var options = {
console.log("Loading function"); console.log("Loading function");
function getPage(pageId) { function getPage(pageId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
https.get( https.get(`https://api-beta.gitbook.com/v1/spaces/-Lzuzdhj8LjrQPaeyCxr/content/v/master/id/${pageId}?format=markdown`, options, (res) => {
`https://api-beta.gitbook.com/v1/spaces/-Lzuzdhj8LjrQPaeyCxr/content/v/master/id/${pageId}?format=markdown`, res.setEncoding('utf8');
options, let rawData = '';
res => { res.on('data', (chunk) => { rawData += chunk; });
res.setEncoding("utf8"); res.on('end', () => {
let rawData = "";
res.on("data", chunk => {
rawData += chunk;
});
res.on("end", () => {
try { try {
const parsedData = JSON.parse(rawData); const parsedData = JSON.parse(rawData);
resolve(parsedData); resolve(parsedData);
} catch (e) { } catch (e) {
reject(e); reject(e)
console.error(e.message); console.error(e.message);
} }
}); });
}, })
);
}); });
} }
const pages = []; const pages = []
function pushChildPages(masterPage) { function pushChildPages(masterPage) {
if (masterPage.pages) { if (masterPage.pages) {
@ -52,21 +48,38 @@ function pushChildPages(masterPage) {
} }
} }
exports.handler = async event => { const orderArr = [{
path: "master/quick-start",
order: 0,
}, {
path: "master/core-concepts/building-the-ui",
order: 1
}, {
path: "master/core-concepts/building-the-ui/displaying-api-data",
order: 2
}, {
path: "master/core-concepts/apis",
order: 3
}, {
path: "master/core-concepts/apis/taking-inputs-from-widgets",
order: 4
}]
function swap(arr, index1, index2) {
let x = arr[index1]
arr[index1] = arr[index2]
arr[index2] = x;
}
exports.handler = async (event) => {
const response = await new Promise((resolve, reject) => { const response = await new Promise((resolve, reject) => {
const req = https.get( const req = https.get("https://api-beta.gitbook.com/v1/spaces/-Lzuzdhj8LjrQPaeyCxr/content", options, (res) => {
"https://api-beta.gitbook.com/v1/spaces/-Lzuzdhj8LjrQPaeyCxr/content", res.setEncoding('utf8');
options, let rawData = '';
res => { res.on('data', (chunk) => { rawData += chunk; });
res.setEncoding("utf8"); res.on('end', () => {
let rawData = "";
res.on("data", chunk => {
rawData += chunk;
});
res.on("end", () => {
try { try {
const parsedData = JSON.parse(rawData); const parsedData = JSON.parse(rawData);
let masterPage = parsedData.variants[0].page; let masterPage = parsedData.variants[0].page;
pushChildPages(masterPage); pushChildPages(masterPage);
@ -80,37 +93,49 @@ exports.handler = async event => {
page.path = pages[index].path; page.path = pages[index].path;
page.pages = undefined; page.pages = undefined;
page.objectID = page.uid; page.objectID = page.uid;
if (page.path === "master/changelog") {
page.document = undefined
}
}); });
resolve({ orderArr.forEach(order => {
statusCode: 200, let index = updatedPages.findIndex(i => i.path === order.path)
body: JSON.stringify(updatedPages), if (index !== -1) {
}); swap(updatedPages, index, order.order)
}
})
updatedPages = updatedPages.map((item, index) => { return { ...item, defaultOrder: index } })
// index.replaceAllObjects(updatedPages, {
// autoGenerateObjectIDIfNotExist: true
// }).then(({ objectIDs }) => {
// console.log(objectIDs);
// resolve({ // resolve({
// statusCode: 200, // statusCode: 200,
// body: JSON.stringify(updatedPages) // body: JSON.stringify(updatedPages)
// }) // })
// }).catch(e => {
// reject({ index.replaceAllObjects(updatedPages, {
// statusCode: 500, autoGenerateObjectIDIfNotExist: true
// body: 'Algolia upload failed.' }).then(({ objectIDs }) => {
// }) console.log(objectIDs);
// }) resolve({
statusCode: 200,
body: JSON.stringify(updatedPages)
})
}).catch(e => {
reject({
statusCode: 500,
body: 'Algolia upload failed.'
})
})
}); });
} catch (e) { } catch (e) {
reject({ reject({
statusCode: 500, statusCode: 500,
body: "Most probably gitbook getPage apis failed", body: 'Most probably gitbook getPage apis failed'
}); })
} }
}); });
}, });
);
}); });
return response; return response;