-
Notifications
You must be signed in to change notification settings - Fork 1
/
md-provider.js
98 lines (90 loc) · 2.56 KB
/
md-provider.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const fs = require('fs')
const chokidar = require('chokidar')
const parser = require('./md-parser')
const defaultMeta = {
title: 'no name slide',
author: 'unknown',
}
class Provider {
constructor(path) {
this.path = path
this.article = ''
this.watcher = chokidar.watch(this.path)
this.watcher.on('all', (event, path) => {
if (event == 'change') {
this.renew()
} else if (event == 'unlink') {
this.release()
}
})
this.renew()
}
release() {
this.watcher.close()
}
async renew() {
try {
this.raw = await new Promise((resolve, reject) => {
fs.readFile(this.path, { encoding: 'utf8' }, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
parser.meta = {}
parser.renderReset()
this.article = parser.render(this.raw)
this.meta = Object.assign(
Object.assign({}, defaultMeta),
parser.meta
)
// make offline data:
let offlineEnv = {
offline: true,
offlineResource: [],
resourceIndex: 0,
}
parser.renderReset()
let offlineArticle = parser.render(this.raw, offlineEnv)
this.offline = {
resource: offlineEnv.offlineResource,
article: offlineArticle,
meta: {
author: this.meta.author,
title: this.meta.title,
},
}
} catch (e) {
console.log(e.toString())
}
}
get() {
return this.article
}
getOffline() {
return this.offline
}
}
Provider.env = {
style: (parser['style-url'] || [])
.map(
(url) =>
`<link rel="stylesheet" type="text/css" media="all" href="${url}">`
)
.join('\n'),
script: (parser['script-url'] || [])
.map((url) => `<script src="${url}"></script>`)
.join('\n'),
}
Provider.offlineEnv = {
style: (parser['offline-style-url'] || [])
.map(
(url) =>
`<link rel="stylesheet" type="text/css" media="all" href="${url}">`
)
.join('\n'),
script: (parser['offline-script-url'] || [])
.map((url) => `<script src="${url}"></script>`)
.join('\n'),
}
Provider.pluginFiles = parser.staticPaths
module.exports = Provider