|
| 1 | +(function () { |
| 2 | +var INDEXS = {}; |
| 3 | + |
| 4 | +function escapeHtml(string) { |
| 5 | + var entityMap = { |
| 6 | + '&': '&', |
| 7 | + '<': '<', |
| 8 | + '>': '>', |
| 9 | + '"': '"', |
| 10 | + '\'': ''', |
| 11 | + '/': '/' |
| 12 | + }; |
| 13 | + |
| 14 | + return String(string).replace(/[&<>"'/]/g, function (s) { return entityMap[s]; }) |
| 15 | +} |
| 16 | + |
| 17 | +function getAllPaths(router) { |
| 18 | + var paths = []; |
| 19 | + |
| 20 | + Docsify.dom.findAll('.sidebar-nav a:not(.section-link):not([data-nosearch])').forEach(function (node) { |
| 21 | + var href = node.href; |
| 22 | + var originHref = node.getAttribute('href'); |
| 23 | + var path = router.parse(href).path; |
| 24 | + |
| 25 | + if ( |
| 26 | + path && |
| 27 | + paths.indexOf(path) === -1 && |
| 28 | + !Docsify.util.isAbsolutePath(originHref) |
| 29 | + ) { |
| 30 | + paths.push(path); |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + return paths |
| 35 | +} |
| 36 | + |
| 37 | +function saveData(maxAge) { |
| 38 | + localStorage.setItem('docsify.search.expires', Date.now() + maxAge); |
| 39 | + localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS)); |
| 40 | +} |
| 41 | + |
| 42 | +function genIndex(path, content, router, depth) { |
| 43 | + if ( content === void 0 ) content = ''; |
| 44 | + |
| 45 | + var tokens = window.marked.lexer(content); |
| 46 | + var slugify = window.Docsify.slugify; |
| 47 | + var index = {}; |
| 48 | + var slug; |
| 49 | + |
| 50 | + tokens.forEach(function (token) { |
| 51 | + if (token.type === 'heading' && token.depth <= depth) { |
| 52 | + slug = router.toURL(path, {id: slugify(token.text)}); |
| 53 | + index[slug] = {slug: slug, title: token.text, body: ''}; |
| 54 | + } else { |
| 55 | + if (!slug) { |
| 56 | + return |
| 57 | + } |
| 58 | + if (!index[slug]) { |
| 59 | + index[slug] = {slug: slug, title: '', body: ''}; |
| 60 | + } else if (index[slug].body) { |
| 61 | + index[slug].body += '\n' + (token.text || ''); |
| 62 | + } else { |
| 63 | + index[slug].body = token.text; |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | + slugify.clear(); |
| 68 | + return index |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * @param {String} query |
| 73 | + * @returns {Array} |
| 74 | + */ |
| 75 | +function 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 | + '...'; |
| 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); |
| 146 | + } |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + for (var i = 0; i < data.length; i++) loop( i ); |
| 151 | + |
| 152 | + return matchingResults |
| 153 | +} |
| 154 | + |
| 155 | +function init$1(config, vm) { |
| 156 | + var isAuto = config.paths === 'auto'; |
| 157 | + var isExpired = localStorage.getItem('docsify.search.expires') < Date.now(); |
| 158 | + |
| 159 | + INDEXS = JSON.parse(localStorage.getItem('docsify.search.index')); |
| 160 | + |
| 161 | + if (isExpired) { |
| 162 | + INDEXS = {}; |
| 163 | + } else if (!isAuto) { |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + var paths = isAuto ? getAllPaths(vm.router) : config.paths; |
| 168 | + var len = paths.length; |
| 169 | + var count = 0; |
| 170 | + |
| 171 | + paths.forEach(function (path) { |
| 172 | + if (INDEXS[path]) { |
| 173 | + return count++ |
| 174 | + } |
| 175 | + |
| 176 | + Docsify |
| 177 | + .get(vm.router.getFile(path), false, vm.config.requestHeaders) |
| 178 | + .then(function (result) { |
| 179 | + INDEXS[path] = genIndex(path, result, vm.router, config.depth); |
| 180 | + len === ++count && saveData(config.maxAge); |
| 181 | + }); |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +var NO_DATA_TEXT = ''; |
| 186 | +var options; |
| 187 | + |
| 188 | +function style() { |
| 189 | + var code = "\n.sidebar {\n padding-top: 0;\n}\n\n.search {\n margin-bottom: 20px;\n padding: 6px;\n border-bottom: 1px solid #eee;\n}\n\n.search .input-wrap {\n display: flex;\n align-items: center;\n}\n\n.search .results-panel {\n display: none;\n}\n\n.search .results-panel.show {\n display: block;\n}\n\n.search input {\n outline: none;\n border: none;\n width: 100%;\n padding: 0 7px;\n line-height: 36px;\n font-size: 14px;\n}\n\n.search input::-webkit-search-decoration,\n.search input::-webkit-search-cancel-button,\n.search input {\n -webkit-appearance: none;\n -moz-appearance: none;\n appearance: none;\n}\n.search .clear-button {\n width: 36px;\n text-align: right;\n display: none;\n}\n\n.search .clear-button.show {\n display: block;\n}\n\n.search .clear-button svg {\n transform: scale(.5);\n}\n\n.search h2 {\n font-size: 17px;\n margin: 10px 0;\n}\n\n.search a {\n text-decoration: none;\n color: inherit;\n}\n\n.search .matching-post {\n border-bottom: 1px solid #eee;\n}\n\n.search .matching-post:last-child {\n border-bottom: 0;\n}\n\n.search p {\n font-size: 14px;\n overflow: hidden;\n text-overflow: ellipsis;\n display: -webkit-box;\n -webkit-line-clamp: 2;\n -webkit-box-orient: vertical;\n}\n\n.search p.empty {\n text-align: center;\n}\n\n.app-name.hide, .sidebar-nav.hide {\n display: none;\n}"; |
| 190 | + |
| 191 | + Docsify.dom.style(code); |
| 192 | +} |
| 193 | + |
| 194 | +function tpl(defaultValue) { |
| 195 | + if ( defaultValue === void 0 ) defaultValue = ''; |
| 196 | + |
| 197 | + var html = |
| 198 | + "<div class=\"input-wrap\">\n <input type=\"search\" value=\"" + defaultValue + "\" />\n <div class=\"clear-button\">\n <svg width=\"26\" height=\"24\">\n <circle cx=\"12\" cy=\"12\" r=\"11\" fill=\"#ccc\" />\n <path stroke=\"white\" stroke-width=\"2\" d=\"M8.25,8.25,15.75,15.75\" />\n <path stroke=\"white\" stroke-width=\"2\"d=\"M8.25,15.75,15.75,8.25\" />\n </svg>\n </div>\n </div>\n <div class=\"results-panel\"></div>\n </div>"; |
| 199 | + var el = Docsify.dom.create('div', html); |
| 200 | + var aside = Docsify.dom.find('aside'); |
| 201 | + |
| 202 | + Docsify.dom.toggleClass(el, 'search'); |
| 203 | + Docsify.dom.before(aside, el); |
| 204 | +} |
| 205 | + |
| 206 | +function doSearch(value) { |
| 207 | + var $search = Docsify.dom.find('div.search'); |
| 208 | + var $panel = Docsify.dom.find($search, '.results-panel'); |
| 209 | + var $clearBtn = Docsify.dom.find($search, '.clear-button'); |
| 210 | + var $sidebarNav = Docsify.dom.find('.sidebar-nav'); |
| 211 | + var $appName = Docsify.dom.find('.app-name'); |
| 212 | + |
| 213 | + if (!value) { |
| 214 | + $panel.classList.remove('show'); |
| 215 | + $clearBtn.classList.remove('show'); |
| 216 | + $panel.innerHTML = ''; |
| 217 | + |
| 218 | + if (options.hideOtherSidebarContent) { |
| 219 | + $sidebarNav.classList.remove('hide'); |
| 220 | + $appName.classList.remove('hide'); |
| 221 | + } |
| 222 | + return |
| 223 | + } |
| 224 | + var matchs = search(value); |
| 225 | + |
| 226 | + var html = ''; |
| 227 | + matchs.forEach(function (post) { |
| 228 | + html += "<div class=\"matching-post\">\n<a href=\"" + (post.url) + "\">\n<h2>" + (post.title) + "</h2>\n<p>" + (post.content) + "</p>\n</a>\n</div>"; |
| 229 | + }); |
| 230 | + |
| 231 | + $panel.classList.add('show'); |
| 232 | + $clearBtn.classList.add('show'); |
| 233 | + $panel.innerHTML = html || ("<p class=\"empty\">" + NO_DATA_TEXT + "</p>"); |
| 234 | + if (options.hideOtherSidebarContent) { |
| 235 | + $sidebarNav.classList.add('hide'); |
| 236 | + $appName.classList.add('hide'); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +function bindEvents() { |
| 241 | + var $search = Docsify.dom.find('div.search'); |
| 242 | + var $input = Docsify.dom.find($search, 'input'); |
| 243 | + var $inputWrap = Docsify.dom.find($search, '.input-wrap'); |
| 244 | + |
| 245 | + var timeId; |
| 246 | + // Prevent to Fold sidebar |
| 247 | + Docsify.dom.on( |
| 248 | + $search, |
| 249 | + 'click', |
| 250 | + function (e) { return e.target.tagName !== 'A' && e.stopPropagation(); } |
| 251 | + ); |
| 252 | + Docsify.dom.on($input, 'input', function (e) { |
| 253 | + clearTimeout(timeId); |
| 254 | + timeId = setTimeout(function (_) { return doSearch(e.target.value.trim()); }, 100); |
| 255 | + }); |
| 256 | + Docsify.dom.on($inputWrap, 'click', function (e) { |
| 257 | + // Click input outside |
| 258 | + if (e.target.tagName !== 'INPUT') { |
| 259 | + $input.value = ''; |
| 260 | + doSearch(); |
| 261 | + } |
| 262 | + }); |
| 263 | +} |
| 264 | + |
| 265 | +function updatePlaceholder(text, path) { |
| 266 | + var $input = Docsify.dom.getNode('.search input[type="search"]'); |
| 267 | + |
| 268 | + if (!$input) { |
| 269 | + return |
| 270 | + } |
| 271 | + if (typeof text === 'string') { |
| 272 | + $input.placeholder = text; |
| 273 | + } else { |
| 274 | + var match = Object.keys(text).filter(function (key) { return path.indexOf(key) > -1; })[0]; |
| 275 | + $input.placeholder = text[match]; |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | +function updateNoData(text, path) { |
| 280 | + if (typeof text === 'string') { |
| 281 | + NO_DATA_TEXT = text; |
| 282 | + } else { |
| 283 | + var match = Object.keys(text).filter(function (key) { return path.indexOf(key) > -1; })[0]; |
| 284 | + NO_DATA_TEXT = text[match]; |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +function updateOptions(opts) { |
| 289 | + options = opts; |
| 290 | +} |
| 291 | + |
| 292 | +function init(opts, vm) { |
| 293 | + var keywords = vm.router.parse().query.s; |
| 294 | + |
| 295 | + updateOptions(opts); |
| 296 | + style(); |
| 297 | + tpl(keywords); |
| 298 | + bindEvents(); |
| 299 | + keywords && setTimeout(function (_) { return doSearch(keywords); }, 500); |
| 300 | +} |
| 301 | + |
| 302 | +function update(opts, vm) { |
| 303 | + updateOptions(opts); |
| 304 | + updatePlaceholder(opts.placeholder, vm.route.path); |
| 305 | + updateNoData(opts.noData, vm.route.path); |
| 306 | +} |
| 307 | + |
| 308 | +var CONFIG = { |
| 309 | + placeholder: 'Type to search', |
| 310 | + noData: 'No Results!', |
| 311 | + paths: 'auto', |
| 312 | + depth: 2, |
| 313 | + maxAge: 86400000, // 1 day |
| 314 | + hideOtherSidebarContent: false |
| 315 | +}; |
| 316 | + |
| 317 | +var install = function (hook, vm) { |
| 318 | + var util = Docsify.util; |
| 319 | + var opts = vm.config.search || CONFIG; |
| 320 | + |
| 321 | + if (Array.isArray(opts)) { |
| 322 | + CONFIG.paths = opts; |
| 323 | + } else if (typeof opts === 'object') { |
| 324 | + CONFIG.paths = Array.isArray(opts.paths) ? opts.paths : 'auto'; |
| 325 | + CONFIG.maxAge = util.isPrimitive(opts.maxAge) ? opts.maxAge : CONFIG.maxAge; |
| 326 | + CONFIG.placeholder = opts.placeholder || CONFIG.placeholder; |
| 327 | + CONFIG.noData = opts.noData || CONFIG.noData; |
| 328 | + CONFIG.depth = opts.depth || CONFIG.depth; |
| 329 | + CONFIG.hideOtherSidebarContent = opts.hideOtherSidebarContent || CONFIG.hideOtherSidebarContent; |
| 330 | + } |
| 331 | + |
| 332 | + var isAuto = CONFIG.paths === 'auto'; |
| 333 | + |
| 334 | + hook.mounted(function (_) { |
| 335 | + init(CONFIG, vm); |
| 336 | + !isAuto && init$1(CONFIG, vm); |
| 337 | + }); |
| 338 | + hook.doneEach(function (_) { |
| 339 | + update(CONFIG, vm); |
| 340 | + isAuto && init$1(CONFIG, vm); |
| 341 | + }); |
| 342 | +}; |
| 343 | + |
| 344 | +$docsify.plugins = [].concat(install, $docsify.plugins); |
| 345 | + |
| 346 | +}()); |
0 commit comments