-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
426852b
commit a723a6f
Showing
25 changed files
with
3,547 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# http://editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 2 | ||
# max_line_length = 80 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
insert_final_newline = false | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- "12.18.2" | ||
- "14.5.0" | ||
cache: npm | ||
script: | ||
- npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Praveen N | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Temporary copy of https://github.com/praveenn77/docusaurus-lunr-search | ||
until https://github.com/praveenn77/docusaurus-lunr-search/pull/88 is released |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"name": "docusaurus-lunr-search", | ||
"version": "2.2.0", | ||
"description": "Offline search component for Docusaurus V2", | ||
"main": "src/index.js", | ||
"publishConfig": { | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
"scripts": { | ||
"test": "mocha '**/__tests__/**/*.spec.js'" | ||
}, | ||
"homepage": "https://github.com/lelouch77/docusaurus-lunr-search/blob/master/README.md", | ||
"repository": { | ||
"url": "https://github.com/lelouch77/docusaurus-lunr-search/" | ||
}, | ||
"author": { | ||
"name": "Praveen N", | ||
"url": "https://github.com/lelouch77" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Pieter C", | ||
"url": "https://github.com/pcallewaert" | ||
}, | ||
{ | ||
"name": "ZachJW34", | ||
"url": "https://github.com/ZachJW34" | ||
}, | ||
{ | ||
"name": "Andrei Rublev", | ||
"url": "https://github.com/anru" | ||
} | ||
], | ||
"keywords": [ | ||
"docusaurus", | ||
"lunr", | ||
"offline-search", | ||
"documentation" | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"autocomplete.js": "^0.37.0", | ||
"classnames": "^2.2.6", | ||
"gauge": "^3.0.0", | ||
"hast-util-select": "^4.0.0", | ||
"hast-util-to-text": "^2.0.0", | ||
"hogan.js": "^3.0.2", | ||
"lunr": "^2.3.8", | ||
"lunr-languages": "^1.4.0", | ||
"minimatch": "^3.0.4", | ||
"object-assign": "^4.1.1", | ||
"rehype-parse": "^7.0.1", | ||
"to-vfile": "^6.1.0", | ||
"unified": "^9.0.0", | ||
"unist-util-is": "^4.0.2" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^8.0.1" | ||
}, | ||
"peerDependencies": { | ||
"@docusaurus/core": "^2.0.0-alpha.60 || ^2.0.0", | ||
"react": "^16.8.4 || ^17", | ||
"react-dom": "^16.8.4 || ^17" | ||
}, | ||
"engines": { | ||
"node": ">= 8.10.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const { parentPort, workerData } = require('worker_threads') | ||
|
||
// unified imports | ||
const unified = require('unified') | ||
const parse = require('rehype-parse') | ||
const select = require('hast-util-select').select | ||
const selectAll = require('hast-util-select').selectAll | ||
const toText = require('hast-util-to-text') | ||
const is = require('unist-util-is') | ||
const toVfile = require('to-vfile') | ||
|
||
const sectionHeaderTest = ({ tagName }) => ['h2', 'h3'].includes(tagName) | ||
|
||
// Build search data for a html | ||
function* scanDocuments({ path, url }) { | ||
let vfile | ||
try { | ||
vfile = toVfile.readSync(path) | ||
} catch (e) { | ||
if (e.code !== 'ENOENT') { | ||
console.error(`docusaurus-lunr-search:: unable to read file ${path}`) | ||
console.error(e) | ||
} | ||
return | ||
} | ||
|
||
const hast = unified() | ||
.use(parse, { emitParseErrors: false }) | ||
.parse(vfile) | ||
|
||
const article = select('article', hast) | ||
if (!article) { | ||
return | ||
} | ||
const markdown = select('.markdown', article) | ||
if (!markdown) { | ||
return | ||
} | ||
|
||
const pageTitleElement = select('h1', article) | ||
if (!pageTitleElement) { | ||
return | ||
} | ||
const pageTitle = toText(pageTitleElement) | ||
const sectionHeaders = getSectionHeaders(markdown) | ||
|
||
const keywords = selectAll('meta[name="keywords"]', hast).reduce((acc, metaNode) => { | ||
if (metaNode.properties.content) { | ||
return acc.concat(metaNode.properties.content.replace(/,/g, ' ')) | ||
} | ||
return acc | ||
}, []).join(' ') | ||
|
||
yield { | ||
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, | ||
} | ||
|
||
for (const sectionDesc of sectionHeaders) { | ||
const { title, content, ref } = sectionDesc; | ||
yield { | ||
title, | ||
type: 1, | ||
pageTitle, | ||
url: `${url}#${ref}`, | ||
content, | ||
} | ||
} | ||
} | ||
|
||
function getContent(element) { | ||
return toText(element).replace(/\s\s+/g, ' ').replace(/(\r\n|\n|\r)/gm, ' ').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"') | ||
} | ||
|
||
function getSectionHeaders(markdown) { | ||
let currentSection = null | ||
const result = [] | ||
let contentsAcc = '' | ||
const emitCurrent = () => { | ||
const ref = select('.anchor', currentSection) | ||
result.push({ | ||
title: toText(currentSection).replace(/^#+/, '').replace(/#$/, ''), | ||
ref: ref ? ref.properties.id : '#', | ||
content: contentsAcc, | ||
}) | ||
contentsAcc = '' | ||
currentSection = null | ||
} | ||
|
||
for (const node of markdown.children) { | ||
if (is(node, sectionHeaderTest)) { | ||
if (currentSection) { | ||
emitCurrent() | ||
} | ||
currentSection = node | ||
} else if (currentSection) { | ||
contentsAcc += getContent(node) + ' ' | ||
} | ||
} | ||
if (currentSection) { | ||
emitCurrent() | ||
} | ||
|
||
return result | ||
} | ||
|
||
function processFile(file) { | ||
console.log(`docusaurus-lunr-search:: Processing`, file) | ||
let scanned = 0 | ||
for (const doc of scanDocuments(file)) { | ||
scanned = 1 | ||
parentPort.postMessage([true, doc]) | ||
} | ||
parentPort.postMessage([null, scanned]) | ||
} | ||
|
||
parentPort.on('message', (maybeFile) => { | ||
if (maybeFile) { | ||
processFile(maybeFile) | ||
} else { | ||
parentPort.close() | ||
} | ||
}) |
Oops, something went wrong.