forked from WeiWenda/effect-note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressDocCurd.js
202 lines (193 loc) · 7.11 KB
/
expressDocCurd.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const express = require('express'),
router = express.Router();
const git = require("isomorphic-git");
const fs = require("fs");
const {listFiles, deleteFile, writeFile, commit, getGitConfig, store} = require("./isomorphicGit");
const path = require("path");
const lunr = require("lunr");
const jieba = require('@node-rs/jieba');
const docId2path = {};
jieba.load();
require("lunr-languages/lunr.stemmer.support")(lunr)
require('lunr-languages/lunr.multi')(lunr)
require('lunr-languages/lunr.zh')(lunr)
const punctuationSplit = function (builder) {
const pipelineFunction = function (token) {
// console.log('token:' + token.toString());
return token.toString().split(new RegExp("[^\u4E00-\u9FA5a-zA-Z0-9]")).flatMap(function (str) {
return jieba.cut(str, true).map(word => {
return token.clone().update(function () {
return word
})
})
})
}
lunr.Pipeline.registerFunction(pipelineFunction, 'punctuationSplit')
builder.pipeline.add(pipelineFunction)
}
let subscriptionIndex = null;
async function refreshIndex() {
const files = await listFiles();
subscriptionIndex = lunr(function() {
this.use(lunr.multiLanguage('en', 'zh'))
this.use(punctuationSplit)
this.ref("id")
this.field("title")
this.field("content")
// this.add({
// 'id': 1,
// 'title': '欢迎文档草稿',
// 'content': '欢迎文档草稿123214',
// })
const gitHome = getGitConfig().gitHome;
files.forEach((filepath, index) => {
const docId = filepath.split('/').pop().split('#').shift()
docId2path[docId] = filepath;
const content = fs.readFileSync(path.join(gitHome, filepath), {encoding: 'utf-8'});
this.add({
'id': index,
'title': filepath,
'content': content,
})
}, this)
})
}
refreshIndex();
function constructDocInfo(content, filepath) {
const paths = filepath.split('/');
const nameSplits = paths.pop().split(new RegExp('[#\[\\]\.]'));
const id = Number(nameSplits.shift());
const name = nameSplits.shift();
const tag = paths.join('/');
const otherTags = nameSplits.filter(split => {
return split && split !== 'json' && split !== 'effect'
})
return {name,
filename: filepath.split('/').pop(),
tag: tag ? JSON.stringify([tag].concat(otherTags)) : JSON.stringify([]), content, id}
}
router.get('/reindex', (req, res) => {
refreshIndex().then(() => {
res.send({message: '更新成功!'})
})
})
router.get('/search', (req, res) => {
res.send(subscriptionIndex.search(req.query.search))
})
router.get('/', (req, res, next) => {
listFiles().then(files => {
const result = [{name: '欢迎使用Effect笔记', filename: 'help.effect.json', tag: JSON.stringify([]), id: -1}].concat(
files.map((file, index) => {
return constructDocInfo(undefined, file);
})
);
res.send({content: result});
}).catch(() => {
res.send({content: []});
});
});
router.post('/', (req, res) => {
const filename = req.body.name;
const docId = req.body.id;
const tags = JSON.parse(req.body.tag);
const dir = tags.shift() || '';
const actualFilename = `${docId}#${filename}${tags.map(tag => '[' + tag.replace('/', ':') + ']').join('')}.effect.json`
const content = req.body.content;
writeFile(dir, actualFilename, content).then(() => {
commit().then(() => {
docId2path[docId] = path.join(dir, actualFilename);
res.send({message: 'save success', id: docId});
})
});
});
router.get('/:docId/versions', async (req, res) => {
const {gitHome} = getGitConfig();
const docId = Number(req.params.docId);
const filepath = docId2path[docId];
const commits = await git.log({ fs, dir: gitHome })
let lastSHA = null
let lastCommit = null
const commitsThatMatter = []
for (const commit of commits) {
try {
const o = await git.readObject({ fs, dir: gitHome, oid: commit.oid, filepath })
if (o.oid !== lastSHA) {
if (lastSHA !== null) commitsThatMatter.push(lastCommit)
lastSHA = o.oid
}
} catch (err) {
// file no longer there
commitsThatMatter.push(lastCommit)
break
}
lastCommit = commit
}
res.send(commitsThatMatter.map(commit => {
return {commitId: commit.oid, time: commit.commit.author.timestamp};
}))
})
router.get('/:docId', async (req, res) => {
const {gitHome} = getGitConfig();
const docId = Number(req.params.docId);
const filepath = docId2path[docId];
let commitOid = req.query.version
if (commitOid === 'HEAD') {
commitOid = await git.resolveRef({ fs, dir: gitHome, ref: 'HEAD' });
}
const { blob } = await git.readBlob({
fs,
dir: gitHome,
oid: commitOid,
filepath: filepath
});
res.send(constructDocInfo(Buffer.from(blob).toString('utf8'), filepath));
});
router.delete('/:docId', async (req, res) => {
const {gitHome} = getGitConfig();
const docId = Number(req.params.docId);
const filepath = docId2path[docId];
delete docId2path[docId];
deleteFile(path.join(gitHome, filepath)).then(() => {
commit().then(() => {
res.send({message: 'delete success!'});
})
})
})
router.put('/:docId', async (req, res) => {
if (req.params.docId === '-1') {
res.send({message: '帮助文档无法保存', id: req.params.docId});
} else {
const {gitHome} = getGitConfig();
const docId = Number(req.params.docId);
const oldFilepath = docId2path[docId].split('/');
const oldFilename = oldFilepath.pop();
const oldDir = oldFilepath.join('/');
const filename = req.body.name;
const tags = JSON.parse(req.body.tag);
const dir = tags.shift() || '';
const actualFilename = `${docId}#${filename}${tags.map(tag => '[' + tag.replace('/', ':') + ']').join('')}.effect.json`
const content = req.body.content;
if (actualFilename === oldFilename && dir === oldDir) {
console.log('仅修改内容')
// 仅修改内容
writeFile(dir, actualFilename, content).then(() => {
commit().then(() => {
res.send({message: 'save success', id: req.params.docId});
})
});
} else {
console.log('重命名或移动目录')
// 重命名或移动目录
deleteFile(path.join(gitHome, docId2path[docId])).then(() => {
writeFile(dir, actualFilename, content).then(() => {
commit().then(() => {
docId2path[docId] = path.join(dir, actualFilename);
res.send({message: 'save success', id: req.params.docId});
})
});
})
}
}
});
// this is required
module.exports = {router, punctuationSplit};