MCPcopy Create free account
hub / github.com/getgridea/gridea / Posts

Class Posts

src/server/posts.ts:15–247  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13Bluebird.promisifyAll(fs)
14
15export default class Posts extends Model {
16 postDir: string
17
18 postImageDir: string
19
20 constructor(appInstance: any) {
21 super(appInstance)
22 this.postDir = path.join(this.appDir, 'posts')
23 this.postImageDir = `${this.appDir}/post-images`
24 }
25
26 public async savePosts() {
27 const resultList: any = []
28 const requestList: any = []
29 let files = await fse.readdir(this.postDir)
30
31 files = files.filter(junk.not)
32 files.forEach((item) => {
33 requestList.push(fs.readFileSync(path.join(this.postDir, item), 'utf8'))
34 })
35 const results = await Bluebird.all(requestList)
36 const fixedResults = JSON.parse(JSON.stringify(results))
37 /**
38 * The format of the correction `tag` is changed from a string to an array, and the article source file is updated. from v0.7.6
39 */
40 await Promise.all(results.map(async (result: any, index: any) => {
41 const postMatter = matter(result)
42 const data = (postMatter.data as any)
43
44 data.title = formatYamlString(data.title)
45
46 if (data && data.date) {
47 if (typeof data.date === 'string') {
48 data.date = moment(data.date).format('YYYY-MM-DD HH:mm:ss')
49 } else {
50 data.date = moment(data.date).subtract(8, 'hours').format('YYYY-MM-DD HH:mm:ss')
51 }
52 }
53
54 // If there is a `tag` and it is of string type, it is corrected to array type.
55 if (data && typeof data.tags === 'string') {
56 const tagReg = /tags: [^\s[]/i
57 const newTagString = data.tags.split(' ').toString()
58
59 if (tagReg.test(result)) {
60 const mdStr = `---
61title: '${data.title}'
62date: ${data.date}
63tags: [${newTagString}]
64published: ${data.published || false}
65hideInList: ${data.hideInList || false}
66feature: ${data.feature || ''}
67isTop: ${data.isTop || false}
68---
69${postMatter.content}`
70
71 fixedResults[index] = mdStr
72 fse.writeFileSync(`${this.postDir}/${files[index]}`, mdStr)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected