-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devtools): add update frontmatter api
- Loading branch information
Showing
21 changed files
with
458 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
}, | ||
|
||
"cSpell.words": [ | ||
"AGUI", | ||
"algoliasearch", | ||
"beian", | ||
"codepen", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
<script lang="ts" setup> | ||
import { onMounted } from 'vue' | ||
import { initDevtoolsClient } from './utils' | ||
onMounted(() => { | ||
initDevtoolsClient() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<RouterView /> | ||
</template> |
57 changes: 54 additions & 3 deletions
57
packages/devtools/src/client/components/PageFrontmatter.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,66 @@ | ||
<script lang="ts" setup> | ||
defineProps<{ | ||
frontmatter?: object | ||
import { ref, toRaw } from 'vue' | ||
import type { PostFrontMatter } from 'valaxy' | ||
import axios from 'axios' | ||
import consola from 'consola' | ||
import { pageData } from '../composables/app' | ||
const props = defineProps<{ | ||
frontmatter: PostFrontMatter | ||
}>() | ||
const newFm = ref<PostFrontMatter>(props.frontmatter) | ||
async function saveNewFm() { | ||
// const data = await fetch('/valaxy-devtools-api/frontmatter', { | ||
// method: 'POST', | ||
// // body: JSON.stringify(toRaw(newFm.value)), | ||
// }) | ||
const res = await axios.post('/valaxy-devtools-api/frontmatter', { | ||
pageData: pageData.value, | ||
frontmatter: toRaw(newFm.value), | ||
}) | ||
if (res) | ||
consola.success('Frontmatter saved') | ||
// console.log(res) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<ul v-if="frontmatter"> | ||
<li v-for="(value, key) in frontmatter" :key="key"> | ||
<strong>{{ key }}</strong>: {{ value }} | ||
<strong>{{ key }}</strong> | ||
<span mr-1>:</span> | ||
|
||
<template v-if="key === 'tags'"> | ||
<VDTag v-for="tag in value" :key="tag"> | ||
{{ tag }} | ||
</VDTag> | ||
</template> | ||
<template v-else-if="key === 'categories'"> | ||
<VDPostCategories :categories="value" /> | ||
</template> | ||
<template v-else-if="key === 'encryptedContent'"> | ||
[Encrypted] | ||
</template> | ||
<template v-else-if="key === 'partiallyEncryptedContents'"> | ||
[Partially Encrypted] | ||
</template> | ||
<template v-else-if="key === 'title'"> | ||
<!-- eslint-disable-next-line vue/no-mutating-props --> | ||
<AGUIInput v-model="newFm.title" /> | ||
</template> | ||
<template v-else> | ||
{{ value }} | ||
</template> | ||
</li> | ||
</ul> | ||
|
||
<AGUIButton @click="saveNewFm"> | ||
Save Frontmatter | ||
</AGUIButton> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<script lang="ts" setup> | ||
import { getWindowProperty, openInEditor } from '../utils' | ||
import { frontmatter } from '../composables/app' | ||
function launchEditor() { | ||
openInEditor({ | ||
file: getWindowProperty('$pageData').path, | ||
}) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div p="2"> | ||
<div class="flex" gap="2" py-2> | ||
<button class="text-xs shadow transition hover:shadow-lg p-2" @click="launchEditor()"> | ||
<div i-vscode-icons:file-type-vscode /> | ||
</button> | ||
</div> | ||
|
||
<PageFrontmatter v-if="frontmatter" :frontmatter="frontmatter" /> | ||
</div> | ||
</template> |
26 changes: 26 additions & 0 deletions
26
packages/devtools/src/client/components/VDPostCategories.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script lang="ts" setup> | ||
import type { Post } from 'valaxy' | ||
defineProps<{ | ||
categories: Post['categories'] | ||
}>() | ||
</script> | ||
|
||
<template> | ||
<RouterLink | ||
:to="{ | ||
path: '/categories', | ||
query: { category: Array.isArray(categories) ? categories.join('/') : categories }, | ||
}" | ||
class="post-category transition text-xs hover:(text-blue-500 border-blue-500)" | ||
px-2 py-1 | ||
border rounded-full | ||
inline-flex justify-center items-center | ||
bg="hover:(blue-500 opacity-10)" | ||
> | ||
<div m="x-1" inline-flex i-ri-folder-2-line /> | ||
<span> | ||
{{ Array.isArray(categories) ? categories.join(' > ') : categories }} | ||
</span> | ||
</RouterLink> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script lang="ts" setup> | ||
</script> | ||
|
||
<template> | ||
<div class="vd-tag inline-flex shadow border rounded text-xs" p="x-2 y-1"> | ||
<slot /> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
import type { PageData, PostFrontMatter } from 'valaxy' | ||
import { ref } from 'vue' | ||
|
||
export const frontmatter = ref<object>({}) | ||
import type { Router } from 'vue-router' | ||
|
||
export const devtoolsRouter = ref<Router>() | ||
export const activePath = ref('') | ||
|
||
export const frontmatter = ref<PostFrontMatter>() | ||
export const pageData = ref<PageData>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,7 @@ html, | |
body { | ||
height: 100%; | ||
} | ||
|
||
.dark { | ||
background-color: rgba(12, 16, 21); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './init' | ||
export * from './api' | ||
export * from './get' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { nextTick, onMounted } from 'vue' | ||
import type { Router } from 'vue-router' | ||
import { activePath, devtoolsRouter, frontmatter, pageData } from '../composables/app' | ||
import { getWindowProperty } from './get' | ||
|
||
export function initDevtoolsClient() { | ||
const __VUE_DEVTOOLS_ROUTER__ = getWindowProperty('__VUE_DEVTOOLS_ROUTER__') as Router | ||
devtoolsRouter.value = __VUE_DEVTOOLS_ROUTER__ | ||
|
||
devtoolsRouter.value.beforeEach((to, _from, next) => { | ||
activePath.value = to.path | ||
next() | ||
}) | ||
|
||
// init $frontmatter and $pageData | ||
onMounted(() => { | ||
frontmatter.value = getWindowProperty('$frontmatter') | ||
pageData.value = getWindowProperty('$pageData') | ||
}) | ||
|
||
devtoolsRouter.value.afterEach(async () => { | ||
await nextTick() | ||
// get target post $frontmatter | ||
frontmatter.value = getWindowProperty('$frontmatter') | ||
pageData.value = getWindowProperty('$pageData') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { ResolvedConfig, ViteDevServer } from 'vite' | ||
import bodyParser from 'body-parser' | ||
|
||
import matter from 'gray-matter' | ||
import fs from 'fs-extra' | ||
|
||
const prefix = '/valaxy-devtools-api' | ||
|
||
export function registerApi(server: ViteDevServer, _viteConfig: ResolvedConfig) { | ||
const app = server.middlewares | ||
|
||
app.use(bodyParser.json()) | ||
|
||
app.use(`${prefix}/frontmatter`, async (req, _res) => { | ||
// update | ||
if (req.method === 'POST') { | ||
const { pageData, frontmatter: newFm } = await (req as any).body | ||
// filePath | ||
const path = pageData.path | ||
if (fs.existsSync(path)) { | ||
const rawMd = await fs.readFile(path, 'utf-8') | ||
const matterFile = matter(rawMd) | ||
|
||
// update frontmatter | ||
matterFile.data = newFm | ||
const newMd = matter.stringify(matterFile.content, matterFile.data) | ||
await fs.writeFile(path, newMd) | ||
} | ||
} | ||
|
||
// console.log('update', url, frontmatter) | ||
|
||
// console.log('frontmatter', req.url, res) | ||
}) | ||
} |
Oops, something went wrong.