mirror of
https://github.com/KivotosRailway/website.git
synced 2026-11-03 19:07:56 +09:00
Add solitude
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
const moment = require('moment')
|
||||
|
||||
hexo.extend.helper.register('getArchiveLength', function (type) {
|
||||
if (type !== 'year' && type !== 'month') {
|
||||
type = 'year';
|
||||
}
|
||||
const posts = this.site.posts.sort('-date').data
|
||||
let archive = {}
|
||||
for (const post of posts) {
|
||||
const postdate = type === 'year' ? moment(post.date).format('YYYY') : moment(post.date).format('YYYY/MM');
|
||||
archive[postdate] = (archive[postdate] || 0) + 1;
|
||||
}
|
||||
return archive;
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Solitude
|
||||
* inject is to head
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
hexo.extend.helper.register('inject_head_js', function () {
|
||||
const createJS = () => `
|
||||
const saveToLocal = {
|
||||
set: function setWithExpiry(key, value, ttl) {
|
||||
if (ttl === 0)
|
||||
return
|
||||
const now = new Date()
|
||||
const expiryDay = ttl * 86400000
|
||||
const item = {
|
||||
value: value,
|
||||
expiry: now.getTime() + expiryDay
|
||||
}
|
||||
localStorage.setItem(key, JSON.stringify(item))
|
||||
},
|
||||
get: function getWithExpiry(key) {
|
||||
const itemStr = localStorage.getItem(key)
|
||||
|
||||
if (!itemStr) {
|
||||
return undefined
|
||||
}
|
||||
const item = JSON.parse(itemStr)
|
||||
const now = new Date()
|
||||
|
||||
if (now.getTime() > item.expiry) {
|
||||
localStorage.removeItem(key)
|
||||
return undefined
|
||||
}
|
||||
return item.value
|
||||
}
|
||||
};
|
||||
window.utils = {
|
||||
saveToLocal: saveToLocal,
|
||||
getCSS: (url, id = false) => new Promise((resolve, reject) => {
|
||||
const link = document.createElement('link')
|
||||
link.rel = 'stylesheet'
|
||||
link.href = url
|
||||
if (id) link.id = id
|
||||
link.onerror = reject
|
||||
link.onload = link.onreadystatechange = function() {
|
||||
const loadState = this.readyState
|
||||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||||
link.onload = link.onreadystatechange = null
|
||||
resolve()
|
||||
}
|
||||
document.head.appendChild(link)
|
||||
}),
|
||||
getScript: (url, attr = {}) => new Promise((resolve, reject) => {
|
||||
const script = document.createElement('script')
|
||||
script.src = url
|
||||
script.async = true
|
||||
script.onerror = reject
|
||||
script.onload = script.onreadystatechange = function() {
|
||||
const loadState = this.readyState
|
||||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||||
script.onload = script.onreadystatechange = null
|
||||
resolve()
|
||||
}
|
||||
|
||||
Object.keys(attr).forEach(key => {
|
||||
script.setAttribute(key, attr[key])
|
||||
})
|
||||
|
||||
document.head.appendChild(script)
|
||||
}),
|
||||
addGlobalFn: (key, fn, name = false, parent = window) => {
|
||||
const globalFn = parent.globalFn || {}
|
||||
const keyObj = globalFn[key] || {}
|
||||
|
||||
if (name && keyObj[name]) return
|
||||
|
||||
name = name || Object.keys(keyObj).length
|
||||
keyObj[name] = fn
|
||||
globalFn[key] = keyObj
|
||||
parent.globalFn = globalFn
|
||||
},
|
||||
}
|
||||
`
|
||||
return `<script>(()=>{${createJS()}})()</script>`
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
"use strict";
|
||||
const { stripHTML, escapeHTML, prettyUrls } = require("hexo-util");
|
||||
hexo.extend.helper.register('page_description', function () {
|
||||
const { config, page } = this
|
||||
let description = page.description || page.content || page.title || config.description
|
||||
|
||||
if (description) {
|
||||
description = escapeHTML(stripHTML(description).substring(0, 150)
|
||||
.trim()
|
||||
).replace(/\n/g, ' ').replace(/\s+/g, ' ');
|
||||
return description
|
||||
}
|
||||
})
|
||||
|
||||
hexo.extend.helper.register("urlNoIndex", function (url = null) {
|
||||
return prettyUrls(url || this.url, { trailing_index: false, trailing_html: true });
|
||||
});
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* related post
|
||||
* from solitude
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
hexo.extend.helper.register('related_posts', function (currentPost, allPosts) {
|
||||
const relatedPosts = []
|
||||
const config = hexo.theme.config
|
||||
const limitNum = config.related_post.limit || 6
|
||||
const dateType = config.related_post.date_type || 'created'
|
||||
const headlineLang = this._p('star')
|
||||
|
||||
currentPost.tags.forEach(function (tag) {
|
||||
allPosts.forEach(function (post) {
|
||||
if (isTagRelated(tag.name, post.tags) && currentPost.path !== post.path) {
|
||||
const relatedPost = {
|
||||
title: post.title,
|
||||
path: post.path,
|
||||
cover: post.cover,
|
||||
cover_type: post.cover_type,
|
||||
weight: 1,
|
||||
updated: post.updated,
|
||||
created: post.date
|
||||
}
|
||||
const index = findItem(relatedPosts, 'path', post.path)
|
||||
if (index !== -1) {
|
||||
relatedPosts[index].weight += 1
|
||||
} else {
|
||||
relatedPosts.push(relatedPost)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
if (relatedPosts.length === 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
relatedPosts.sort(compare('weight', dateType))
|
||||
|
||||
let result = '<div class="relatedPosts">'
|
||||
result += `<div class="headline"><i class="solitude st-star-smile-fill"></i><span>${headlineLang}</span><div class="relatedPosts-link"><a onclick="event.preventDefault(); toRandomPost();" href="javascript:void(0);" rel="external nofollow" data-pjax-state="">${this._p('random')}</a></div></div>`
|
||||
result += '<div class="relatedPosts-list">'
|
||||
|
||||
for (let i = 0; i < Math.min(relatedPosts.length, limitNum); i++) {
|
||||
const cover = relatedPosts[i].cover || 'var(--default-bg-color)'
|
||||
const title = this.escape_html(relatedPosts[i].title)
|
||||
result += `<div><a href="${this.url_for(relatedPosts[i].path)}" title="${title}">`
|
||||
result += `<img class="cover" src="${this.url_for(cover)}" alt="cover">`
|
||||
result += `<div class="content is-center"><div class="title">${title}</div></div>`
|
||||
result += '</a></div>'
|
||||
}
|
||||
|
||||
result += '</div></div>'
|
||||
return result
|
||||
})
|
||||
|
||||
function isTagRelated(tagName, TBDtags) {
|
||||
return TBDtags.some(function (tag) {
|
||||
return tagName === tag.name
|
||||
})
|
||||
}
|
||||
|
||||
function findItem(arrayToSearch, attr, val) {
|
||||
return arrayToSearch.findIndex(function (item) {
|
||||
return item[attr] === val
|
||||
})
|
||||
}
|
||||
|
||||
function compare(attr, dateType) {
|
||||
return function (a, b) {
|
||||
const val1 = a[attr]
|
||||
const val2 = b[attr]
|
||||
if (val1 === val2) {
|
||||
if (dateType === 'created') {
|
||||
return b.created - a.created
|
||||
} else if (dateType === 'updated') {
|
||||
return b.updated - a.updated
|
||||
}
|
||||
}
|
||||
return val2 - val1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
hexo.extend.filter.register('stylus:renderer', function (style) {
|
||||
const {config, theme} = this;
|
||||
const data = hexo.locals.get('data');
|
||||
|
||||
style.define('$about', data && data.about ? Object.keys(data.about) : false);
|
||||
style.define('$link', !!(data && data.links) || !!(data && data.tlink))
|
||||
style.define('$equipment', !!(data && data.equipment))
|
||||
|
||||
// highlight
|
||||
const {syntax_highlighter: syntaxHighlighter, highlight, prismjs} = hexo.config
|
||||
let {enable: highlightEnable, line_number: highlightLineNumber} = highlight
|
||||
let {enable: prismjsEnable, line_number: prismjsLineNumber} = prismjs
|
||||
|
||||
// for hexo > 7.0
|
||||
if (syntaxHighlighter) {
|
||||
highlightEnable = syntaxHighlighter === 'highlight.js'
|
||||
prismjsEnable = syntaxHighlighter === 'prismjs'
|
||||
}
|
||||
|
||||
style.define('$highlight_enable', highlightEnable)
|
||||
style.define('$highlight_line_number', highlightLineNumber)
|
||||
style.define('$prismjs_enable', prismjsEnable)
|
||||
style.define('$prismjs_line_number', prismjsLineNumber)
|
||||
});
|
||||
Reference in New Issue
Block a user