Skip to content

Commit 4cbdf56

Browse files
committed
feat: include post id in file name
1 parent 0edeade commit 4cbdf56

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

src/cmd/post-list/open-post-in-vscode.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import sanitizeFileName from 'sanitize-filename'
99
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
1010
import { fsUtil } from '@/infra/fs/fsUtil'
1111

12-
export function buildLocalPostFileUri(post: Post, includePostId = false): Uri {
12+
export function buildLocalPostFileUri(post: Post, appendToFileName = ''): Uri {
1313
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
1414
const ext = `.${post.isMarkdown ? 'md' : 'html'}`
15-
const postIdSegment = includePostId ? `.${post.id}` : ''
1615
const postTitle = sanitizeFileName(post.title)
17-
18-
return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`)
16+
return Uri.joinPath(workspaceUri, `${postTitle}${appendToFileName}.${post.id}${ext}`)
1917
}
2018

2119
export async function openPostInVscode(postId: number, forceUpdateLocalPostFile = false): Promise<Uri | false> {
@@ -40,14 +38,14 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
4038
// 博文尚未关联到本地文件的情况
4139
// 本地存在和博文同名的文件, 询问用户是要覆盖还是同时保留两者
4240
if (mappedPostFilePath === undefined && (await fsUtil.exists(fileUri.fsPath))) {
43-
const opt = ['保留本地文件并以博文 ID 为文件名新建另一个文件', '覆盖本地文件']
41+
const opt = ['保留本地文件并新建另一个文件', '覆盖本地文件']
4442
const selected = await Alert.info(
4543
`无法建立博文与本地文件的关联, 文件名冲突`,
4644
{ detail: `本地已存在名为 ${path.basename(fileUri.fsPath)} 的文件`, modal: true },
4745
...opt
4846
)
4947

50-
if (selected === opt[0]) fileUri = buildLocalPostFileUri(post, true)
48+
if (selected === opt[0]) fileUri = buildLocalPostFileUri(post, '-new')
5149
}
5250

5351
// 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件

src/cmd/post-list/post-pull-all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function postPullAll() {
7373
!(await fsUtil.exists(path)) ||
7474
path.indexOf(WorkspaceCfg.getWorkspaceUri().fsPath) < 0
7575
) {
76-
const uri = buildLocalPostFileUri(post, false)
76+
const uri = buildLocalPostFileUri(post)
7777
const buf = Buffer.from(post.postBody)
7878
await workspace.fs.writeFile(uri, buf)
7979
await PostFileMapManager.updateOrCreate(post.id, uri.path)

src/cmd/post-list/post-pull.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
1919
const path = PostFileMapManager.getFilePath(post.id)
2020
if (path === undefined || !(await fsUtil.exists(path))) {
2121
isFreshPull = true
22-
const uri = buildLocalPostFileUri(post, false)
22+
const uri = buildLocalPostFileUri(post)
2323
await workspace.fs.writeFile(uri, Buffer.from(post.postBody))
2424
await PostFileMapManager.updateOrCreate(post.id, uri.path)
2525
await handlePostInput(input, ctxList, uri.path)

0 commit comments

Comments
 (0)