MCPcopy Create free account
hub / github.com/frank-lam/fullstack-tutorial / search

Function search

notes/docsify/unpkg/docsify/lib/plugins/search.js:75–153  ·  view source on GitHub ↗

* @param {String} query * @returns {Array}

(query)

Source from the content-addressed store, hash-verified

73 * @returns {Array}
74 */
75function search(query) {
76 var matchingResults = [];
77 var data = [];
78 Object.keys(INDEXS).forEach(function (key) {
79 data = data.concat(Object.keys(INDEXS[key]).map(function (page) { return INDEXS[key][page]; }));
80 });
81
82 query = query.trim();
83 var keywords = query.split(/[\s\-,\\/]+/);
84 if (keywords.length !== 1) {
85 keywords = [].concat(query, keywords);
86 }
87
88 var loop = function ( i ) {
89 var post = data[i];
90 var isMatch = false;
91 var resultStr = '';
92 var postTitle = post.title && post.title.trim();
93 var postContent = post.body && post.body.trim();
94 var postUrl = post.slug || '';
95
96 if (postTitle && postContent) {
97 keywords.forEach(function (keyword) {
98 // From https://github.com/sindresorhus/escape-string-regexp
99 var regEx = new RegExp(
100 keyword.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
101 'gi'
102 );
103 var indexTitle = -1;
104 var indexContent = -1;
105
106 indexTitle = postTitle && postTitle.search(regEx);
107 indexContent = postContent && postContent.search(regEx);
108
109 if (indexTitle < 0 && indexContent < 0) {
110 isMatch = false;
111 } else {
112 isMatch = true;
113 if (indexContent < 0) {
114 indexContent = 0;
115 }
116
117 var start = 0;
118 var end = 0;
119
120 start = indexContent < 11 ? 0 : indexContent - 10;
121 end = start === 0 ? 70 : indexContent + keyword.length + 60;
122
123 if (end > postContent.length) {
124 end = postContent.length;
125 }
126
127 var matchContent =
128 '...' +
129 escapeHtml(postContent)
130 .substring(start, end)
131 .replace(regEx, ("<em class=\"search-keyword\">" + keyword + "</em>")) +
132 '...';

Callers 1

doSearchFunction · 0.85

Calls 1

loopFunction · 0.85

Tested by

no test coverage detected