Merge pull request #1575 from SeaYJ/dev

添加對 ChartJS 雙模式的支持
This commit is contained in:
Jerry Wong
2024-10-13 19:23:14 +08:00
committed by GitHub
9 changed files with 213 additions and 3 deletions

View File

@@ -341,7 +341,7 @@ hexo.extend.filter.register('before_generate', () => {
user_id: null,
pageSize: 10,
order_by: 'social',
lang: 'zh_TW'
lang: 'en_US'
},
twikoo: {
envId: null,
@@ -512,6 +512,21 @@ hexo.extend.filter.register('before_generate', () => {
dark: 'dark'
}
},
chartjs: {
enable: false,
fontColor: {
light: 'rgba(0, 0, 0, 0.8)',
dark: 'rgba(255, 255, 255, 0.8)'
},
borderColor: {
light: 'rgba(0, 0, 0, 0.1)',
dark: 'rgba(255, 255, 255, 0.2)'
},
scale_ticks_backdropColor: {
light: 'transparent',
dark: 'transparent'
}
},
note: {
style: 'flat',
icons: true,

49
scripts/tag/chartjs.js Normal file
View File

@@ -0,0 +1,49 @@
/**
* Butterfly
* chartjs
* https://www.chartjs.org/
* {% chartjs [width, abreast, chartId] %}
* <!-- chart -->
* <!-- endchart -->
* <!-- desc -->
* <!-- enddesc -->
* {% endchartjs %}
*/
'use strict'
const { escapeHTML } = require('hexo-util')
const chartjs = (args, content) => {
if (!content) return
const chartRegex = /<!--\s*chart\s*-->\n([\w\W\s\S]*?)<!--\s*endchart\s*-->/
const descRegex = /<!--\s*desc\s*-->\n([\w\W\s\S]*?)<!--\s*enddesc\s*-->/
const selfConfig = args.join(' ').trim()
const [width = '', layout = false, chartId = ''] = selfConfig.split(',').map(s => s.trim())
const chartMatch = content.match(chartRegex)
const descMatch = content.match(descRegex)
if (!chartMatch) {
hexo.log.warn('chartjs tag: chart content is required!')
return
}
const chartConfig = chartMatch && chartMatch[1] ? chartMatch[1] : ''
const descContent = descMatch && descMatch[1] ? descMatch[1] : ''
const renderedDesc = descContent ? hexo.render.renderSync({ text: descContent, engine: 'markdown' }).trim() : ''
const descDOM = renderedDesc ? `<div class="chartjs-desc">${renderedDesc}</div>` : ''
const abreastClass = layout ? ' chartjs-abreast' : ''
const widthStyle = width ? `data-width="${width}%"` : ''
return `<div class="chartjs-container${abreastClass}" data-chartjs-id="${chartId}" ${widthStyle}>
<pre class="chartjs-src" hidden>${escapeHTML(chartConfig)}</pre>
${descDOM}
</div>`
}
hexo.extend.tag.register('chartjs', chartjs, { ends: true })