Files
hexo-theme-butterfly/scripts/tag/timeline.js
myw 0d0001c808 feat: 更新 plugins.yml 中的依賴版本至最新
feat: 優化 aside_archives ,改進性能和可讀性
feat: 改善 inlineImg 和 timeline 標籤的文檔,優化時間線邏輯
feat: 更新 gallery 標籤以支持額外參數,優化圖片顯示邏輯
improvement: 優化隨機封面過濾器邏輯, 避免連續重複
feat: 最新評論限制顯示 1-10 條之間
fix: artalk 的最新評論顯示待定或者封禁的評論的 bug
2024-12-10 20:35:58 +08:00

51 lines
1.4 KiB
JavaScript

/**
* Timeline tag for Hexo
* Syntax:
* {% timeline [headline],[color] %}
* <!-- timeline [title] -->
* [content]
* <!-- endtimeline -->
* <!-- timeline [title] -->
* [content]
* <!-- endtimeline -->
* {% endtimeline %}
*/
'use strict'
const timeLineFn = (args, content) => {
// Use named capture groups for better readability
const tlBlock = /<!--\s*timeline\s*(?<title>.*?)\s*-->\n(?<content>[\s\S]*?)<!--\s*endtimeline\s*-->/g
// Pre-compile markdown render function
const renderMd = text => hexo.render.renderSync({ text, engine: 'markdown' })
// Parse arguments more efficiently
const [text, color = ''] = args.length ? args.join(' ').split(',') : []
// Build initial headline if text exists
const headline = text
? `<div class='timeline-item headline'>
<div class='timeline-item-title'>
<div class='item-circle'>${renderMd(text)}</div>
</div>
</div>`
: ''
// Match all timeline blocks in one pass and transform
const items = Array.from(content.matchAll(tlBlock))
.map(({ groups: { title, content } }) =>
`<div class='timeline-item'>
<div class='timeline-item-title'>
<div class='item-circle'>${renderMd(title)}</div>
</div>
<div class='timeline-item-content'>${renderMd(content)}</div>
</div>`
)
.join('')
return `<div class="timeline ${color}">${headline}${items}</div>`
}
hexo.extend.tag.register('timeline', timeLineFn, { ends: true })