Skip to content

Commit

Permalink
Merge pull request #6 from lelouch77/build-restructure
Browse files Browse the repository at this point in the history
Build restructuring
  • Loading branch information
lelouch77 authored Apr 10, 2020
2 parents 021688e + 9d5401d commit 8117b65
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 174 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ npm i docusurus-lunr-search --save
```
npm run swizzle docusurus-lunr-search SearchBar
```
3. Copy the [`build-search-data.js`](./build-search-data.js) to the root folder of your project
4. Then build your Docusurus project
3. Add the docusurus-lunr-search plugin to your `docusaurus.config.js`
```
npm run build
module.exports = {
// ...
plugins: [
'docusurus-lunr-search'
]
}
```
5. Create the search data using by running `build-search-data.js`
4. Then build your Docusurus project
```
node build-search-data.js
npm run build
```
6. You are done!.
\
Now you can build your project again with new `search-data.js`
5. Serve your application
```
npm run build
//or
npm start
npx http-server ./build
```

Note: Docusaurus search information can only be generated from a production build. Local development is currently not supported.
## Sample
<p align="center">
<img width="460" height="300" src="https://raw.githubusercontent.com/lelouch77/docusurus-lunr-search/master/assets/search-offline.png">
Expand Down
122 changes: 0 additions & 122 deletions build-search-data.js

This file was deleted.

40 changes: 11 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docusurus-lunr-search",
"version": "0.0.8",
"version": "1.0.0",
"description": "Offline search component for Docusaurus V2",
"main": "src/index.js",
"publishConfig": {
Expand All @@ -19,6 +19,10 @@
{
"name": "Pieter C",
"url": "https://github.com/pcallewaert"
},
{
"name": "ZachJW34",
"url": "https://github.com/ZachJW34"
}
],
"keywords": [
Expand All @@ -32,7 +36,6 @@
"autocomplete.js": "^0.37.0",
"cheerio": "^1.0.0-rc.3",
"classnames": "^2.2.6",
"fs-extra": "^8.1.0",
"hogan.js": "^3.0.2",
"lunr": "^2.3.8"
},
Expand Down
108 changes: 101 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
* LICENSE file in the root directory of this source tree.
*/

const path = require('path');
const fs = require("fs");
const path = require("path");
const cheerio = require("cheerio");

module.exports = function() {
return {
name: 'docusurus-lunr-search',
const sectionHeaderElements = ["h2", "h3"];

module.exports = function () {
return {
name: "docusurus-lunr-search",
getThemePath() {
return path.resolve(__dirname, './theme');
return path.resolve(__dirname, "./theme");
},

configureWebpack() {
// Ensure that algolia docsearch css is its own chunk
return {
optimization: {
splitChunks: {
cacheGroups: {
algolia: {
name: 'algolia',
name: "algolia",
test: /algolia\.css$/,
chunks: `all`,
enforce: true,
Expand All @@ -34,5 +36,97 @@ module.exports = function() {
},
};
},
async postBuild({ routesPaths = [], outDir, baseUrl }) {
const files = routesPaths
.filter((route) => route !== baseUrl && route !== `${baseUrl}404.html`)
.map((route) => route.substr(baseUrl.length))
.map((route) => ({
path: path.join(outDir, route, "index.html"),
url: route,
}));
const searchData = buildSearchData(files);
fs.writeFileSync(
path.join(outDir, "search-data.json"),
JSON.stringify(searchData)
);
},
};
};

// Build search data for a html
function buildSearchData(files) {
const searchData = [];
files.forEach(({ path, url }) => {
const htmlFile = fs.readFileSync(path);
// const dom = new JSDOM(htmlFile);
const $ = cheerio.load(htmlFile);

const article = $("article");
if (!article.length) {
return;
}
const markdown = article.find(".markdown");
if (!markdown.length) {
return;
}

const pageTitleElement = article.find("h1");
if (!pageTitleElement.length) {
return;
}
const pageTitle = article.find("h1").text();
const sectionHeaders = sectionHeaderElements.reduce((acc, selector) => {
acc = acc.concat(Array.from(markdown.find(selector)));
return acc;
}, []);

keywords = $("meta[name='keywords']").attr("content");
if (typeof keywords !== "undefined" && keywords) {
keywords = keywords.replace(",", " ");
}

searchData.push({
title: pageTitle,
type: 0,
sectionRef: "#",
url,
// If there is no sections then push the complete content under page title
content: sectionHeaders.length === 0 ? getContent(markdown) : "",
keywords: keywords,
});

sectionHeaders.forEach((sectionHeader) => {
sectionHeader = $(sectionHeader);
const title = sectionHeader.text().slice(1);
const sectionRef = sectionHeader.children().first().attr("id");
const content = getSectionContent(sectionHeader);
searchData.push({
title,
type: 1,
pageTitle,
url: `${url}#${sectionRef}`,
content,
});
});
});
return searchData;
}

function getContent(element) {
const text =
element.is("table") || element.find("table").length !== 0
? element.html().replace(/<[^>]*>/g, " ")
: element.text();
return text.replace(/\s\s+/g, " ").replace(/(\r\n|\n|\r)/gm, " ");
}

function getSectionContent(section) {
let content = "";
section = section.next();
while (section.length) {
if (sectionHeaderElements.some((s) => section.is(s))) break;
content += getContent(section) + " ";
section = section.next();
}
return content;
}
Loading

0 comments on commit 8117b65

Please sign in to comment.