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

View File

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