${this.date(relatedPosts[i].created, hexoConfig.date_format)}
`
} else {
- result +=
- '
' +
- ' ' +
- this.date(relatedPosts[i].updated, hexoConfig.date_format) +
- '
'
+ result += `
${this.date(relatedPosts[i].updated, hexoConfig.date_format)}
`
}
- result +=
- '
' + relatedPosts[i].title + '
'
+ result += `
${relatedPosts[i].title}
`
result += '
'
}
diff --git a/themes/butterfly/scripts/tag/button.js b/themes/butterfly/scripts/tag/button.js
index 791a8e3..33a0e53 100644
--- a/themes/butterfly/scripts/tag/button.js
+++ b/themes/butterfly/scripts/tag/button.js
@@ -21,7 +21,7 @@ function btn (args) {
icon = icon.trim()
option = option.trim()
- return `
${icon.length ? `` : ''}${text.length ? `${text}` : ''}`
}
diff --git a/themes/butterfly/scripts/tag/gallery.js b/themes/butterfly/scripts/tag/gallery.js
index 6bec4c4..acccd0b 100644
--- a/themes/butterfly/scripts/tag/gallery.js
+++ b/themes/butterfly/scripts/tag/gallery.js
@@ -8,7 +8,7 @@
const urlFor = require('hexo-util').url_for.bind(hexo)
function gallery (args, content) {
- return `
${hexo.render.renderSync({ text: content, engine: 'markdown' }).split('\n').join('')}
+ return `
${hexo.render.renderSync({ text: content, engine: 'markdown' }).split('\n').join('')}
`
}
@@ -20,7 +20,7 @@ function galleryGroup (args) {
return `
-
+
${name}
${desrc}
diff --git a/themes/butterfly/scripts/tag/hide.js b/themes/butterfly/scripts/tag/hide.js
index ae463c7..996ad2b 100644
--- a/themes/butterfly/scripts/tag/hide.js
+++ b/themes/butterfly/scripts/tag/hide.js
@@ -28,7 +28,7 @@ function hideInline (args) {
if (color) group += `color: ${color}`
group += '"'
- return `'
)
}
- },
- cssClasses: {
- item: 'algolia-hit-item'
}
})
- )
+ ])
- search.addWidget(
+ search.addWidgets([
instantsearch.widgets.stats({
container: '#algolia-stats',
templates: {
- body: function (data) {
+ text: function (data) {
+ const icon = '
'
const stats = GLOBAL_CONFIG.algolia.languages.hits_stats
.replace(/\$\{hits}/, data.nbHits)
.replace(/\$\{time}/, data.processingTimeMS)
return (
- '
' +
- stats +
- '
' +
- '
' +
- ''
+ `
${stats}
${icon}`
)
}
}
})
- )
+ ])
- search.addWidget(
+ search.addWidgets([
instantsearch.widgets.pagination({
container: '#algolia-pagination',
- scrollTo: false,
- showFirstLast: false,
- labels: {
+ totalPages: 5,
+ templates: {
first: '
',
last: '
',
previous: '
',
next: '
'
- },
- cssClasses: {
- root: 'pagination',
- item: 'pagination-item',
- link: 'page-number',
- active: 'current',
- disabled: 'disabled-item'
}
})
- )
+ ])
search.start()
window.pjax && search.on('render', () => {
diff --git a/themes/butterfly/source/js/search/local-search.js b/themes/butterfly/source/js/search/local-search.js
index 1abc7cf..de2db62 100644
--- a/themes/butterfly/source/js/search/local-search.js
+++ b/themes/butterfly/source/js/search/local-search.js
@@ -1,10 +1,12 @@
window.addEventListener('load', () => {
let loadFlag = false
- const openSearch = function () {
- document.body.style.cssText = 'width: 100%;overflow: hidden'
- document.querySelector('#local-search .search-dialog').style.display = 'block'
- document.querySelector('#local-search-input input').focus()
- btf.fadeIn(document.getElementById('search-mask'), 0.5)
+ const openSearch = () => {
+ const bodyStyle = document.body.style
+ bodyStyle.width = '100%'
+ bodyStyle.overflow = 'hidden'
+ btf.animateIn(document.getElementById('search-mask'), 'to_show 0.5s')
+ btf.animateIn(document.querySelector('#local-search .search-dialog'), 'titleScale 0.5s')
+ setTimeout(() => { document.querySelector('#local-search-input input').focus() }, 100)
if (!loadFlag) {
search(GLOBAL_CONFIG.localSearch.path)
loadFlag = true
@@ -18,12 +20,12 @@ window.addEventListener('load', () => {
})
}
- const closeSearch = function () {
- document.body.style.cssText = "width: '';overflow: ''"
- const $searchDialog = document.querySelector('#local-search .search-dialog')
- $searchDialog.style.animation = 'search_close .5s'
- setTimeout(() => { $searchDialog.style.cssText = "display: none; animation: ''" }, 500)
- btf.fadeOut(document.getElementById('search-mask'), 0.5)
+ const closeSearch = () => {
+ const bodyStyle = document.body.style
+ bodyStyle.width = ''
+ bodyStyle.overflow = ''
+ btf.animateOut(document.querySelector('#local-search .search-dialog'), 'search_close .5s')
+ btf.animateOut(document.getElementById('search-mask'), 'to_hide 0.5s')
}
// click function
@@ -41,106 +43,122 @@ window.addEventListener('load', () => {
searchClickFn()
})
- function search (path) {
- fetch(GLOBAL_CONFIG.root + path)
- .then(response => response.text())
- .then(str => new window.DOMParser().parseFromString(str, 'text/xml'))
- .then(data => {
- const datas = [...data.querySelectorAll('entry')].map(function (item) {
- return {
- title: item.querySelector('title').textContent,
- content: item.querySelector('content').textContent,
- url: item.querySelector('url').textContent
- }
- })
+ async function search (path) {
+ let datas = []
+ const typeF = path.split('.')[1]
+ const response = await fetch(GLOBAL_CONFIG.root + path)
+ if (typeF === 'json') {
+ datas = await response.json()
+ } else if (typeF === 'xml') {
+ const res = await response.text()
+ const t = await new window.DOMParser().parseFromString(res, 'text/xml')
+ const a = await t
+ datas = [...a.querySelectorAll('entry')].map(function (item) {
+ return {
+ title: item.querySelector('title').textContent,
+ content: item.querySelector('content').textContent,
+ url: item.querySelector('url').textContent
+ }
+ })
+ }
+ if (response.ok) {
+ const $loadDataItem = document.getElementById('loading-database')
+ $loadDataItem.nextElementSibling.style.display = 'block'
+ $loadDataItem.remove()
+ }
- const $input = document.querySelector('#local-search-input input')
- const $resultContent = document.getElementById('local-search-results')
- $input.addEventListener('input', function () {
- let str = '
'
- const keywords = this.value.trim().toLowerCase().split(/[\s]+/)
- $resultContent.innerHTML = ''
- if (this.value.trim().length <= 0) return
- let count = 0
- // perform local searching
- datas.forEach(function (data) {
- let isMatch = true
- if (!data.title || data.title.trim() === '') {
- data.title = 'Untitled'
- }
- let dataTitle = data.title.trim().toLowerCase()
- const dataContent = data.content.trim().replace(/<[^>]+>/g, '').toLowerCase()
- const dataUrl = data.url.startsWith('/') ? data.url : GLOBAL_CONFIG.root + data.url
- let indexTitle = -1
- let indexContent = -1
- let firstOccur = -1
- // only match artiles with not empty titles and contents
- if (dataTitle !== '' || dataContent !== '') {
- keywords.forEach(function (keyword, i) {
- indexTitle = dataTitle.indexOf(keyword)
- indexContent = dataContent.indexOf(keyword)
- if (indexTitle < 0 && indexContent < 0) {
- isMatch = false
- } else {
- if (indexContent < 0) {
- indexContent = 0
- }
- if (i === 0) {
- firstOccur = indexContent
- }
- }
- })
- } else {
+ const $input = document.querySelector('#local-search-input input')
+ const $resultContent = document.getElementById('local-search-results')
+ const $loadingStatus = document.getElementById('loading-status')
+ $input.addEventListener('input', function () {
+ const keywords = this.value.trim().toLowerCase().split(/[\s]+/)
+ if (keywords[0] !== '') $loadingStatus.innerHTML = '
'
+
+ $resultContent.innerHTML = ''
+ let str = '
'
+ if (this.value.trim().length <= 0) return
+ let count = 0
+ // perform local searching
+ datas.forEach(function (data) {
+ let isMatch = true
+ if (!data.title || data.title.trim() === '') {
+ data.title = ''
+ }
+ let dataTitle = data.title.trim().toLowerCase()
+ const dataContent = data.content ? data.content.trim().replace(/<[^>]+>/g, '').toLowerCase() : ''
+ const dataUrl = data.url.startsWith('/') ? data.url : GLOBAL_CONFIG.root + data.url
+ let indexTitle = -1
+ let indexContent = -1
+ let firstOccur = -1
+ // only match artiles with not empty titles and contents
+ if (dataTitle !== '' || dataContent !== '') {
+ keywords.forEach(function (keyword, i) {
+ indexTitle = dataTitle.indexOf(keyword)
+ indexContent = dataContent.indexOf(keyword)
+ if (indexTitle < 0 && indexContent < 0) {
isMatch = false
- }
-
- // show search results
- if (isMatch) {
- const content = data.content.trim().replace(/<[^>]+>/g, '')
- if (firstOccur >= 0) {
- // cut out 130 characters
- let start = firstOccur - 30
- let end = firstOccur + 100
-
- if (start < 0) {
- start = 0
- }
-
- if (start === 0) {
- end = 100
- }
-
- if (end > content.length) {
- end = content.length
- }
-
- let matchContent = content.substring(start, end)
-
- // highlight all keywords
- keywords.forEach(function (keyword) {
- const regS = new RegExp(keyword, 'gi')
- matchContent = matchContent.replace(regS, '
' + keyword + '')
- dataTitle = dataTitle.replace(regS, '
' + keyword + '')
- })
-
- str += '
' + dataTitle + ''
- count += 1
-
- if (dataContent !== '') {
- str += '
' + matchContent + '...
'
- }
+ } else {
+ if (indexContent < 0) {
+ indexContent = 0
+ }
+ if (i === 0) {
+ firstOccur = indexContent
}
- str += '
'
}
})
- if (count === 0) {
- str += '
' + GLOBAL_CONFIG.localSearch.languages.hits_empty.replace(/\$\{query}/, this.value.trim()) +
- '
'
+ } else {
+ isMatch = false
+ }
+
+ // show search results
+ if (isMatch) {
+ const content = data.content.trim().replace(/<[^>]+>/g, '')
+ if (firstOccur >= 0) {
+ // cut out 130 characters
+ // let start = firstOccur - 30 < 0 ? 0 : firstOccur - 30
+ // let end = firstOccur + 50 > content.length ? content.length : firstOccur + 50
+ let start = firstOccur - 30
+ let end = firstOccur + 100
+
+ if (start < 0) {
+ start = 0
+ }
+
+ if (start === 0) {
+ end = 100
+ }
+
+ if (end > content.length) {
+ end = content.length
+ }
+
+ let matchContent = content.substring(start, end)
+
+ // highlight all keywords
+ keywords.forEach(function (keyword) {
+ const regS = new RegExp(keyword, 'gi')
+ matchContent = matchContent.replace(regS, '
' + keyword + '')
+ dataTitle = dataTitle.replace(regS, '
' + keyword + '')
+ })
+
+ str += '
' + dataTitle + ''
+ count += 1
+
+ if (dataContent !== '') {
+ str += '
' + matchContent + '...
'
+ }
}
str += '
'
- $resultContent.innerHTML = str
- window.pjax && window.pjax.refresh($resultContent)
- })
+ }
})
+ if (count === 0) {
+ str += '
' + GLOBAL_CONFIG.localSearch.languages.hits_empty.replace(/\$\{query}/, this.value.trim()) +
+ '
'
+ }
+ str += '
'
+ $resultContent.innerHTML = str
+ if (keywords[0] !== '') $loadingStatus.innerHTML = ''
+ window.pjax && window.pjax.refresh($resultContent)
+ })
}
})
diff --git a/themes/butterfly/source/js/utils.js b/themes/butterfly/source/js/utils.js
index 8fa5d8a..b3e5df0 100644
--- a/themes/butterfly/source/js/utils.js
+++ b/themes/butterfly/source/js/utils.js
@@ -58,34 +58,18 @@ const btf = {
}
},
- snackbarShow: (text, showAction, duration) => {
- const sa = (typeof showAction !== 'undefined') ? showAction : false
- const dur = (typeof duration !== 'undefined') ? duration : 2000
- const position = GLOBAL_CONFIG.Snackbar.position
- const bg = document.documentElement.getAttribute('data-theme') === 'light' ? GLOBAL_CONFIG.Snackbar.bgLight : GLOBAL_CONFIG.Snackbar.bgDark
+ snackbarShow: (text, showAction = false, duration = 2000) => {
+ const { position, bgLight, bgDark } = GLOBAL_CONFIG.Snackbar
+ const bg = document.documentElement.getAttribute('data-theme') === 'light' ? bgLight : bgDark
Snackbar.show({
text: text,
backgroundColor: bg,
- showAction: sa,
- duration: dur,
+ showAction: showAction,
+ duration: duration,
pos: position
})
},
- initJustifiedGallery: function (selector) {
- if (!(selector instanceof jQuery)) {
- selector = $(selector)
- }
- selector.each(function (i, o) {
- if ($(this).is(':visible')) {
- $(this).justifiedGallery({
- rowHeight: 220,
- margins: 4
- })
- }
- })
- },
-
diffDate: (d, more = false) => {
const dateNow = new Date()
const datePost = new Date(d)
@@ -135,15 +119,11 @@ const btf = {
}
},
- scrollToDest: (pos, time) => {
- if (pos < 0 || time < 0) {
- return
- }
-
- const currentPos = window.scrollY || window.screenTop
+ scrollToDest: (pos, time = 500) => {
+ const currentPos = window.pageYOffset
if (currentPos > pos) pos = pos - 70
- if ('CSS' in window && CSS.supports('scroll-behavior', 'smooth')) {
+ if ('scrollBehavior' in document.documentElement.style) {
window.scrollTo({
top: pos,
behavior: 'smooth'
@@ -152,39 +132,35 @@ const btf = {
}
let start = null
- time = time || 500
+ pos = +pos
window.requestAnimationFrame(function step (currentTime) {
start = !start ? currentTime : start
+ const progress = currentTime - start
if (currentPos < pos) {
- const progress = currentTime - start
window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos)
- if (progress < time) {
- window.requestAnimationFrame(step)
- } else {
- window.scrollTo(0, pos)
- }
} else {
- const progress = currentTime - start
window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time))
- if (progress < time) {
- window.requestAnimationFrame(step)
- } else {
- window.scrollTo(0, pos)
- }
+ }
+ if (progress < time) {
+ window.requestAnimationFrame(step)
+ } else {
+ window.scrollTo(0, pos)
}
})
},
- fadeIn: (ele, time) => {
- ele.style.cssText = `display:block;animation: to_show ${time}s`
+ animateIn: (ele, text) => {
+ ele.style.display = 'block'
+ ele.style.animation = text
},
- fadeOut: (ele, time) => {
+ animateOut: (ele, text) => {
ele.addEventListener('animationend', function f () {
- ele.style.cssText = "display: none; animation: '' "
+ ele.style.display = ''
+ ele.style.animation = ''
ele.removeEventListener('animationend', f)
})
- ele.style.animation = `to_hide ${time}s`
+ ele.style.animation = text
},
getParents: (elem, selector) => {
@@ -204,7 +180,6 @@ const btf = {
},
/**
- *
* @param {*} selector
* @param {*} eleType the type of create element
* @param {*} options object key: value
@@ -226,14 +201,6 @@ const btf = {
}
},
- isJqueryLoad: fn => {
- if (typeof jQuery === 'undefined') {
- getScript(GLOBAL_CONFIG.source.jQuery).then(fn)
- } else {
- fn()
- }
- },
-
isHidden: ele => ele.offsetHeight === 0 && ele.offsetWidth === 0,
getEleTop: ele => {
@@ -246,6 +213,65 @@ const btf = {
}
return actualTop
- }
+ },
+ loadLightbox: ele => {
+ const service = GLOBAL_CONFIG.lightbox
+
+ if (service === 'mediumZoom') {
+ const zoom = mediumZoom(ele)
+ zoom.on('open', e => {
+ const photoBg = document.documentElement.getAttribute('data-theme') === 'dark' ? '#121212' : '#fff'
+ zoom.update({
+ background: photoBg
+ })
+ })
+ }
+
+ if (service === 'fancybox') {
+ ele.forEach(i => {
+ if (i.parentNode.tagName !== 'A') {
+ const dataSrc = i.dataset.lazySrc || i.src
+ const dataCaption = i.title || i.alt || ''
+ btf.wrap(i, 'a', { href: dataSrc, 'data-fancybox': 'gallery', 'data-caption': dataCaption, 'data-thumb': dataSrc })
+ }
+ })
+
+ if (!window.fancyboxRun) {
+ Fancybox.bind('[data-fancybox]', {
+ Hash: false,
+ Thumbs: {
+ autoStart: false
+ }
+ })
+ window.fancyboxRun = true
+ }
+ }
+ },
+
+ initJustifiedGallery: function (selector) {
+ selector.forEach(function (i) {
+ if (!btf.isHidden(i)) {
+ fjGallery(i, {
+ itemSelector: '.fj-gallery-item',
+ rowHeight: 220,
+ gutter: 4,
+ onJustify: function () {
+ this.$container.style.opacity = '1'
+ }
+ })
+ }
+ })
+ },
+
+ updateAnchor: (anchor) => {
+ if (anchor !== window.location.hash) {
+ if (!anchor) anchor = location.pathname
+ const title = GLOBAL_CONFIG_SITE.title
+ window.history.replaceState({
+ url: location.href,
+ title: title
+ }, title, anchor)
+ }
+ }
}