From f2331319743204daab7959956605d6b93aea1d1c Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 23 Oct 2018 11:35:18 +0100 Subject: [PATCH] build(docs-infra): refactor generateKeywords processor (#26676) PR Close #26676 --- .../processors/generateKeywords.js | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/aio/tools/transforms/angular-base-package/processors/generateKeywords.js b/aio/tools/transforms/angular-base-package/processors/generateKeywords.js index 38236978c2..485b44d803 100644 --- a/aio/tools/transforms/angular-base-package/processors/generateKeywords.js +++ b/aio/tools/transforms/angular-base-package/processors/generateKeywords.js @@ -47,40 +47,6 @@ module.exports = function generateKeywordsProcessor(log, readFilesProcessor) { var ignoreWordsMap = convertToMap(wordsToIgnore); - // If the heading contains a name starting with ng, e.g. "ngController", then add the - // name without the ng to the text, e.g. "controller". - function tokenize(text) { - const rawTokens = text.split(/[\s\/]+/mg); - const tokens = []; - rawTokens.forEach(token => { - // Strip off unwanted trivial characters - token = token - .trim() - .replace(/^[_\-"'`({[<$*)}\]>.]+/, '') - .replace(/[_\-"'`({[<$*)}\]>.]+$/, ''); - // Ignore tokens that contain weird characters - if (/^[\w.\-]+$/.test(token)) { - tokens.push(token.toLowerCase()); - const ngTokenMatch = /^[nN]g([A-Z]\w*)/.exec(token); - if (ngTokenMatch) { - tokens.push(ngTokenMatch[1].toLowerCase()); - } - } - }); - return tokens; - } - - function extractWords(text, words, keywordMap) { - var tokens = tokenize(text); - tokens.forEach(function(token) { - if (!keywordMap[token]) { - words.push(token); - keywordMap[token] = true; - } - }); - } - - const filteredDocs = docs // We are not interested in some docTypes .filter(function(doc) { return !docTypesToIgnore[doc.docType]; }) @@ -104,14 +70,10 @@ module.exports = function generateKeywordsProcessor(log, readFilesProcessor) { if (isString(value) && !propertiesToIgnore[key]) { extractWords(value, words, keywordMap); } - - // Special case properties that contain content relating to "members" - // of a doc that represents, say, a class or interface - if (key === 'members' || key === 'statics') { - value.forEach(function(member) { extractWords(member.name, members, membersMap); }); - } }); + extractMemberWords(doc, members, membersMap); + // Extract all the keywords from the headings if (doc.vFile && doc.vFile.headings) { Object.keys(doc.vFile.headings).forEach(function(headingTag) { @@ -166,4 +128,46 @@ function convertToMap(collection) { const obj = {}; collection.forEach(key => { obj[key] = true; }); return obj; +} + +// If the heading contains a name starting with ng, e.g. "ngController", then add the +// name without the ng to the text, e.g. "controller". +function tokenize(text) { + const rawTokens = text.split(/[\s\/]+/mg); + const tokens = []; + rawTokens.forEach(token => { + // Strip off unwanted trivial characters + token = token + .trim() + .replace(/^[_\-"'`({[<$*)}\]>.]+/, '') + .replace(/[_\-"'`({[<$*)}\]>.]+$/, ''); + // Ignore tokens that contain weird characters + if (/^[\w.\-]+$/.test(token)) { + tokens.push(token.toLowerCase()); + const ngTokenMatch = /^[nN]g([A-Z]\w*)/.exec(token); + if (ngTokenMatch) { + tokens.push(ngTokenMatch[1].toLowerCase()); + } + } + }); + return tokens; +} + +function extractWords(text, words, keywordMap) { + var tokens = tokenize(text); + tokens.forEach(function(token) { + if (!keywordMap[token]) { + words.push(token); + keywordMap[token] = true; + } + }); +} + +function extractMemberWords(doc, members, membersMap) { + if (doc.members) { + doc.members.forEach(function(member) { extractWords(member.name, members, membersMap); }); + } + if (doc.statics) { + doc.statics.forEach(function(member) { extractWords(member.name, members, membersMap); }); + } } \ No newline at end of file