1 line
14 KiB
Plaintext
1 line
14 KiB
Plaintext
{"version":3,"sources":["local-search.js"],"names":["searchEscape","keyword","htmlEntityMap","replace","i","regEscape","regEntityMap","$","getParam","reqParam","results","RegExp","exec","window","location","decodeURIComponent","setNotice","type","info","searchInfo","document","getElementById","className","innerText","clearPosts","innerHTML","createPosts","resArr","resultSectionElement","resultString","forEach","resInfo","pageInfo","pageTags","tags","tag","postTagTemplate","concat","postTemplate","category","link","title","content","date","loadDataSearch","searchDataFile","skeys","fetch","then","res","NProgress","inc","json","datas","startTime","performance","now","resultArray","resultCount","keywords","trim","toLowerCase","split","data","matched","dataTitle","dataContent","dataWeight","indexs","halfLenth","indexOf","firstOccur","lastOccur","tPage","Date","toLocaleDateString","categories","url","regS","start","end","length","substr","push","finishTime","Math","round","sort","a","b","done","catch","error","keySearch","inpSearch","value","history","pushState","href"],"mappings":"AAAA,aAAA,SAASA,aAAaC,GAClB,IAAMC,EAAgB,CAClB,IAAK,QACL,IAAK,OACL,IAAK,OACL,IAAK,SACL,IAAM,QACN,IAAK,UAGT,OAAOD,EAAQE,QAAQ,aAAa,SAAUC,GAC1C,OAAOF,EAAcE,MAI7B,SAASC,UAAUJ,GACf,IAAMK,EAAe,CACjB,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACLC,EAAK,OAGT,OAAON,EAAQE,QAAQ,+BAA+B,SAAUC,GAC5D,OAAOE,EAAaF,MAI5B,SAASI,SAASC,GAEdA,EAAWA,EAASN,QAAQ,OAAQ,OAAQA,QAAQ,OAAQ,OAC5D,IACMO,EADU,IAAIC,OAAO,SAAWF,EAAW,aACzBG,KAAKC,OAAOC,UACpC,OAAmB,OAAZJ,EAAmB,GAAKK,mBAAmBL,EAAQ,GAAGP,QAAQ,MAAO,MAGhF,SAASa,UAAUC,EAAMC,GACrB,IAAMC,EAAaC,SAASC,eAAe,oBAC3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,GAC9CpB,EAAS7B,SAAQ,SAAC7B,GACd,IAAM6E,EAAO,IAAInE,OAAON,UAAUJ,GAAW,QAAS,MACtDwE,EAAM/B,QAAU+B,EAAM/B,QAAQvC,QAAQ2E,EAAM,gBAGpDrB,EAAY0B,KAAK,CAACV,EAAON,SAIb,IAAhBT,EAAmB,CACnB,IAAM0B,EAAa7B,YAAYC,MAC/BxC,UAAU,UAAW,MAAQ0C,EAAc,aAAe2B,KAAKC,MAA+B,KAAxBF,EAAa9B,IAAgB,IAAM,QACzGG,EAAY8B,MAAK,SAACC,EAAGC,GACjB,OAAOA,EAAE,GAAKD,EAAE,MAEpB9D,YAAY+B,QAEZzC,UAAU,SAAU,eACpBQ,aAEqB,oBAAd0B,WACPA,UAAUwC,UAGjBC,UAAM,SAACC,GACJ5E,UAAU,SAAU,QAAU4E,MAI1C,SAASC,UAAU/C,GAEf9B,UAAU,OAAQ,eAGO,oBAAdkC,WACPA,UAAU6B,QAIdnC,eAAeC,eAAgB7C,aAAa8C,IAGhD,SAASgD,YAEL,IAAMhD,EAAQ1B,SAASC,eAAe,gBAAgB0E,MAKtD,OAHAlF,OAAOmF,QAAQC,UAAU,GAAG,EAAEpF,OAAOC,SAASoF,KAAKpC,MAAM,KAAK,GAAG,MAAQhB,EAAM3C,QAAQ,MAAO,MAE9F0F,UAAU/C,IACH,GAGX,WACI,IAAMA,EAAQtC,SAAS,KACT,KAAVsC,IAEA1B,SAASC,eAAe,gBAAgB0E,MAAQjD,EAEhD+C,UAAU/C,IANlB","file":"../js/local-search.min.js","sourcesContent":["function searchEscape(keyword) {\r\n const htmlEntityMap = {\r\n '&': '&',\r\n '<': '<',\r\n '>': '>',\r\n '\"': '"',\r\n '\\'': ''',\r\n '/': '/'\r\n };\r\n\r\n return keyword.replace(/[&<>\"'/]/g, function (i) {\r\n return htmlEntityMap[i];\r\n });\r\n}\r\n\r\nfunction regEscape(keyword) {\r\n const regEntityMap = {\r\n '{': '\\\\\\{',\r\n '}': '\\\\\\}',\r\n '[': '\\\\\\[',\r\n ']': '\\\\\\]',\r\n '(': '\\\\\\(',\r\n ')': '\\\\\\)',\r\n '?': '\\\\\\?',\r\n '*': '\\\\\\*',\r\n '.': '\\\\\\.',\r\n '+': '\\\\\\+',\r\n '^': '\\\\\\^',\r\n '$': '\\\\\\$'\r\n };\r\n\r\n return keyword.replace(/[\\{\\}\\[\\]\\(\\)\\?\\*\\.\\+\\^\\$]/g, function (i) {\r\n return regEntityMap[i];\r\n });\r\n}\r\n\r\nfunction getParam(reqParam) {\r\n // 获取参数\r\n reqParam = reqParam.replace(/[\\[]/, \"\\\\\\[\").replace(/[\\]]/, \"\\\\\\]\");\r\n const paraReg = new RegExp('[\\\\?&]' + reqParam + '=([^&#]*)');\r\n const results = paraReg.exec(window.location);\r\n return results === null ? '' : decodeURIComponent(results[1].replace(/\\+/g, ' '));\r\n}\r\n\r\nfunction setNotice(type, info) {\r\n const searchInfo = document.getElementById('kr-search-notice');\r\n searchInfo.className = 'alert alert-' + type;\r\n searchInfo.innerText = info;\r\n}\r\n\r\nfunction clearPosts() {\r\n const resultSectionElement = document.getElementById('result-posts');\r\n resultSectionElement.innerHTML = '';\r\n}\r\n\r\nfunction createPosts(resArr) {\r\n const resultSectionElement = document.getElementById('result-posts');\r\n let resultString = '';\r\n\r\n resArr.forEach((resInfo)=>{\r\n const pageInfo = resInfo[0];\r\n let pageTags = '';\r\n pageInfo.tags.forEach((tag, i)=>{\r\n pageTags += i ? ', ' : '';\r\n const postTagTemplate = `<a class=\"tag-link\" href=\"${tag[1]}\" rel=\"tag\">${tag[0]}</a>`;\r\n pageTags += postTagTemplate;\r\n });\r\n const postTemplate = `\r\n <article class=\"kratos-hentry clearfix\">\r\n <div class=\"kratos-entry-border-new clearfix\">\r\n <div class=\"kratos-post-inner-new kr-search-result\">\r\n <header class=\"kratos-entry-header-new\">\r\n ${ pageInfo.category[0]!=='undefined' ? `<a class=\"label-link\" href=\"${pageInfo.category[1]}\">${pageInfo.category[0]}</a>` : '' }\r\n <h2 class=\"kratos-entry-title-new\"><a href=\"${pageInfo.link}\">${pageInfo.title}</a></h2>\r\n </header>\r\n <div class=\"kratos-entry-content-new\">\r\n <p>...${pageInfo.content}...</p>\r\n </div>\r\n </div>\r\n <div class=\"kratos-post-meta-new\">\r\n <span class=\"pull-left\">\r\n <a><i class=\"fa fa-calendar\"></i></a><a>${pageInfo.date}</a>\r\n <a><i class=\"fa fa-tags\"></i></a>\r\n ${pageTags}\r\n </span>\r\n </div>\r\n </div>\r\n </article>\r\n `;\r\n\r\n resultString += postTemplate;\r\n });\r\n resultSectionElement.innerHTML = resultString;\r\n}\r\n\r\nfunction loadDataSearch(searchDataFile, skeys) {\r\n fetch(searchDataFile)\r\n .then((res)=>{\r\n setNotice('success', '文件加载完成,开始搜索啦~');\r\n if (typeof NProgress !== 'undefined') {\r\n NProgress.inc();\r\n }\r\n return res.json();\r\n })\r\n .then((datas)=>{\r\n const startTime = performance.now();\r\n let resultArray = [];\r\n let resultCount = 0;\r\n let keywords = skeys.trim().toLowerCase().split(/\\s/);\r\n\r\n datas.forEach((data)=>{\r\n if (typeof data.title === 'undefined' || typeof data.content === 'undefined') {\r\n return;\r\n }\r\n\r\n // 开始匹配\r\n let matched = false;\r\n\r\n const dataTitle = data.title.trim().toLowerCase();\r\n const dataContent = data.content ? data.content.trim().replace(/<[^>]+>/g, '').toLowerCase() : '';\r\n let dataWeight = 0;\r\n\r\n let indexs = {};\r\n indexs.title = -1;\r\n indexs.content = -1;\r\n indexs.firstOccur = -1;\r\n indexs.lastOccur = -1;\r\n const halfLenth = 100;\r\n\r\n if (dataTitle) {\r\n keywords.forEach((keyword)=>{\r\n indexs.title = dataTitle.indexOf(keyword);\r\n indexs.content = dataContent.indexOf(keyword);\r\n if (indexs.title !== -1 || indexs.content !== -1) {\r\n matched = true;\r\n if (indexs.content !== -1) {\r\n if (indexs.firstOccur > indexs.content || indexs.firstOccur === -1) {\r\n indexs.firstOccur = indexs.content;\r\n }\r\n if (indexs.lastOccur < indexs.content) {\r\n indexs.lastOccur = indexs.content;\r\n }\r\n } else {\r\n //命中 title,但正文 conten 未命中时\r\n indexs.firstOccur = halfLenth;\r\n indexs.lastOccur = 0;\r\n }\r\n dataWeight += indexs.title !== -1 ? 2 : 0;\r\n dataWeight += indexs.content !== -1 ? 1 : 0;\r\n resultCount++;\r\n }\r\n });\r\n }\r\n\r\n // 设置高亮\r\n if (matched) {\r\n let tPage = {};\r\n tPage.title = data.title;\r\n tPage.date = new Date(data.date).toLocaleDateString();\r\n tPage.tags = data.tags || [];\r\n tPage.category = data.categories[0] || [];\r\n tPage.link = data.url;\r\n keywords.forEach((keyword)=>{\r\n const regS = new RegExp(regEscape(keyword) + '(?!>)', 'gi');\r\n tPage.title = tPage.title.replace(regS, '<m>$&</m>');\r\n });\r\n if (indexs.firstOccur >= 0) {\r\n //const halfLenth = 100;\r\n let start = indexs.firstOccur - halfLenth;\r\n let end = indexs.lastOccur + halfLenth;\r\n if (start < 0) {\r\n start = 0;\r\n }\r\n if (start === 0) {\r\n end = halfLenth * 2;\r\n }\r\n if (end > dataContent.length) {\r\n end = dataContent.length;\r\n }\r\n tPage.content = dataContent.substr(start, end-start);\r\n keywords.forEach((keyword)=>{\r\n const regS = new RegExp(regEscape(keyword) + '(?!>)', 'gi');\r\n tPage.content = tPage.content.replace(regS, '<m>$&</m>');\r\n });\r\n }\r\n resultArray.push([tPage, dataWeight]);\r\n }\r\n\r\n });\r\n if (resultCount !== 0) {\r\n const finishTime = performance.now();\r\n setNotice('success', '找到 ' + resultCount + ' 条搜索结果,用时 ' + Math.round((finishTime - startTime)*100)/100 + ' 毫秒~');\r\n resultArray.sort((a, b)=>{\r\n return b[1] - a[1];\r\n });\r\n createPosts(resultArray);\r\n } else {\r\n setNotice('danger', '什么都没有找到欸...');\r\n clearPosts();\r\n }\r\n if (typeof NProgress !== 'undefined') {\r\n NProgress.done();\r\n }\r\n })\r\n .catch((error)=>{\r\n setNotice('danger', '错误 : ' + error);\r\n });\r\n}\r\n\r\nfunction keySearch(skeys) {\r\n // 设置搜索提示\r\n setNotice('info', '正在加载搜索文件...');\r\n\r\n // 启动进度条\r\n if (typeof NProgress !== 'undefined') {\r\n NProgress.start();\r\n }\r\n\r\n // 加载数据并搜索\r\n loadDataSearch(searchDataFile, searchEscape(skeys));\r\n}\r\n\r\nfunction inpSearch() {\r\n // 单击按钮检索\r\n const skeys = document.getElementById('search-input').value;\r\n // 更新URL\r\n window.history.pushState({},0,window.location.href.split('?')[0]+'?s=' + skeys.replace(/\\s/g, '+'));\r\n // 开始搜索\r\n keySearch(skeys);\r\n return false;\r\n}\r\n\r\n(()=>{\r\n const skeys = getParam('s');\r\n if (skeys !== '') {\r\n // 存在关键词,把搜索词放到输入框里面\r\n document.getElementById('search-input').value = skeys;\r\n // 开始搜索\r\n keySearch(skeys);\r\n }\r\n})();"]} |