diff --git a/scripts/tags/lib/timeline.js b/scripts/tags/lib/timeline.js index ce38c417..d48490c6 100644 --- a/scripts/tags/lib/timeline.js +++ b/scripts/tags/lib/timeline.js @@ -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 += '
' @@ -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 = '' @@ -48,23 +77,17 @@ module.exports = ctx => function(args, content = '') { el += '
' } - var arr = content.split(//g).filter(item => item.trim().length > 0) + var arr = content.split(//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 += '
' el += layoutNodeTitle(ctx, node.header) @@ -75,4 +98,4 @@ module.exports = ctx => function(args, content = '') { el += '
' return el -} +} \ No newline at end of file