mirror of
https://github.com/jerryc127/hexo-theme-butterfly.git
synced 2026-04-16 20:30:53 +08:00
1. 新增 macstyle 設定,取消 mac / mac light 主題設定 2. 整合搜索相關設定 3. 修改程式碼區塊設定 4. 主頁文章新增多種版面配置 5. 新增說說頁面 6. 適配 hexo-blog-encrypt 加密插件 7. 改善手機端目錄的開啟效果 8. 新增平滑滾動功能 9. 支持以程式碼區塊方式撰寫 mermaid 圖表 10. 可自訂文章標題位置 11. 新增程式碼全螢幕按鈕 12. 友情連結頭像改為圓角設計 13. 優化程式碼,使用 hexo-util 的參數和 hexo 內建參數 14. 可自訂搜索框提示文字 15. 未設定選單時,不顯示側邊欄目錄和按鈕 16. 螢幕寬度超過 2000px 時,增加卡片高度 17. 根據語言設定調整字體:簡體中文使用雅黑,其他使用正黑體 18. 更新 plugins.yml 19. 全新的側邊欄界面設計 20. 新增 giscus 的 js 設定 21. 調整 utterances js 的設定位置 22. 新增 utterances option 設定 23. 修改 giscus 的主題設定 24. 多個界面元素改為圓角設計 25. 可選擇圓角或直角界面風格 26. 圖庫加載按鈕新增圖標 27. 改善標籤頁面的滑鼠懸停效果 28. 調整側邊欄的滑鼠懸停效果 29. 微調部分界面元素 1. 修復 Hexo 新版本下 Prism.js 無法正確高亮的問題 2. 修復文章標籤為空時可能出現的錯誤 3. 修正 mermaid 圖表可能出現的錯誤 4. 解決未設定選單時控制台報錯的問題 5. 修復 Algolia 搜索的每頁顯示數量設定無效的問題 6. 解決 Algolia 搜索結果出現滾動條的問題 7. 修正滾動條出現上下按鈕的問題 8. 修復圖庫遠程連結未加前綴的問題 9. 修正 label 標籤外掛右側多餘空格的問題 10. 解決 APlayer 報告內存洩漏的問題 1. 優化 PJAX 下的函數調用 2. 整體代碼優化 3. 提升兼容性 4. 改善 Lighthouse 評分 5. 在 PJAX 關閉時減少不必要的全局變量 6. 優化 Waline 的 import 兼容性 7. 改善頁面進入效果 8. 優化程式碼區塊工具列顯示邏輯 9. 改善不同螢幕寬度下文章標題位置的顯示 10. 優化標籤顏色生成算法,避免過暗或過亮 11. 調整 Artalk 和 Waline 在夜間模式下的字體顏色,與主題保持一致 12. 調整 Algolia 搜索加載動畫位置,避免換行 13. 優化 Algolia 搜索結果為空時的處理 14. 改善系列文章的滑鼠懸停效果 15. 優化 404 頁面代碼 16. 解決搜索和側邊欄開啟時窗口抖動的問題 17. 優化 tabs 標籤外掛的代碼和效能 18. 改善 tabs 中使用 gallery 標籤外掛時的圖片加載邏輯 19. 優化目錄滾動效果,使當前標題保持在中間 20. 調整螢幕寬度超過 1024px 時 gallerygroup 的顯示數量
361 lines
12 KiB
JavaScript
361 lines
12 KiB
JavaScript
/**
|
|
* Refer to hexo-generator-searchdb
|
|
* https://github.com/next-theme/hexo-generator-searchdb/blob/main/dist/search.js
|
|
* Modified by hexo-theme-butterfly
|
|
*/
|
|
|
|
class LocalSearch {
|
|
constructor ({
|
|
path = '',
|
|
unescape = false,
|
|
top_n_per_article = 1
|
|
}) {
|
|
this.path = path
|
|
this.unescape = unescape
|
|
this.top_n_per_article = top_n_per_article
|
|
this.isfetched = false
|
|
this.datas = null
|
|
}
|
|
|
|
getIndexByWord (words, text, caseSensitive = false) {
|
|
const index = []
|
|
const included = new Set()
|
|
|
|
if (!caseSensitive) {
|
|
text = text.toLowerCase()
|
|
}
|
|
words.forEach(word => {
|
|
if (this.unescape) {
|
|
const div = document.createElement('div')
|
|
div.innerText = word
|
|
word = div.innerHTML
|
|
}
|
|
const wordLen = word.length
|
|
if (wordLen === 0) return
|
|
let startPosition = 0
|
|
let position = -1
|
|
if (!caseSensitive) {
|
|
word = word.toLowerCase()
|
|
}
|
|
while ((position = text.indexOf(word, startPosition)) > -1) {
|
|
index.push({ position, word })
|
|
included.add(word)
|
|
startPosition = position + wordLen
|
|
}
|
|
})
|
|
// Sort index by position of keyword
|
|
index.sort((left, right) => {
|
|
if (left.position !== right.position) {
|
|
return left.position - right.position
|
|
}
|
|
return right.word.length - left.word.length
|
|
})
|
|
return [index, included]
|
|
}
|
|
|
|
// Merge hits into slices
|
|
mergeIntoSlice (start, end, index) {
|
|
let item = index[0]
|
|
let { position, word } = item
|
|
const hits = []
|
|
const count = new Set()
|
|
while (position + word.length <= end && index.length !== 0) {
|
|
count.add(word)
|
|
hits.push({
|
|
position,
|
|
length: word.length
|
|
})
|
|
const wordEnd = position + word.length
|
|
|
|
// Move to next position of hit
|
|
index.shift()
|
|
while (index.length !== 0) {
|
|
item = index[0]
|
|
position = item.position
|
|
word = item.word
|
|
if (wordEnd > position) {
|
|
index.shift()
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
hits,
|
|
start,
|
|
end,
|
|
count: count.size
|
|
}
|
|
}
|
|
|
|
// Highlight title and content
|
|
highlightKeyword (val, slice) {
|
|
let result = ''
|
|
let index = slice.start
|
|
for (const { position, length } of slice.hits) {
|
|
result += val.substring(index, position)
|
|
index = position + length
|
|
result += `<mark class="search-keyword">${val.substr(position, length)}</mark>`
|
|
}
|
|
result += val.substring(index, slice.end)
|
|
return result
|
|
}
|
|
|
|
getResultItems (keywords) {
|
|
const resultItems = []
|
|
this.datas.forEach(({ title, content, url }) => {
|
|
// The number of different keywords included in the article.
|
|
const [indexOfTitle, keysOfTitle] = this.getIndexByWord(keywords, title)
|
|
const [indexOfContent, keysOfContent] = this.getIndexByWord(keywords, content)
|
|
const includedCount = new Set([...keysOfTitle, ...keysOfContent]).size
|
|
|
|
// Show search results
|
|
const hitCount = indexOfTitle.length + indexOfContent.length
|
|
if (hitCount === 0) return
|
|
|
|
const slicesOfTitle = []
|
|
if (indexOfTitle.length !== 0) {
|
|
slicesOfTitle.push(this.mergeIntoSlice(0, title.length, indexOfTitle))
|
|
}
|
|
|
|
let slicesOfContent = []
|
|
while (indexOfContent.length !== 0) {
|
|
const item = indexOfContent[0]
|
|
const { position } = item
|
|
// Cut out 120 characters. The maxlength of .search-input is 80.
|
|
const start = Math.max(0, position - 20)
|
|
const end = Math.min(content.length, position + 100)
|
|
slicesOfContent.push(this.mergeIntoSlice(start, end, indexOfContent))
|
|
}
|
|
|
|
// Sort slices in content by included keywords' count and hits' count
|
|
slicesOfContent.sort((left, right) => {
|
|
if (left.count !== right.count) {
|
|
return right.count - left.count
|
|
} else if (left.hits.length !== right.hits.length) {
|
|
return right.hits.length - left.hits.length
|
|
}
|
|
return left.start - right.start
|
|
})
|
|
|
|
// Select top N slices in content
|
|
const upperBound = parseInt(this.top_n_per_article, 10)
|
|
if (upperBound >= 0) {
|
|
slicesOfContent = slicesOfContent.slice(0, upperBound)
|
|
}
|
|
|
|
let resultItem = ''
|
|
|
|
url = new URL(url, location.origin)
|
|
url.searchParams.append('highlight', keywords.join(' '))
|
|
|
|
if (slicesOfTitle.length !== 0) {
|
|
resultItem += `<div class="local-search-hit-item"><a href="${url.href}"><span class="search-result-title">${this.highlightKeyword(title, slicesOfTitle[0])}</span>`
|
|
} else {
|
|
resultItem += `<div class="local-search-hit-item"><a href="${url.href}"><span class="search-result-title">${title}</span>`
|
|
}
|
|
|
|
slicesOfContent.forEach(slice => {
|
|
resultItem += `<p class="search-result">${this.highlightKeyword(content, slice)}...</p></a>`
|
|
})
|
|
|
|
resultItem += '</div>'
|
|
resultItems.push({
|
|
item: resultItem,
|
|
id: resultItems.length,
|
|
hitCount,
|
|
includedCount
|
|
})
|
|
})
|
|
return resultItems
|
|
}
|
|
|
|
fetchData () {
|
|
const isXml = !this.path.endsWith('json')
|
|
fetch(this.path)
|
|
.then(response => response.text())
|
|
.then(res => {
|
|
// Get the contents from search data
|
|
this.isfetched = true
|
|
this.datas = isXml
|
|
? [...new DOMParser().parseFromString(res, 'text/xml').querySelectorAll('entry')].map(element => ({
|
|
title: element.querySelector('title').textContent,
|
|
content: element.querySelector('content').textContent,
|
|
url: element.querySelector('url').textContent
|
|
}))
|
|
: JSON.parse(res)
|
|
// Only match articles with non-empty titles
|
|
this.datas = this.datas.filter(data => data.title).map(data => {
|
|
data.title = data.title.trim()
|
|
data.content = data.content ? data.content.trim().replace(/<[^>]+>/g, '') : ''
|
|
data.url = decodeURIComponent(data.url).replace(/\/{2,}/g, '/')
|
|
return data
|
|
})
|
|
// Remove loading animation
|
|
window.dispatchEvent(new Event('search:loaded'))
|
|
})
|
|
}
|
|
|
|
// Highlight by wrapping node in mark elements with the given class name
|
|
highlightText (node, slice, className) {
|
|
const val = node.nodeValue
|
|
let index = slice.start
|
|
const children = []
|
|
for (const { position, length } of slice.hits) {
|
|
const text = document.createTextNode(val.substring(index, position))
|
|
index = position + length
|
|
const mark = document.createElement('mark')
|
|
mark.className = className
|
|
mark.appendChild(document.createTextNode(val.substr(position, length)))
|
|
children.push(text, mark)
|
|
}
|
|
node.nodeValue = val.substring(index, slice.end)
|
|
children.forEach(element => {
|
|
node.parentNode.insertBefore(element, node)
|
|
})
|
|
}
|
|
|
|
// Highlight the search words provided in the url in the text
|
|
highlightSearchWords (body) {
|
|
const params = new URL(location.href).searchParams.get('highlight')
|
|
const keywords = params ? params.split(' ') : []
|
|
if (!keywords.length || !body) return
|
|
const walk = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null)
|
|
const allNodes = []
|
|
while (walk.nextNode()) {
|
|
if (!walk.currentNode.parentNode.matches('button, select, textarea, .mermaid')) allNodes.push(walk.currentNode)
|
|
}
|
|
allNodes.forEach(node => {
|
|
const [indexOfNode] = this.getIndexByWord(keywords, node.nodeValue)
|
|
if (!indexOfNode.length) return
|
|
const slice = this.mergeIntoSlice(0, node.nodeValue.length, indexOfNode)
|
|
this.highlightText(node, slice, 'search-keyword')
|
|
})
|
|
}
|
|
}
|
|
|
|
window.addEventListener('load', () => {
|
|
// Search
|
|
const { path, top_n_per_article, unescape, languages } = GLOBAL_CONFIG.localSearch
|
|
const localSearch = new LocalSearch({
|
|
path,
|
|
top_n_per_article,
|
|
unescape
|
|
})
|
|
|
|
const input = document.querySelector('#local-search-input input')
|
|
const statsItem = document.getElementById('local-search-stats-wrap')
|
|
const $loadingStatus = document.getElementById('loading-status')
|
|
const isXml = !path.endsWith('json')
|
|
|
|
const inputEventFunction = () => {
|
|
if (!localSearch.isfetched) return
|
|
let searchText = input.value.trim().toLowerCase()
|
|
isXml && (searchText = searchText.replace(/</g, '<').replace(/>/g, '>'))
|
|
if (searchText !== '') $loadingStatus.innerHTML = '<i class="fas fa-spinner fa-pulse"></i>'
|
|
const keywords = searchText.split(/[-\s]+/)
|
|
const container = document.getElementById('local-search-results')
|
|
let resultItems = []
|
|
if (searchText.length > 0) {
|
|
// Perform local searching
|
|
resultItems = localSearch.getResultItems(keywords)
|
|
}
|
|
if (keywords.length === 1 && keywords[0] === '') {
|
|
container.textContent = ''
|
|
statsItem.textContent = ''
|
|
} else if (resultItems.length === 0) {
|
|
container.textContent = ''
|
|
const statsDiv = document.createElement('div')
|
|
statsDiv.className = 'search-result-stats'
|
|
statsDiv.textContent = languages.hits_empty.replace(/\$\{query}/, searchText)
|
|
statsItem.innerHTML = statsDiv.outerHTML
|
|
} else {
|
|
resultItems.sort((left, right) => {
|
|
if (left.includedCount !== right.includedCount) {
|
|
return right.includedCount - left.includedCount
|
|
} else if (left.hitCount !== right.hitCount) {
|
|
return right.hitCount - left.hitCount
|
|
}
|
|
return right.id - left.id
|
|
})
|
|
|
|
const stats = languages.hits_stats.replace(/\$\{hits}/, resultItems.length)
|
|
|
|
container.innerHTML = `<div class="search-result-list">${resultItems.map(result => result.item).join('')}</div>`
|
|
statsItem.innerHTML = `<hr><div class="search-result-stats">${stats}</div>`
|
|
window.pjax && window.pjax.refresh(container)
|
|
}
|
|
|
|
$loadingStatus.textContent = ''
|
|
}
|
|
|
|
let loadFlag = false
|
|
const $searchMask = document.getElementById('search-mask')
|
|
const $searchDialog = document.querySelector('#local-search .search-dialog')
|
|
|
|
// fix safari
|
|
const fixSafariHeight = () => {
|
|
if (window.innerWidth < 768) {
|
|
$searchDialog.style.setProperty('--search-height', window.innerHeight + 'px')
|
|
}
|
|
}
|
|
|
|
const openSearch = () => {
|
|
btf.overflowPaddingR.add()
|
|
btf.animateIn($searchMask, 'to_show 0.5s')
|
|
btf.animateIn($searchDialog, 'titleScale 0.5s')
|
|
setTimeout(() => { input.focus() }, 300)
|
|
if (!loadFlag) {
|
|
!localSearch.isfetched && localSearch.fetchData()
|
|
input.addEventListener('input', inputEventFunction)
|
|
loadFlag = true
|
|
}
|
|
// shortcut: ESC
|
|
document.addEventListener('keydown', function f (event) {
|
|
if (event.code === 'Escape') {
|
|
closeSearch()
|
|
document.removeEventListener('keydown', f)
|
|
}
|
|
})
|
|
|
|
fixSafariHeight()
|
|
window.addEventListener('resize', fixSafariHeight)
|
|
}
|
|
|
|
const closeSearch = () => {
|
|
btf.overflowPaddingR.remove()
|
|
btf.animateOut($searchDialog, 'search_close .5s')
|
|
btf.animateOut($searchMask, 'to_hide 0.5s')
|
|
window.removeEventListener('resize', fixSafariHeight)
|
|
}
|
|
|
|
const searchClickFn = () => {
|
|
btf.addEventListenerPjax(document.querySelector('#search-button > .search'), 'click', openSearch)
|
|
}
|
|
|
|
const searchFnOnce = () => {
|
|
document.querySelector('#local-search .search-close-button').addEventListener('click', closeSearch)
|
|
$searchMask.addEventListener('click', closeSearch)
|
|
if (GLOBAL_CONFIG.localSearch.preload) {
|
|
localSearch.fetchData()
|
|
}
|
|
localSearch.highlightSearchWords(document.getElementById('article-container'))
|
|
}
|
|
|
|
window.addEventListener('search:loaded', () => {
|
|
const $loadDataItem = document.getElementById('loading-database')
|
|
$loadDataItem.nextElementSibling.style.display = 'block'
|
|
$loadDataItem.remove()
|
|
})
|
|
|
|
searchClickFn()
|
|
searchFnOnce()
|
|
|
|
// pjax
|
|
window.addEventListener('pjax:complete', () => {
|
|
!btf.isHidden($searchMask) && closeSearch()
|
|
localSearch.highlightSearchWords(document.getElementById('article-container'))
|
|
searchClickFn()
|
|
})
|
|
})
|