|
|
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 ), |
|
|
}; |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SNIPPET_PAD_LENGTH = 40; |
|
|
const DEFAULT_SNIPPET_LENGTH = 100; |
|
|
|
|
|
|
|
|
Prism.languages.es6 = Prism.languages.javascript; |
|
|
|
|
|
|
|
|
marked.setOptions( { |
|
|
highlight: function ( code, language ) { |
|
|
const syntax = Prism.languages[ language ]; |
|
|
return syntax ? Prism.highlight( code, syntax ) : code; |
|
|
}, |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ), |
|
|
}; |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: '', |
|
|
}; |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function makeSnippet( doc, query ) { |
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
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 ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function defaultSnippet( doc ) { |
|
|
const content = doc.body.substring( 0, DEFAULT_SNIPPET_LENGTH ); |
|
|
return escapeHTML( content ) + '…'; |
|
|
} |
|
|
|
|
|
function normalizeDocPath( path ) { |
|
|
|
|
|
if ( ! path.endsWith( '.md' ) ) { |
|
|
path = fspath.join( path, 'README.md' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
path = path.replace( /^\//, '' ); |
|
|
|
|
|
return path; |
|
|
} |
|
|
|
|
|
export default function devdocs() { |
|
|
const app = express(); |
|
|
|
|
|
|
|
|
app.use( '/devdocs/service', ( request, response, next ) => { |
|
|
if ( ! config.isEnabled( 'devdocs' ) ) { |
|
|
response.status( 404 ); |
|
|
next( 'Not found' ); |
|
|
} else { |
|
|
next(); |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
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' } ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
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' } ); |
|
|
} |
|
|
} ); |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
|