File size: 2,596 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node

/**
 * This script generates a Lunr.js index and document array suitable for server-side
 * documentation search. It finds all .md files that are part of the Calypso repository
 * and writes the index to build/search-index.json.
 *
 * The design is currently limited by available RAM, both during indexing and serving the
 * content. A more scalable solution would need to use a separate indexing service like Sphinx.
 */

const fs = require( 'fs' );
const globby = require( 'globby' );
const lunr = require( 'lunr' );

function main() {
	// Build a list of all .md files in allowed subdirectories...
	const dirList = [
		'assets',
		'bin',
		'client',
		'config',
		'docs',
		'packages',
		'test',
		'.github',
	].map( ( dir ) => dir + '/**/*.md' );
	// ... and the current directory
	dirList.push( '*.md' );
	// don't descend into node_modules
	dirList.push( '!**/node_modules/**' );

	const documents = globby
		.sync( dirList )
		.map( documentFromFile )
		.filter( ( doc ) => doc.title && doc.body /* skip empty/invalid files */ );

	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 ) => {
			/*
			 * we use the array index as the document id
			 * so that we can look the preprocessed contents
			 * up out of the "documents" array also exported below
			 */

			const indexDoc = {};
			indexDoc.id = i;
			indexDoc.title = doc.title;

			//preprocess body to remove non-word characters, e.g. <optgroup> becomes optgroup
			indexDoc.body = doc.body.replace( /<>\/="/, ' ' );

			this.add( indexDoc );
		} );
	} );

	const searchIndexJSON = JSON.stringify( { index, documents } );
	fs.writeFileSync( searchIndexPath, searchIndexJSON );
}

/**
 * Strip formatting from content and extract the title and
 * return a basic JSON object suitable for indexing
 */

function documentFromFile( path ) {
	const content = fs.readFileSync( path, { encoding: 'utf8' } );

	const data = content
		.replace( /^\s*[\r\n]/gm, '' ) // strip leading and trailing lines/spaces
		.replace( /^#+|^={2,}|^-{2,}/gm, '' ); //strip common, noisy markdown stuff

	const firstLineEnd = data.indexOf( '\n' );

	if ( firstLineEnd === -1 ) {
		//this must be an empty file
		return {};
	}

	const title = data.slice( 0, firstLineEnd );
	const body = data.slice( firstLineEnd + 1 );

	return {
		path,
		content,
		title,
		body,
	};
}

main();