Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
],
"contributes": {
"commands": [
{
"command": "valaxy.addPost",
"category": "Valaxy",
"title": "Add a Post",
"icon": "$(add)"
},
{
"command": "valaxy.refreshPosts",
"category": "Valaxy",
Expand All @@ -57,6 +63,11 @@
],
"menus": {
"view/title": [
{
"command": "valaxy.addPost",
"when": "view =~ /valaxy-posts/",
"group": "navigation"
},
{
"command": "valaxy.refreshPosts",
"when": "view =~ /valaxy-posts/",
Expand Down
13 changes: 13 additions & 0 deletions res/md/template.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 }
4 changes: 4 additions & 0 deletions src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export class Context {
return bDate.getTime() - aDate.getTime()
})
}

findPost(uri: Uri) {
return this.posts.find(p => p.uri.fsPath === uri.fsPath)
}
}

export const ctx = new Context()
9 changes: 6 additions & 3 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Uri } from 'vscode'
import { commands, window, workspace } from 'vscode'
import { RelativePattern, Uri, commands, window, workspace } from 'vscode'
import Markdown from 'markdown-it'
import matter from 'gray-matter'
import { ctx } from './ctx'
Expand Down Expand Up @@ -29,7 +28,7 @@ export function configEditor() {

const postsProvider = new PostsProvider()

const mdWatcher = workspace.createFileSystemWatcher('**/*.md')
const mdWatcher = workspace.createFileSystemWatcher(new RelativePattern(Uri.joinPath(workspace.workspaceFolders![0].uri, 'pages/posts'), '**/*.md'))
mdWatcher.onDidCreate(async (uri) => {
await ctx.addPost(uri)
postsProvider.refresh()
Expand Down Expand Up @@ -80,6 +79,10 @@ export function configEditor() {
postsProvider.refresh()
})

commands.registerCommand('valaxy.addPost', async () => {
postsProvider.add()
})

update()
}

Expand Down
42 changes: 41 additions & 1 deletion src/view/ValaxyProvider.ts
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({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a clearer name: postName

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember to use trim()

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`))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default is post.md, I think we should use it directly, and only need one input for postName.

Copy link
Member

Choose a reason for hiding this comment

The 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)
}
Expand Down