From d5eb7cf4f0a392afce55409bb96c1d216272ed70 Mon Sep 17 00:00:00 2001 From: Leng Ganghua Date: Thu, 2 Nov 2017 18:37:38 +0800 Subject: [PATCH 1/3] highlight searched keywords and scroll to the first one, when open a page from searching-result-list --- template/theme/source/js/index.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/template/theme/source/js/index.js b/template/theme/source/js/index.js index 7ba4e07..bbd9b42 100755 --- a/template/theme/source/js/index.js +++ b/template/theme/source/js/index.js @@ -74,7 +74,7 @@ var initSearch = function() { } return searchTpl .replace('{{title}}', title) - .replace('{{link}}', link) + .replace('{{link}}', link + '?search=' + keywords) .replace('{{preview}}', preview) } searchWorker.onmessage = function(event) { @@ -158,3 +158,24 @@ $(function() { // init search initSearch() }) + +// get searched keywords from url +var reg = new RegExp("(^|&)search=([^&]*)(&|$)") +var r = window.location.search.substr(1).match(reg) +var keywords = decodeURI(r[2]).split(',') + +// highlight searched keywords +var content = document.body.innerHTML +if (keywords != null && keywords.toString().length > 1) { + for (var i = 0; i < keywords.length; i++) { + var keyword = keywords[i] + var wrap = '' + keyword + '' // id="searched" is for scrolling below + var reg = new RegExp(keyword, 'ig') + content = content.replace(reg, wrap) + } +} +document.body.innerHTML = content + +// scroll to the first searched keyword +var elmnt = document.getElementById("searched") +elmnt.scrollIntoView() \ No newline at end of file From 51e43ae01aab31a0e890b5ca409a333cf6079764 Mon Sep 17 00:00:00 2001 From: Leng Ganghua Date: Thu, 2 Nov 2017 19:59:47 +0800 Subject: [PATCH 2/3] hightlight keywords - css --- template/theme/source/css/content.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/template/theme/source/css/content.css b/template/theme/source/css/content.css index abb09b3..e007276 100644 --- a/template/theme/source/css/content.css +++ b/template/theme/source/css/content.css @@ -132,5 +132,8 @@ margin-top: -10px; margin-bottom: 30px; } + .searched { + background-color: yellow; + } } } From b28ce682d38c3471c0b8985585187d5e1780c1cb Mon Sep 17 00:00:00 2001 From: Leng Ganghua Date: Fri, 3 Nov 2017 07:00:24 +0800 Subject: [PATCH 3/3] make codes grace --- template/theme/source/js/index.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/template/theme/source/js/index.js b/template/theme/source/js/index.js index bbd9b42..3eaf6d9 100755 --- a/template/theme/source/js/index.js +++ b/template/theme/source/js/index.js @@ -74,7 +74,7 @@ var initSearch = function() { } return searchTpl .replace('{{title}}', title) - .replace('{{link}}', link + '?search=' + keywords) + .replace('{{link}}', link + '?search=' + keywords) // append keywords to url .replace('{{preview}}', preview) } searchWorker.onmessage = function(event) { @@ -159,23 +159,18 @@ $(function() { initSearch() }) -// get searched keywords from url -var reg = new RegExp("(^|&)search=([^&]*)(&|$)") -var r = window.location.search.substr(1).match(reg) -var keywords = decodeURI(r[2]).split(',') - // highlight searched keywords -var content = document.body.innerHTML if (keywords != null && keywords.toString().length > 1) { + var content = document.body.innerHTML for (var i = 0; i < keywords.length; i++) { var keyword = keywords[i] - var wrap = '' + keyword + '' // id="searched" is for scrolling below + var wrap = '' + keyword + '' var reg = new RegExp(keyword, 'ig') content = content.replace(reg, wrap) } -} -document.body.innerHTML = content + document.body.innerHTML = content -// scroll to the first searched keyword -var elmnt = document.getElementById("searched") -elmnt.scrollIntoView() \ No newline at end of file + // scroll to the first searched keyword + var elmnt = document.getElementsByClassName("searched") + elmnt[0].scrollIntoView() +}