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 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
13 changes: 13 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 Expand Up @@ -150,11 +161,13 @@
"devDependencies": {
"@antfu/eslint-config": "^0.38.5",
"@antfu/ni": "^0.21.3",
"@types/ejs": "^3.1.2",
"@types/markdown-it": "^12.2.3",
"@types/node": "^18.16.0",
"@types/vscode": "^1.77.0",
"axios": "^1.3.6",
"bumpp": "^9.1.0",
"ejs": "^3.1.9",
"eslint": "^8.39.0",
"esno": "^0.16.3",
"gray-matter": "^4.0.3",
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions res/md/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { formatTime } from '../../src/utils'

function newPostTemplate(): string {
const now = formatTime(new Date())
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
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ export function isDarkTheme() {
// IDK, maybe dark
return true
}

export function formatTime(date: Date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()

const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

const pad = (n: number) => n.toString().padStart(2, '0')
return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
Copy link
Member

Choose a reason for hiding this comment

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

You can use dayjs to format it.

}
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 { render } from 'ejs'
import { ctx } from '../ctx'
import { newPostTemplate } from '../../res/md/template'
import { formatTime } from '../utils'
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 postName = (await window.showInputBox({
placeHolder: 'Type a filename or use a scaffold for the new post',
}))?.trim()

if (postName) {
const uri = workspace.workspaceFolders?.[0].uri
if (!uri)
return

try {
const template = (await workspace.fs.readFile(Uri.joinPath(uri, '/scaffolds', `${postName}.md`))).toString()
createPost(uri, 'post', render(template, { title: '', layout: 'post', date: formatTime(new Date()) }), true)
}
catch {
createPost(uri, postName, newPostTemplate())
}
}

async function createPost(uri: Uri, filename: string, content: string, autoSuffix = false, suffixNum = 0) {
const sufFilename = `${filename}${suffixNum > 0 ? suffixNum : ''}.md`
if (ctx.findPost(Uri.joinPath(uri, '/pages/posts', sufFilename))) {
if (autoSuffix)
createPost(uri, filename, content, true, suffixNum + 1)
else
window.showErrorMessage(EXIST_NOTICE_MSG)
return
}

const filePath = Uri.joinPath(uri, '/pages/posts', sufFilename)
await workspace.fs.writeFile(filePath, Buffer.from(content))
}
}

refresh(): void {
this._onDidChangeTreeData.fire(undefined)
}
Expand Down