File size: 6,697 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import fs from 'fs';
import fspath from 'path';
import config from '@automattic/calypso-config';
import express from 'express';
import { escapeRegExp, find, escape as escapeHTML, once } from 'lodash';
import lunr from 'lunr';
import { marked } from 'marked';
import Prism from 'prismjs';
import 'prismjs/components/prism-jsx';
import 'prismjs/components/prism-json';
import 'prismjs/components/prism-scss';
import searchSelectors from './selectors';

const loadSearchIndex = once( async () => {
	const searchIndexPath = fspath.resolve( __dirname, '../../../build/devdocs-search-index.json' );
	const searchIndex = await fs.promises.readFile( searchIndexPath, 'utf-8' );
	const { index, documents } = JSON.parse( searchIndex );
	return {
		documents,
		index: lunr.Index.load( index ),
	};
} );

/**
 * Constants
 */
const SNIPPET_PAD_LENGTH = 40;
const DEFAULT_SNIPPET_LENGTH = 100;

// Alias `javascript` language to `es6`
Prism.languages.es6 = Prism.languages.javascript;

// Configure marked to use Prism for code-block highlighting.
marked.setOptions( {
	highlight: function ( code, language ) {
		const syntax = Prism.languages[ language ];
		return syntax ? Prism.highlight( code, syntax ) : code;
	},
} );

/**
 * Query the index using lunr.
 * We store the documents and index in memory for speed,
 * and also because lunr.js is designed to be memory resident
 * @param {Object} query The search query for lunr
 * @returns {Array} The results from the query
 */
async function queryDocs( query ) {
	const { index, documents } = await loadSearchIndex();
	const results = index.search( query );
	return results.map( ( result ) => {
		const doc = documents[ result.ref ];

		return {
			path: doc.path,
			title: doc.title,
			snippet: makeSnippet( doc, query ),
		};
	} );
}

/**
 * Return an array of results based on the provided filenames
 * @param {Array} filePaths An array of file paths
 * @returns {Array} The results from the docs
 */
async function listDocs( filePaths ) {
	const { documents } = await loadSearchIndex();
	return filePaths.map( ( path ) => {
		const doc = find( documents, { path } );

		if ( doc ) {
			return {
				path,
				title: doc.title,
				snippet: defaultSnippet( doc ),
			};
		}

		return {
			path,
			title: 'Not found: ' + path,
			snippet: '',
		};
	} );
}

/**
 * Extract a snippet from a document, capturing text either side of
 * any term(s) featured in a whitespace-delimited search query.
 * We look for up to 3 matches in a document and concatenate them.
 * @param {Object} doc The document to extract the snippet from
 * @param {Object} query The query to be searched for
 * @returns {string} A snippet from the document
 */
function makeSnippet( doc, query ) {
	// generate a regex of the form /[^a-zA-Z](term1|term2)/ for the query "term1 term2"
	const termRegexMatchers = lunr
		.tokenizer( query )
		.map( ( token ) => token.update( escapeRegExp ) );
	const termRegexString = '[^a-zA-Z](' + termRegexMatchers.join( '|' ) + ')';
	const termRegex = new RegExp( termRegexString, 'gi' );
	const snippets = [];
	let match;

	// find up to 4 matches in the document and extract snippets to be joined together
	// TODO: detect when snippets overlap and merge them.
	while ( ( match = termRegex.exec( doc.body ) ) !== null && snippets.length < 4 ) {
		const matchStr = match[ 1 ];
		const index = match.index + 1;
		const before = doc.body.substring( index - SNIPPET_PAD_LENGTH, index );
		const after = doc.body.substring(
			index + matchStr.length,
			index + matchStr.length + SNIPPET_PAD_LENGTH
		);

		snippets.push( before + '<mark>' + matchStr + '</mark>' + after );
	}

	if ( snippets.length ) {
		return '…' + snippets.join( ' … ' ) + '…';
	}

	return defaultSnippet( doc );
}

/**
 * Generate a standardized snippet
 * @param {Object} doc The document from which to generate the snippet
 * @returns {string} The snippet
 */
function defaultSnippet( doc ) {
	const content = doc.body.substring( 0, DEFAULT_SNIPPET_LENGTH );
	return escapeHTML( content ) + '…';
}

function normalizeDocPath( path ) {
	// if the path is a directory, default to README.md in that dir
	if ( ! path.endsWith( '.md' ) ) {
		path = fspath.join( path, 'README.md' );
	}

	// Remove the optional leading `/` to make the path relative, i.e., convert `/client/README.md`
	// to `client/README.md`. The `path` query arg can use both forms.
	path = path.replace( /^\//, '' );

	return path;
}

export default function devdocs() {
	const app = express();

	// this middleware enforces access control
	app.use( '/devdocs/service', ( request, response, next ) => {
		if ( ! config.isEnabled( 'devdocs' ) ) {
			response.status( 404 );
			next( 'Not found' );
		} else {
			next();
		}
	} );

	// search the documents using a search phrase "q"
	app.get( '/devdocs/service/search', async ( request, response ) => {
		const { q: query } = request.query;

		if ( ! query ) {
			response.status( 400 ).json( { message: 'Missing required "q" parameter' } );
			return;
		}

		try {
			const result = await queryDocs( query );
			response.json( result );
		} catch ( error ) {
			console.error( error );
			response.status( 400 ).json( { message: 'Internal server error: no document index' } );
		}
	} );

	// return a listing of documents from filenames supplied in the "files" parameter
	app.get( '/devdocs/service/list', async ( request, response ) => {
		const { files } = request.query;

		if ( ! files ) {
			response.status( 400 ).json( { message: 'Missing required "files" parameter' } );
			return;
		}

		try {
			const result = await listDocs( files.split( ',' ) );
			response.json( result );
		} catch ( error ) {
			console.error( error );
			response.status( 400 ).json( { message: 'Internal server error: no document index' } );
		}
	} );

	// return the content of a document in the given format (assumes that the document is in
	// markdown format)
	app.get( '/devdocs/service/content', async ( request, response ) => {
		const { path, format = 'html' } = request.query;

		if ( ! path ) {
			response
				.status( 400 )
				.send( 'Need to provide a file path (e.g. path=client/devdocs/README.md)' );
			return;
		}

		try {
			const { documents } = await loadSearchIndex();
			const doc = find( documents, { path: normalizeDocPath( path ) } );

			if ( ! doc ) {
				response.status( 404 ).send( 'File does not exist' );
				return;
			}

			response.send( 'html' === format ? marked.parse( doc.content ) : doc.content );
		} catch ( error ) {
			console.error( error );
			response.status( 400 ).json( { message: 'Internal server error: no document index' } );
		}
	} );

	app.get( '/devdocs/service/selectors', searchSelectors );

	return app;
}