-
Notifications
You must be signed in to change notification settings - Fork 17
/
generateShareMD.js
executable file
·82 lines (79 loc) · 2.47 KB
/
generateShareMD.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
71
72
73
74
75
76
77
78
79
80
81
82
import fs from 'fs'
import path from 'path'
import mkdirp from 'mkdirp'
let deleted = require('./unauthorizedPost')
let basePath = './source/_posts'
let targetPath = './share/'
let originInfo = new Promise(function (resolve, reject) {
fs.readdir(basePath, (err, files) => {
if (err) throw err
resolve(files.filter(file => !((file.indexOf(".") === 0) || (file in deleted.file))))
})
})
.then(files => Promise.all(
files.map(
file => new Promise((resolve, reject) =>
fs.readFile(path.join(basePath, file), function (err, content) {
if (err) throw err
content = content.toString()
resolve({
dirName: content.match(/permalink: (.*)/)[1].trim(),
fileName: content.match(/title: "?(.*)"?/)[1].trim(),
content: content
})
})
)
)
))
.then(files => files.map(file => {
function changeToTab(code){
code = code.replace(/```/g, "")
code = code.trim()
code = code.startsWith("Swift") || code.startsWith("swift") ? code.substr(5) : code
code = code.replace(/\n/g, "\n ")
code = " " + code
return code
}
let origin = file.content
origin = origin.substr(origin.indexOf("---") + 3)
origin = origin.replace(/!\[(.*?)\]\(\/(.*?)\)/g, "![$1](http://swift.gg/$2)")
origin = origin.replace(/(```[.\s\S]*?```)/gm, changeToTab)
origin = origin.replace(/<center>!\[给译者打赏\]\(.*?\)<\/center>/, "")
origin = origin.replace("<!--more-->", "")
origin = origin.replace("<!--此处开始正文-->", "")
let jianshu = origin
jianshu = file.fileName + "\n" + jianshu
let wechat = file.fileName + "\n" + origin
return {
dirName: file.dirName,
jianshu: jianshu,
wechat: wechat
}
}))
.then(shareContents => shareContents.map(
shareContent => {
new Promise((resolve, reject) => {
let fullPath = path.join(targetPath, shareContent.dirName, "jianshu.md")
mkdirp(path.dirname(fullPath), err => {
if (err) throw err
fs.writeFile(fullPath, shareContent.jianshu, (err) => {
if (err) throw err
resolve()
})
})
})
.then(() => {
new Promise((resolve, reject) => {
let fullPath = path.join(targetPath, shareContent.dirName, "wechat.md")
mkdirp(path.dirname(fullPath), err => {
if (err) throw err
fs.writeFile(fullPath, shareContent.wechat, (err) => {
if (err) throw err
resolve()
})
})
})
})
}
))
.catch(err => console.log(err))