Docs: add parameter names to search keywords

This commit is contained in:
Lovell Fuller 2021-05-23 18:02:18 +01:00
parent d72852b3aa
commit ed3377cb2d
3 changed files with 13 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@ -2,7 +2,7 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { extractDescription, extractKeywords } = require('./extract'); const { extractDescription, extractKeywords, extractParameters } = require('./extract');
const searchIndex = []; const searchIndex = [];
@ -37,18 +37,19 @@ for (const match of matches) {
].forEach((section) => { ].forEach((section) => {
const contents = fs.readFileSync(path.join(__dirname, '..', `api-${section}.md`), 'utf8'); const contents = fs.readFileSync(path.join(__dirname, '..', `api-${section}.md`), 'utf8');
const matches = contents.matchAll( const matches = contents.matchAll(
/\n## (?<title>[A-Za-z]+)\n\n(?<firstparagraph>.+?)\n\n/gs /\n## (?<title>[A-Za-z]+)\n\n(?<firstparagraph>.+?)\n\n(?<parameters>### Parameters*.+?)#/gs
); );
for (const match of matches) { for (const match of matches) {
const { title, firstparagraph } = match.groups; const { title, firstparagraph, parameters } = match.groups;
const description = firstparagraph.startsWith('###') const description = firstparagraph.startsWith('###')
? 'Constructor' ? 'Constructor'
: extractDescription(firstparagraph); : extractDescription(firstparagraph);
const parameterNames = extractParameters(parameters);
searchIndex.push({ searchIndex.push({
t: title, t: title,
d: description, d: description,
k: extractKeywords(`${title} ${description}`), k: extractKeywords(`${title} ${description} ${parameterNames}`),
l: `/api-${section}#${title.toLowerCase()}` l: `/api-${section}#${title.toLowerCase()}`
}); });
} }

View File

@ -4,6 +4,7 @@ const stopWords = require('./stop-words');
const extractDescription = (str) => const extractDescription = (str) =>
str str
.replace(/### Examples.*/sg, '')
.replace(/\(http[^)]+/g, '') .replace(/\(http[^)]+/g, '')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.replace(/[^A-Za-z0-9_/\-,. ]/g, '') .replace(/[^A-Za-z0-9_/\-,. ]/g, '')
@ -11,6 +12,11 @@ const extractDescription = (str) =>
.substr(0, 180) .substr(0, 180)
.trim(); .trim();
const extractParameters = (str) =>
[...str.matchAll(/options\.(?<name>[^.`]+)/gs)]
.map((match) => match.groups.name)
.join(' ');
const extractKeywords = (str) => const extractKeywords = (str) =>
[ [
...new Set( ...new Set(
@ -21,4 +27,4 @@ const extractKeywords = (str) =>
) )
].join(' '); ].join(' ');
module.exports = { extractDescription, extractKeywords }; module.exports = { extractDescription, extractKeywords, extractParameters };