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

Function loop

notes/docsify/unpkg/docsify/lib/plugins/search.js:88–148  ·  view source on GitHub ↗
( i )

Source from the content-addressed store, hash-verified

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 '...';
133
134 resultStr += matchContent;
135 }
136 });
137
138 if (isMatch) {
139 var matchingPost = {
140 title: escapeHtml(postTitle),
141 content: resultStr,
142 url: postUrl
143 };
144
145 matchingResults.push(matchingPost);

Callers 1

searchFunction · 0.85

Calls 1

escapeHtmlFunction · 0.85

Tested by

no test coverage detected