* Save Post to file * @param post
(post: IPost)
| 158 | * @param post |
| 159 | */ |
| 160 | async savePostToFile(post: IPost): Promise<IPost | null> { |
| 161 | const helper = new ContentHelper() |
| 162 | const content = helper.changeImageUrlLocalToDomain(post.content, this.db.setting.domain) |
| 163 | const extendName = (post.featureImage.name || 'jpg').split('.').pop() |
| 164 | |
| 165 | post.title = formatYamlString(post.title) |
| 166 | |
| 167 | const mdStr = `--- |
| 168 | title: '${post.title}' |
| 169 | date: ${post.date} |
| 170 | tags: [${post.tags.join(',')}] |
| 171 | published: ${post.published} |
| 172 | hideInList: ${post.hideInList} |
| 173 | feature: ${post.featureImage.name ? `/post-images/${post.fileName}.${extendName}` : post.featureImagePath} |
| 174 | isTop: ${post.isTop} |
| 175 | --- |
| 176 | ${content}` |
| 177 | |
| 178 | try { |
| 179 | // If exist feature image |
| 180 | if (post.featureImage.path) { |
| 181 | const filePath = `${this.postImageDir}/${post.fileName}.${extendName}` |
| 182 | |
| 183 | if (post.featureImage.path !== filePath) { |
| 184 | fse.copySync(post.featureImage.path, filePath) |
| 185 | |
| 186 | // Clean the old file |
| 187 | if (post.featureImage.path.includes(this.postImageDir)) { |
| 188 | fse.removeSync(post.featureImage.path) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Write file must use fse, beause fs.writeFile need callback |
| 194 | await fse.writeFile(`${this.postDir}/${post.fileName}.md`, mdStr) |
| 195 | |
| 196 | // Clean the old file |
| 197 | if (post.deleteFileName) { |
| 198 | fse.removeSync(`${this.postDir}/${post.deleteFileName}.md`) |
| 199 | } |
| 200 | } catch (e) { |
| 201 | console.error('ERROR: ', e) |
| 202 | } |
| 203 | return post |
| 204 | } |
| 205 | |
| 206 | async deletePost(post: IPostDb) { |
| 207 | try { |
no test coverage detected