-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub.js
70 lines (54 loc) · 1.89 KB
/
pub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs')
const slugify = require('slugify')
const moment = require('moment')
const path = require('path')
const logs = fs.readdirSync('./pub')
const newfiles = logs.filter(f => {
// no start with date and no start with .
return !f.match(/\d\d\d\d-\d\d-\d\d/) && !f.match(/^[\.]/)
})
newfiles.forEach(f => {
let src = fs.readFileSync(`./pub/${f}`, 'utf8')
const rawtitle = f.split('.')[0]
const title = rawtitle
const filename = slugify(title).toLowerCase()
src = src.replace('# ', '').replace(title, '')
let re = /!\[(.*)\]\((.*)\)/gim
let mtc = src.match(re)
if (mtc) {
mtc.forEach(d => {
re = /!\[(.*)\]\((.*)\)/gim
const rex = re.exec(d)
const r = parseInt(Math.random() * 1000)
const absPath = path.resolve('pub', rex[2])
const assetName = slugify(`n${r}_` + path.basename(absPath)).toLowerCase()
const newpath = `./static/assets/log/${assetName}`
fs.renameSync(absPath, newpath)
const newtag = `![](/assets/log/${assetName})`
src = src.replace(rex[0], newtag)
})
}
re = /<video src="(.*)"><\/video>/gim
mtc = src.match(re)
if (mtc) {
mtc.forEach(d => {
const rex = re.exec(d)
const r = parseInt(Math.random() * 1000)
const absPath = path.resolve('pub', rex[1])
const assetName = slugify(`n${r}_` + path.basename(absPath)).toLowerCase()
const newpath = `./static/assets/log/${assetName}`
fs.renameSync(absPath, newpath)
src = src.replace(rex[0], `<video autoplay muted loop src="/assets/log/${assetName}"></video>`)
})
}
const today = moment().subtract(4, 'hours')
const frontmatter = `---
title: ${title}
date: ${today.format('YYYY-MM-DD HH:mm:00 +0100')}
---
`
const dest = frontmatter + src
const finalname = `${today.format('YYYY-MM-DD')}-${filename}.md`
fs.writeFileSync(`./content/log/${finalname}`, dest)
fs.unlinkSync(`./pub/${f}`)
})