diff --git a/assets/js/helpers.js b/assets/js/helpers.js index 9bf003bce..8c853b4de 100644 --- a/assets/js/helpers.js +++ b/assets/js/helpers.js @@ -6,7 +6,6 @@ import $ from 'jquery' import find from 'lodash.find' -import findKey from 'lodash.findkey' // Escape a string for use in a regular expression export function escapeText (text) { @@ -20,13 +19,14 @@ export function getModuleType () { // Find out if the anchor belongs to either // Types, Functions, Macros or Callbacks export function findSidebarCategory (items, query) { + if (!items) return + for (let item of items) { - const res = findKey(item, (value, key) => { - const a = find(value, ({anchor}) => anchor === query) - return a + const res = find(item.nodeGroups, ({nodes}) => { + return find(nodes, ({anchor}) => anchor === query) }) - if (res) return res + if (res) return res.key } } diff --git a/assets/js/search.js b/assets/js/search.js index 09bf93f3e..49c07da26 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -32,18 +32,20 @@ function cleaner (element) { return !!element } -function findNested (elements, parentId, matcher) { - return (elements || []).map(function (element) { +function findNested (elements, parentId, matcher, acc) { + return (elements || []).reduce((acc, element) => { // Match things like module.func var parentMatch = (parentId + '.' + element.id).match(matcher) var match = element.id && element.id.match(matcher) - if (parentMatch || match) { + if ((parentMatch || match) && !acc[element.id]) { var result = JSON.parse(JSON.stringify(element)) result.match = match ? highlight(match) : element.id - return result + acc[result.id] = result } - }).filter(cleaner) + + return acc + }, acc || {}) } function pushLevel (levels, searchEntity, name) { @@ -54,29 +56,35 @@ function pushLevel (levels, searchEntity, name) { export function findIn (elements, matcher) { return elements.map(function (element) { - var title = element.title - var titleMatch = title && title.match(matcher) - var functionMatches = findNested(element.functions, title, matcher) - var guardMatches = findNested(element.guards, title, matcher) - var callbackMatches = findNested(element.callbacks, title, matcher) - var typeMatches = findNested(element.types, title, matcher) - - var result = { + let title = element.title + let titleMatch = title && title.match(matcher) + let result = { id: element.id, match: titleMatch ? highlight(titleMatch) : element.title } + let hasMatch = !!titleMatch + + if (element.nodeGroups) { + for (let {key, nodes} of element.nodeGroups) { + let matches = findNested(nodes, title, matcher, result[key]) + if (Object.keys(matches).length > 0) { + hasMatch = true + if (key === 'types' || key === 'callbacks') { + result[key] = matches + } else { + result.functions = matches + } + } + } + } + + if (hasMatch) { + for (let key in result) { + if (key !== 'id' && key !== 'match') { + result[key] = Object.values(result[key]).sort((a, b) => a.id.localeCompare(b.id)) + } + } - if (functionMatches.length > 0) result.functions = functionMatches - if (guardMatches.length > 0) result.guards = guardMatches - if (callbackMatches.length > 0) result.callbacks = callbackMatches - if (typeMatches.length > 0) result.types = typeMatches - - if (titleMatch || - functionMatches.length > 0 || - guardMatches.length > 0 || - callbackMatches.length > 0 || - typeMatches.length > 0 - ) { return result } }).filter(cleaner) diff --git a/assets/js/template-helpers/isEmptyNode.js b/assets/js/template-helpers/isEmptyNode.js index 5228e0fd0..4dc6825ae 100644 --- a/assets/js/template-helpers/isEmptyNode.js +++ b/assets/js/template-helpers/isEmptyNode.js @@ -1,17 +1,19 @@ export default function (node, options) { - var nodeItems = [ - node.headers, - node.types, - node.functions, - node.guards, - node.callbacks - ] + if (hasItems(node.headers)) { + return options.inverse(this) + } - for (var i = 0; i < nodeItems.length; i++) { - if (Array.isArray(nodeItems[i]) && (nodeItems[i].length > 0)) { - return options.inverse(this) + if (node.nodeGroups) { + for (let {nodes} of node.nodeGroups) { + if (hasItems(nodes)) { + return options.inverse(this) + } } } return options.fn(this) } + +function hasItems (items) { + return Array.isArray(items) && (items.length > 0) +} diff --git a/assets/js/template-helpers/showSummary.js b/assets/js/template-helpers/showSummary.js index db78f44ca..d362df463 100644 --- a/assets/js/template-helpers/showSummary.js +++ b/assets/js/template-helpers/showSummary.js @@ -1,5 +1,5 @@ export default function (node, options) { - if (node.types || node.functions || node.guards || node.callbacks) { + if (node.nodeGroups) { return options.fn(this) } } diff --git a/assets/js/templates/search-results.handlebars b/assets/js/templates/search-results.handlebars index 9fe25cf19..bcd93db82 100644 --- a/assets/js/templates/search-results.handlebars +++ b/assets/js/templates/search-results.handlebars @@ -10,11 +10,6 @@

{{{match}}}

-