From e81a81c9d1a51db72a1823a74776ba232fe3c432 Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 7 Apr 2020 14:24:11 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=94=AF=E6=8C=81=E9=A1=AF=E7=A4=BA=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=85=A7=E5=AE=B9=E5=92=8C=E9=AB=98=E4=BA=AEkeyword?= =?UTF-8?q?=20:bug:=20fix:=20=E4=BF=AE=E5=BE=A9=E6=90=9C=E7=B4=A2=E7=B5=90?= =?UTF-8?q?=E6=9E=9C=E5=9C=A8=E6=89=8B=E6=A9=9F=E7=AB=AF=E7=84=A1=E6=B3=95?= =?UTF-8?q?=E6=BB=BE=E5=8B=95=E7=9A=84bug=20:bug:=20fix:=20=E4=BF=AE?= =?UTF-8?q?=E5=BE=A9aside=20categories=20=E6=9F=A5=E7=9C=8B=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E8=B7=B3=E8=BD=89=E5=88=B0tags=E9=A0=81=E9=9D=A2?= =?UTF-8?q?=E7=9A=84bug=20closes=20#188?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/helpers/aside_categories.js | 4 +- source/css/_search/index.styl | 3 +- source/css/_search/local-search.styl | 13 ++++++- source/css/var.styl | 1 + source/js/search/local-search.js | 58 +++++++++++++++++++++++++--- 5 files changed, 69 insertions(+), 10 deletions(-) diff --git a/scripts/helpers/aside_categories.js b/scripts/helpers/aside_categories.js index 8fa89d3..0bf44f5 100644 --- a/scripts/helpers/aside_categories.js +++ b/scripts/helpers/aside_categories.js @@ -18,7 +18,7 @@ hexo.extend.helper.register('aside_categories', function (categories, options) { const depth = options.depth ? parseInt(options.depth, 10) : 0 const orderby = options.orderby || 'name' const order = options.order || 1 - const tagDir = this.url_for(config.tag_dir) + const categoryDir = this.url_for(config.category_dir) const limit = options.limit === 0 ? categories.length : options.limit const buttonLabel = this._p('aside.more_button') const prepareQuery = parent => { @@ -74,7 +74,7 @@ hexo.extend.helper.register('aside_categories', function (categories, options) { var moreHtml = '' if (categories.length <= limit) return '' moreHtml += '
  • ' - moreHtml += `` + moreHtml += `` moreHtml += buttonLabel moreHtml += '
  • ' return moreHtml diff --git a/source/css/_search/index.styl b/source/css/_search/index.styl index bf3794e..79bb359 100644 --- a/source/css/_search/index.styl +++ b/source/css/_search/index.styl @@ -50,4 +50,5 @@ height: 100% .search-result-list - max-height: 100% !important + max-height: 75vh !important + padding-bottom: 2rem diff --git a/source/css/_search/local-search.styl b/source/css/_search/local-search.styl index e1861f9..534117f 100644 --- a/source/css/_search/local-search.styl +++ b/source/css/_search/local-search.styl @@ -18,7 +18,8 @@ .local-search__hit-item position: relative - padding-left: 1.5rem + padding-left: 1.3rem + line-height: 1.7 &:hover &:before @@ -27,7 +28,7 @@ &:before $w = .3rem position: absolute - top: .4rem + top: .3rem left: 0 width: w = $w height: h = w @@ -41,12 +42,20 @@ a display: block color: $font-black + font-weight: 600 font-size: 14px cursor: pointer &:hover color: $search-color + .search-result + margin: 0 0 .4rem + + .search-keyword + color: $search-keyword-highlight + font-weight: bold + .local-search-stats__hr display: none !important diff --git a/source/css/var.styl b/source/css/var.styl index 9be3e5d..49fb7d4 100644 --- a/source/css/var.styl +++ b/source/css/var.styl @@ -79,6 +79,7 @@ $reward-pop-up-color = #858585 // search $search-bg = #f6f8fa $search-color = $theme-color +$search-keyword-highlight = #F47466 //gallery $gallery-color = #fff // tag-hide diff --git a/source/js/search/local-search.js b/source/js/search/local-search.js index efcec8a..c2bee81 100644 --- a/source/js/search/local-search.js +++ b/source/js/search/local-search.js @@ -1,7 +1,10 @@ $(function () { var loadFlag = false $('a.social-icon.search').on('click', function () { - $('body').css({ width: '100%', overflow: 'hidden' }) + $('body').css({ + width: '100%', + overflow: 'hidden' + }) $('.search-dialog').css('display', 'block') $('#local-search-input input').focus() $('.search-mask').fadeIn() @@ -52,6 +55,7 @@ $(function () { url: $('url', this).text() } }).get() + var $input = $('#local-search-input input')[0] var $resultContent = $('#local-hits')[0] $input.addEventListener('input', function () { @@ -66,13 +70,17 @@ $(function () { // perform local searching datas.forEach(function (data) { var isMatch = true + if (!data.title || data.title.trim() === '') { + data.title = 'Untitled' + } var dataTitle = data.title.trim().toLowerCase() var dataContent = data.content.trim().replace(/<[^>]+>/g, '').toLowerCase() var dataUrl = data.url var indexTitle = -1 var indexContent = -1 + var firstOccur = -1 // only match artiles with not empty titles and contents - if (dataTitle !== '' && dataContent !== '') { + if (dataTitle !== '' || dataContent !== '') { keywords.forEach(function (keyword, i) { indexTitle = dataTitle.indexOf(keyword) indexContent = dataContent.indexOf(keyword) @@ -82,20 +90,60 @@ $(function () { if (indexContent < 0) { indexContent = 0 } + if (i === 0) { + firstOccur = indexContent + } } }) + } else { + isMatch = false } + // show search results if (isMatch) { - str += '
    ' + dataTitle + '' + '
    ' - count += 1 - $('.local-search-stats__hr').show() + var content = data.content.trim().replace(/<[^>]+>/g, '') + if (firstOccur >= 0) { + // cut out 130 characters + var start = firstOccur - 30 + var end = firstOccur + 100 + + if (start < 0) { + start = 0 + } + + if (start === 0) { + end = 100 + } + + if (end > content.length) { + end = content.length + } + + var matchContent = content.substring(start, end) + + // highlight all keywords + keywords.forEach(function (keyword) { + var regS = new RegExp(keyword, 'gi') + matchContent = matchContent.replace(regS, '' + keyword + '') + dataTitle = dataTitle.replace(regS, '' + keyword + '') + }) + + str += '
    ' + dataTitle + '' + count += 1 + $('.local-search-stats__hr').show() + + if (dataContent !== '') { + str += '

    ' + matchContent + '...

    ' + } + } + str += '
    ' } }) if (count === 0) { str += '
    ' + GLOBAL_CONFIG.localSearch.languages.hits_empty.replace(/\$\{query}/, this.value.trim()) + '
    ' } + str += '' $resultContent.innerHTML = str }) }