fix: single file import

This commit is contained in:
DIYgod 2023-03-13 10:57:14 +00:00
parent 96e7fa4a07
commit ec9958a537
No known key found for this signature in database
5 changed files with 38 additions and 49 deletions

View File

@ -21,7 +21,7 @@
"blog": "博客",
"Owner": "所有者",
"Transaction Hash": "交易哈希",
"IPFS Address": "IPF 地址",
"IPFS Address": "IPFS 地址",
"Creation": "创建",
"Last Update": "最后更新",
"Create Character": "创建角色",

View File

@ -101,5 +101,6 @@
"hsinyau",
"rootless",
"haydenull",
"walk2future"
"walk2future",
"cryptonerdcn"
]

View File

@ -26,6 +26,7 @@ import { renderPageContent } from "~/markdown"
import { Tooltip } from "../ui/Tooltip"
import { APP_NAME } from "~/lib/env"
import { Trans, useTranslation } from "next-i18next"
import { readFiles } from "~/lib/read-files"
export const PagesManager: React.FC<{
isPost: boolean
@ -242,45 +243,28 @@ export const PagesManager: React.FC<{
input.type = "file"
input.accept = ".md"
input.addEventListener("change", async (e: any) => {
const file = e.target?.files?.[0]
const reader = new FileReader()
reader.readAsText(file, "UTF-8")
reader.onload = (evt) => {
if (evt.target?.result) {
const pageContent = renderPageContent(evt.target.result as string)
const id = nanoid()
const key = `draft-${subdomain}-local-${id}`
const tags =
pageContent.frontMatter.tags || pageContent.frontMatter.categories
setStorage(key, {
date: +new Date(),
values: {
content: evt.target.result,
published: false,
publishedAt: (
pageContent.frontMatter.date ||
file.lastModified ||
new Date()
).toISOString(),
slug:
pageContent.frontMatter.permalink ||
file.name.split(".").slice(0, -1).join("."),
tags: tags?.join?.(", ") || tags,
title: pageContent.frontMatter.title,
},
isPost: isPost,
})
queryClient.invalidateQueries(["getPagesBySite", subdomain])
router.push(
`/dashboard/${subdomain}/editor?id=local-${id}&type=${
isPost ? "post" : "page"
}`,
)
}
}
reader.onerror = (evt) => {
toast.error("Error reading file")
const file = (await readFiles(e.target?.files))?.[0]
if (file) {
const id = nanoid()
const key = `draft-${subdomain}-local-${id}`
setStorage(key, {
date: +new Date(),
values: {
content: file.content,
published: false,
publishedAt: file.date_published,
slug: file.slug,
tags: file.tags?.join?.(", "),
title: file.title,
},
isPost: isPost,
})
queryClient.invalidateQueries(["getPagesBySite", subdomain])
router.push(
`/dashboard/${subdomain}/editor?id=local-${id}&type=${
isPost ? "post" : "page"
}`,
)
}
})
input.click()

View File

@ -77,13 +77,15 @@ export const renderPageContent = (
.use(remarkFrontmatter, ["yaml"])
.use(() => (tree) => {
const yaml = tree.children.find((node) => node.type === "yaml")
try {
env.frontMatter = jsYaml.load((yaml as any)?.value) as Record<
string,
any
>
} catch (e) {
console.log(e)
if ((yaml as any)?.value) {
try {
env.frontMatter = jsYaml.load((yaml as any)?.value) as Record<
string,
any
>
} catch (e) {
console.error(e)
}
}
})
.use(() => (tree) => {

View File

@ -161,7 +161,9 @@ const expandPage = async (page: Note, useStat?: boolean) => {
}
}
page.cover = rendered.cover
page.metadata!.frontMatter = rendered.frontMatter
if (page.metadata) {
page.metadata.frontMatter = rendered.frontMatter
}
}
page.slug = encodeURIComponent(
page.attributes?.find((a) => a.trait_type === "xlog_slug")?.value ||