| | #!/usr/bin/env node |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | const fs = require( 'fs' ); |
| | const globby = require( 'globby' ); |
| | const lunr = require( 'lunr' ); |
| |
|
| | function main() { |
| | |
| | const dirList = [ |
| | 'assets', |
| | 'bin', |
| | 'client', |
| | 'config', |
| | 'docs', |
| | 'packages', |
| | 'test', |
| | '.github', |
| | ].map( ( dir ) => dir + '/**/*.md' ); |
| | |
| | dirList.push( '*.md' ); |
| | |
| | dirList.push( '!**/node_modules/**' ); |
| |
|
| | const documents = globby |
| | .sync( dirList ) |
| | .map( documentFromFile ) |
| | .filter( ( doc ) => doc.title && doc.body ); |
| |
|
| | fs.mkdirSync( 'build', { recursive: true } ); |
| | writeSearchIndex( documents, 'build/devdocs-search-index.json' ); |
| | } |
| |
|
| | function writeSearchIndex( documents, searchIndexPath ) { |
| | const index = lunr( function () { |
| | this.field( 'title', { boost: 10 } ); |
| | this.field( 'body' ); |
| |
|
| | documents.forEach( ( doc, i ) => { |
| | |
| | |
| | |
| | |
| | |
| |
|
| | const indexDoc = {}; |
| | indexDoc.id = i; |
| | indexDoc.title = doc.title; |
| |
|
| | |
| | indexDoc.body = doc.body.replace( /<>\/="/, ' ' ); |
| |
|
| | this.add( indexDoc ); |
| | } ); |
| | } ); |
| |
|
| | const searchIndexJSON = JSON.stringify( { index, documents } ); |
| | fs.writeFileSync( searchIndexPath, searchIndexJSON ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | function documentFromFile( path ) { |
| | const content = fs.readFileSync( path, { encoding: 'utf8' } ); |
| |
|
| | const data = content |
| | .replace( /^\s*[\r\n]/gm, '' ) |
| | .replace( /^#+|^={2,}|^-{2,}/gm, '' ); |
| |
|
| | const firstLineEnd = data.indexOf( '\n' ); |
| |
|
| | if ( firstLineEnd === -1 ) { |
| | |
| | return {}; |
| | } |
| |
|
| | const title = data.slice( 0, firstLineEnd ); |
| | const body = data.slice( firstLineEnd + 1 ); |
| |
|
| | return { |
| | path, |
| | content, |
| | title, |
| | body, |
| | }; |
| | } |
| |
|
| | main(); |
| |
|