()
| 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 = `--- |
| 61 | title: '${data.title}' |
| 62 | date: ${data.date} |
| 63 | tags: [${newTagString}] |
| 64 | published: ${data.published || false} |
| 65 | hideInList: ${data.hideInList || false} |
| 66 | feature: ${data.feature || ''} |
| 67 | isTop: ${data.isTop || false} |
| 68 | --- |
| 69 | ${postMatter.content}` |
| 70 | |
| 71 | fixedResults[index] = mdStr |
| 72 | fse.writeFileSync(`${this.postDir}/${files[index]}`, mdStr) |
| 73 | } |
| 74 | } |
| 75 | })) |
| 76 | |
| 77 | fixedResults.forEach((result: any, index: any) => { |
| 78 | const postMatter = matter(result) |
| 79 | |
| 80 | const data = (postMatter.data as any) |
| 81 | |
| 82 | // Remove useless `'` in formatYamlString generate |
| 83 | if (data && data.title) { |
no test coverage detected