mirror of
https://github.com/rancher/rancher-docs.git
synced 2026-09-24 20:18:18 +00:00
Add algolia build script
This commit is contained in:
Executable
+87
@@ -0,0 +1,87 @@
|
||||
#! /usr/bin/env node
|
||||
'use strict';
|
||||
const jsdom = require("jsdom");
|
||||
const {
|
||||
JSDOM
|
||||
} = jsdom;
|
||||
const md5 = require('md5');
|
||||
const atomicalgolia = require("atomic-algolia")
|
||||
const fs = require('fs');
|
||||
const indexName = "dev_docs"
|
||||
const rawdata = fs.readFileSync('public/algolia.json');
|
||||
const nodes = JSON.parse(rawdata);
|
||||
const nue = [];
|
||||
|
||||
nodes.forEach(node => {
|
||||
const dom = new JSDOM(node.content);
|
||||
const content = dom.window.document.body; //post content wrapped in a body tag
|
||||
const contentChildren = content.children; // all the children of the body tag
|
||||
const paragraphOut = {
|
||||
anchor: '#',
|
||||
title: '',
|
||||
content: '',
|
||||
postref: node.objectID,
|
||||
objectID: md5(node.permalink),
|
||||
permalink: node.permalink
|
||||
};
|
||||
|
||||
let childCount = contentChildren.length - 1; // how many children
|
||||
|
||||
// loop over the content until the next h2 heading -> this is the paragraph of searchable text
|
||||
while(childCount >= 0) {
|
||||
const child = contentChildren[childCount];
|
||||
|
||||
if (child.tagName === "H2") {
|
||||
//this is our header
|
||||
paragraphOut.anchor = `#${child.id}`;
|
||||
paragraphOut.title = child.textContent;
|
||||
|
||||
let next = child.nextElementSibling;
|
||||
|
||||
while(next && next.tagName !== 'H2') {
|
||||
if (next) {
|
||||
paragraphOut.content += next.textContent;
|
||||
}
|
||||
next = next.nextElementSibling;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
childCount--;
|
||||
}
|
||||
|
||||
// a post without headers
|
||||
if (paragraphOut.title === '') {
|
||||
// Set the title to the page title
|
||||
paragraphOut.title = node.title;
|
||||
|
||||
// pass along the content
|
||||
paragraphOut.content = content.textContent;
|
||||
}
|
||||
|
||||
// limit the content to 10k so we dont blow up just incase someone decides to make a 40k blog post in one paragraph ¯\_(ツ)_/¯
|
||||
paragraphOut.content = paragraphOut.content.substr(0, 8000);
|
||||
|
||||
// objectID is not quite unique yet so hash the entire object
|
||||
paragraphOut.objectID = md5(JSON.stringify(paragraphOut));
|
||||
|
||||
|
||||
nue.push(paragraphOut);
|
||||
|
||||
// remove potentially large content (see size limits) and replace with teh summary so that we don't get results with zero highlightable results
|
||||
node.content = node.summary;
|
||||
|
||||
// remove summary for dedup
|
||||
delete node.summary;
|
||||
|
||||
});
|
||||
|
||||
const merged = [...nodes, ...nue];
|
||||
|
||||
// fs.writeFileSync('public/combined.algolia.json', JSON.stringify(merged));
|
||||
// process.exit(0);
|
||||
atomicalgolia(indexName, merged, (err, result) => {
|
||||
if (err) throw err
|
||||
console.log(result);
|
||||
process.exit(0);
|
||||
});
|
||||
+3
-1
@@ -8,7 +8,7 @@
|
||||
"server": "gulp server",
|
||||
"server:with-drafts": "gulp server:with-drafts",
|
||||
"cms:delete": "gulp cms-delete",
|
||||
"algolia": "atomic-algolia"
|
||||
"algolia": "node build-algolia.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"atomic-algolia": "^0.3.15",
|
||||
@@ -33,6 +33,8 @@
|
||||
"gulp-watch": "^5.0.0",
|
||||
"instantsearch.js": "^2.8.0",
|
||||
"jquery": "^3.3.1",
|
||||
"jsdom": "^11.11.0",
|
||||
"md5": "^2.2.1",
|
||||
"moment": "^2.20.1",
|
||||
"node-sass-tilde-importer": "^1.0.0",
|
||||
"rancher-website-theme": "https://github.com/rancherlabs/website-theme.git",
|
||||
|
||||
+2
-19
@@ -25,28 +25,12 @@ const bootstrapDocsSearch = function() {
|
||||
container: '#hits',
|
||||
templates: {
|
||||
empty: '<h3>No results</h3>',
|
||||
item: `<h3><a href="{{permalink}}">{{{_highlightResult.title.value}}}</a></h3><div class="body">{{{_highlightResult.summary.value}}}</div>`
|
||||
item: `<h3><a href="{{permalink}}">{{{_highlightResult.title.value}}}</a></h3><div class="body">{{{_snippetResult.content.value}}}</div>`
|
||||
},
|
||||
escapeHits: true,
|
||||
})
|
||||
);
|
||||
|
||||
// search.addWidget(
|
||||
// instantsearch.widgets.hits({
|
||||
// container: '#hits',
|
||||
// templates: {
|
||||
// empty: '<h3>No results</h3>',
|
||||
// allItems: `<table class="search-results">
|
||||
// <tbody>
|
||||
// {{#hits}}
|
||||
// <tr><td><h3><a href="{{permalink}}">{{title}}</a></h3><div>{{summary}}</div></td></tr>
|
||||
// {{/hits}}
|
||||
// </tbody>
|
||||
// </table>`,
|
||||
// }
|
||||
// })
|
||||
// );
|
||||
|
||||
search.start();
|
||||
|
||||
$(window).on('keyup', e => {
|
||||
@@ -56,11 +40,10 @@ const bootstrapDocsSearch = function() {
|
||||
}
|
||||
});
|
||||
|
||||
$('header').on('click', '#button-search', e => {
|
||||
$('header').on('click', '#button-search', () => {
|
||||
|
||||
let container = $('.container-search');
|
||||
let overlay = $('.overlay-search');
|
||||
// let header = $(e.currentTarget).closest('header');
|
||||
|
||||
container.toggleClass('open');
|
||||
overlay.toggleClass('open');
|
||||
|
||||
Reference in New Issue
Block a user