From 0476b27deeac8ed35485303dbb8154411752c849 Mon Sep 17 00:00:00 2001 From: miku8miku <52441374+miku8miku@users.noreply.github.com> Date: Sun, 23 Jun 2024 02:50:53 +0800 Subject: [PATCH] Delete scripts/helpers/charts.js --- scripts/helpers/charts.js | 447 -------------------------------------- 1 file changed, 447 deletions(-) delete mode 100644 scripts/helpers/charts.js diff --git a/scripts/helpers/charts.js b/scripts/helpers/charts.js deleted file mode 100644 index 3bca8ce..0000000 --- a/scripts/helpers/charts.js +++ /dev/null @@ -1,447 +0,0 @@ -const cheerio = require('cheerio') -const moment = require('moment') - -hexo.extend.filter.register('after_render:html', function (locals) { - const $ = cheerio.load(locals) - const post = $('#posts-chart') - const tag = $('#tags-chart') - const category = $('#categories-chart') - const htmlEncode = false - - if (post.length > 0 || tag.length > 0 || category.length > 0) { - if (post.length > 0 && $('#postsChart').length === 0) { - if (post.attr('data-encode') === 'true') htmlEncode = true - post.after(postsChart(post.attr('data-start'))) - } - if (tag.length > 0 && $('#tagsChart').length === 0) { - if (tag.attr('data-encode') === 'true') htmlEncode = true - tag.after(tagsChart(tag.attr('data-length'))) - } - if (category.length > 0 && $('#categoriesChart').length === 0) { - if (category.attr('data-encode') === 'true') htmlEncode = true - category.after(categoriesChart(category.attr('data-parent'))) - } - - if (htmlEncode) { - return $.root().html().replace(/&#/g, '&#') - } else { - return $.root().html() - } - } else { - return locals - } -}, 15) - -function postsChart (startMonth) { - const startDate = moment(startMonth || '2020-01') - const endDate = moment() - - const monthMap = new Map() - const dayTime = 3600 * 24 * 1000 - for (let time = startDate; time <= endDate; time += dayTime) { - const month = moment(time).format('YYYY-MM') - if (!monthMap.has(month)) { - monthMap.set(month, 0) - } - } - hexo.locals.get('posts').forEach(function (post) { - const month = post.date.format('YYYY-MM') - if (monthMap.has(month)) { - monthMap.set(month, monthMap.get(month) + 1) - } - }) - const monthArr = JSON.stringify([...monthMap.keys()]) - const monthValueArr = JSON.stringify([...monthMap.values()]) - - return ` - ` -} - -function tagsChart (len) { - const tagArr = [] - hexo.locals.get('tags').map(function (tag) { - tagArr.push({ name: tag.name, value: tag.length, path: tag.path }) - }) - tagArr.sort((a, b) => { return b.value - a.value }) - - const dataLength = Math.min(tagArr.length, len) || tagArr.length - const tagNameArr = [] - for (let i = 0; i < dataLength; i++) { - tagNameArr.push(tagArr[i].name) - } - const tagNameArrJson = JSON.stringify(tagNameArr) - const tagArrJson = JSON.stringify(tagArr) - - return ` - ` -} - -function categoriesChart (dataParent) { - const categoryArr = [] - let categoryParentFlag = false - hexo.locals.get('categories').map(function (category) { - if (category.parent) categoryParentFlag = true - categoryArr.push({ - name: category.name, - value: category.length, - path: category.path, - id: category._id, - parentId: category.parent || '0' - }) - }) - categoryParentFlag = categoryParentFlag && dataParent === 'true' - categoryArr.sort((a, b) => { return b.value - a.value }) - function translateListToTree (data, parent) { - let tree = [] - let temp - data.forEach((item, index) => { - if (data[index].parentId == parent) { - let obj = data[index]; - temp = translateListToTree(data, data[index].id); - if (temp.length > 0) { - obj.children = temp - } - if (tree.indexOf()) - tree.push(obj) - } - }) - return tree - } - const categoryNameJson = JSON.stringify(categoryArr.map(function (category) { return category.name })) - const categoryArrJson = JSON.stringify(categoryArr) - const categoryArrParentJson = JSON.stringify(translateListToTree(categoryArr, '0')) - - return ` - ` -} - - -function switchPostChart () { - // 这里为了统一颜色选取的是“明暗模式”下的两种字体颜色,也可以自己定义 - let color = document.documentElement.getAttribute('data-theme') === 'light' ? '#4C4948' : 'rgba(255,255,255,0.7)' - if (document.getElementById('posts-chart') && postsOption) { - try { - let postsOptionNew = postsOption - postsOptionNew.title.textStyle.color = color - postsOptionNew.xAxis.nameTextStyle.color = color - postsOptionNew.yAxis.nameTextStyle.color = color - postsOptionNew.xAxis.axisLabel.color = color - postsOptionNew.yAxis.axisLabel.color = color - postsOptionNew.xAxis.axisLine.lineStyle.color = color - postsOptionNew.yAxis.axisLine.lineStyle.color = color - postsOptionNew.series[0].markLine.data[0].label.color = color - postsChart.setOption(postsOptionNew) - } catch (error) { - console.log(error) - } - } - if (document.getElementById('tags-chart') && tagsOption) { - try { - let tagsOptionNew = tagsOption - tagsOptionNew.title.textStyle.color = color - tagsOptionNew.xAxis.nameTextStyle.color = color - tagsOptionNew.yAxis.nameTextStyle.color = color - tagsOptionNew.xAxis.axisLabel.color = color - tagsOptionNew.yAxis.axisLabel.color = color - tagsOptionNew.xAxis.axisLine.lineStyle.color = color - tagsOptionNew.yAxis.axisLine.lineStyle.color = color - tagsOptionNew.series[0].markLine.data[0].label.color = color - tagsChart.setOption(tagsOptionNew) - } catch (error) { - console.log(error) - } - } - if (document.getElementById('categories-chart') && categoriesOption) { - try { - let categoriesOptionNew = categoriesOption - categoriesOptionNew.title.textStyle.color = color - categoriesOptionNew.legend.textStyle.color = color - if (!categoryParentFlag) { categoriesOptionNew.series[0].label.color = color } - categoriesChart.setOption(categoriesOptionNew) - } catch (error) { - console.log(error) - } - } -} -document.getElementById("mode-button").addEventListener("click", function () { setTimeout(switchPostChart, 100) }) \ No newline at end of file