-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add post #2
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function newPostTemplate(): string { | ||
const now = new Date().toISOString() | ||
return `--- | ||
title: | ||
date: ${now} | ||
updated: ${now} | ||
tags: | ||
- | ||
categories: | ||
---\n` | ||
} | ||
|
||
export { newPostTemplate } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
import { Buffer } from 'node:buffer' | ||
import type { ProviderResult, TreeDataProvider } from 'vscode' | ||
import { EventEmitter } from 'vscode' | ||
import { EventEmitter, Uri, window, workspace } from 'vscode' | ||
import { ctx } from '../ctx' | ||
import { newPostTemplate } from '../../res/md/template' | ||
import { PostItem } from './PostItem' | ||
|
||
export class PostsProvider implements TreeDataProvider<PostItem> { | ||
private _onDidChangeTreeData = new EventEmitter<PostItem | undefined>() | ||
readonly onDidChangeTreeData = this._onDidChangeTreeData.event | ||
|
||
async add(): Promise<void> { | ||
const EXIST_NOTICE_MSG = 'A post with the same name already exists' | ||
|
||
const input1 = await window.showInputBox({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a clearer name: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remember to use |
||
placeHolder: 'Type the filename/scaffold\'s name of the new post', | ||
}) | ||
|
||
if (input1) { | ||
const uri = workspace.workspaceFolders?.[0].uri | ||
if (!uri) | ||
return | ||
|
||
try { | ||
await workspace.fs.stat(Uri.joinPath(uri, '/scaffolds', `${input1}.md`)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or a select (query option form scaffolds) |
||
const input2 = await window.showInputBox({ | ||
placeHolder: 'Type the filename of the new post', | ||
}) | ||
if (input2) { | ||
if (ctx.findPost(Uri.joinPath(uri, '/pages/posts', `${input2}.md`))) { | ||
window.showErrorMessage(EXIST_NOTICE_MSG) | ||
return | ||
} | ||
|
||
await workspace.fs.copy(Uri.joinPath(uri, '/scaffolds', `${input1}.md`), Uri.joinPath(uri, '/pages/posts', `${input2}.md`)) | ||
} | ||
} | ||
catch { | ||
if (ctx.findPost(Uri.joinPath(uri, '/pages/posts', `${input1}.md`))) { | ||
window.showErrorMessage(EXIST_NOTICE_MSG) | ||
return | ||
} | ||
|
||
const filePath = Uri.joinPath(uri, '/pages/posts', `${input1}.md`) | ||
await workspace.fs.writeFile(filePath, Buffer.from(newPostTemplate())) | ||
} | ||
} | ||
} | ||
|
||
refresh(): void { | ||
this._onDidChangeTreeData.fire(undefined) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relize it like this
https://github.com/YunYouJun/valaxy/blob/2d7d0d6c1cc3ffeb3317824bf6bc6bcbbaab2005/packages/valaxy/node/cli/utils/post.ts#L57