Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 更新 timeline 组件的功能,可以自动显示时间戳 #539

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions scripts/tags/lib/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

'use strict'

const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

function layoutNodeTitle(ctx, content) {
var el = ''
el += '<div class="header">'
Expand All @@ -34,6 +38,31 @@ function layoutNodeContent(ctx, content) {
return el
}

function getCurrentTimestamp() {
return new Date().toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
function getOrCreateTimestamp(content, ctx) {
const timestampFile = path.join(ctx.base_dir, 'timeline_timestamps.json');
let timestamps = {};
if (fs.existsSync(timestampFile)) {
timestamps = JSON.parse(fs.readFileSync(timestampFile, 'utf8'));
}

const hash = crypto.createHash('md5').update(content).digest('hex');
if (!timestamps[hash]) {
timestamps[hash] = getCurrentTimestamp();
fs.writeFileSync(timestampFile, JSON.stringify(timestamps, null, 2));
}

return timestamps[hash];
}

module.exports = ctx => function(args, content = '') {
args = ctx.args.map(args, ['api', 'user', 'type', 'limit', 'hide', 'avatar'])
var el = ''
Expand All @@ -48,23 +77,17 @@ module.exports = ctx => function(args, content = '') {
el += '<div class="tag-plugin timeline">'
}

var arr = content.split(/<!--\s*node (.*?)\s*-->/g).filter(item => item.trim().length > 0)
var arr = content.split(/<!--\s*node(.*?)\s*-->/g)
if (arr.length > 0) {
var nodes = []
arr.forEach((item, i) => {
if (i % 2 == 0) {
nodes.push({
header: item
})
} else if (nodes.length > 0) {
var node = nodes[nodes.length-1]
if (node.body == undefined) {
node.body = item
} else {
node.body += '\n' + item
}
}
})
for (let i = 1; i < arr.length; i += 2) {
let header = arr[i].trim();
let body = arr[i + 1] ? arr[i + 1].trim() : '';
nodes.push({
header: header || getOrCreateTimestamp(body, ctx),
body: body
})
}
nodes.forEach((node, i) => {
el += '<div class="timenode" index="' + (i) + '">'
el += layoutNodeTitle(ctx, node.header)
Expand All @@ -75,4 +98,4 @@ module.exports = ctx => function(args, content = '') {

el += '</div>'
return el
}
}