Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,6 @@
"editPresentation": "singlelineText",
"markdownDescription": "macOS 上 Chromium 可执行文件路径, 用于进行 PDF 导出等操作"
},
"cnblogsClient.createLocalPostFileWithCategory": {
"order": 5,
"default": true,
"scope": "application",
"type": "boolean",
"markdownDescription": "创建本地博文时, 是否根据博文分类保存到对应的文件夹中"
},
"cnblogsClient.pageSize.postList": {
"order": 6,
"default": 30,
Expand Down
15 changes: 1 addition & 14 deletions src/cmd/post-cat/update-post-cat-treeview.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ProgressLocation, window, Uri, workspace } from 'vscode'
import { ProgressLocation, window } from 'vscode'
import { PostCat } from '@/model/post-cat'
import { PostCatService } from '@/service/post/post-cat'
import { inputPostCat } from './input-post-cat'
import { PostCatCfg } from '@/ctx/cfg/post-cat'
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
import { Alert } from '@/infra/alert'
import { PostCatTreeItem } from '@/tree-view/model/post-category-tree-item'
import { extTreeViews } from '@/tree-view/tree-view-register'
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
import { fsUtil } from '@/infra/fs/fsUtil'

export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
let category: PostCat
Expand Down Expand Up @@ -36,16 +33,6 @@ export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
try {
await PostCatService.update(updateDto)
postCategoryDataProvider.refresh()
// 如果选择了createLocalPostFileWithCategory模式且本地有该目录,则重命名该目录
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory()
const path = Uri.joinPath(workspaceUri, category.title).fsPath
const isFileExist = await fsUtil.exists(path)
if (shouldCreateLocalPostFileWithCategory && isFileExist) {
const oldUri = Uri.joinPath(workspaceUri, category.title)
const newUri = Uri.joinPath(workspaceUri, addDto.title)
await workspace.fs.rename(oldUri, newUri)
}
p.report({ increment: 100 })
} catch (e) {
void Alert.err(`更新博文失败: ${<string>e}`)
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/post-list/open-post-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LocalPost } from '@/service/local-post'
import { fsUtil } from '@/infra/fs/fsUtil'
import { postPull } from './post-pull'
import { Alert } from '@/infra/alert'
import { WorkspaceCfg } from '@/ctx/cfg/workspace'

export async function openPostFile(
post: LocalPost | Post | string,
Expand All @@ -17,7 +18,10 @@ export async function openPostFile(
filePath = post.filePath
} else if (post instanceof Post) {
filePath = PostFileMapManager.getFilePath(post.id) ?? ''
if (autoPull) if (!(await fsUtil.exists(filePath))) await postPull(post, false, true)
if (autoPull) {
if (!(await fsUtil.exists(filePath)) || filePath.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0)
await postPull(post, false, true)
}
} else {
filePath = post
}
Expand Down
26 changes: 4 additions & 22 deletions src/cmd/post-list/open-post-in-vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,17 @@ import { Alert } from '@/infra/alert'
import { PostService } from '@/service/post/post'
import { PostFileMapManager } from '@/service/post/post-file-map'
import { openPostFile } from './open-post-file'
import { PostCatService } from '@/service/post/post-cat'
import sanitizeFileName from 'sanitize-filename'
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
import { PostCatCfg } from '@/ctx/cfg/post-cat'
import { fsUtil } from '@/infra/fs/fsUtil'

export async function buildLocalPostFileUri(post: Post, includePostId = false): Promise<Uri> {
export function buildLocalPostFileUri(post: Post, includePostId = false): Uri {
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory()
const ext = `.${post.isMarkdown ? 'md' : 'html'}`
const postIdSegment = includePostId ? `.${post.id}` : ''
const postTitle = sanitizeFileName(post.title)

if (!shouldCreateLocalPostFileWithCategory) return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`)

const firstCategoryId = post.categoryIds?.[0] ?? null
let i = firstCategoryId !== null ? await PostCatService.getOne(firstCategoryId) : null
let categoryTitle = ''
while (i != null) {
categoryTitle = path.join(
sanitizeFileName(i.title, {
replacement: invalidChar => (invalidChar === '/' ? '_' : ''),
}),
categoryTitle
)
i = i.parent ?? null
}

return Uri.joinPath(workspaceUri, categoryTitle, `${postTitle}${postIdSegment}${ext}`)
return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`)
}

export async function openPostInVscode(postId: number, forceUpdateLocalPostFile = false): Promise<Uri | false> {
Expand All @@ -53,7 +35,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile

const workspaceUri = WorkspaceCfg.getWorkspaceUri()
await mkDirIfNotExist(workspaceUri)
let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : await buildLocalPostFileUri(post)
let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : buildLocalPostFileUri(post)

// 博文尚未关联到本地文件的情况
// 本地存在和博文同名的文件, 询问用户是要覆盖还是同时保留两者
Expand All @@ -65,7 +47,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
...opt
)

if (selected === opt[0]) fileUri = await buildLocalPostFileUri(post, true)
if (selected === opt[0]) fileUri = buildLocalPostFileUri(post, true)
}

// 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/post-pull-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function postPullAll() {

// 本地没有博文或关联到的文件不存在
if (path === undefined || !(await fsUtil.exists(path))) {
const uri = await buildLocalPostFileUri(post, false)
const uri = buildLocalPostFileUri(post, false)
const buf = Buffer.from(post.postBody)
await workspace.fs.writeFile(uri, buf)
await PostFileMapManager.updateOrCreate(post.id, uri.path)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/post-list/post-pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
path.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0
) {
isFreshPull = true
const uri = await buildLocalPostFileUri(post, false)
const uri = buildLocalPostFileUri(post, false)
await workspace.fs.writeFile(uri, Buffer.from(post.postBody))
await PostFileMapManager.updateOrCreate(post.id, uri.path)
await handlePostInput(input, ctxList, uri.path)
Expand Down
13 changes: 0 additions & 13 deletions src/ctx/cfg/post-cat.ts

This file was deleted.