diff --git a/public/locales/zh/common.json b/public/locales/zh/common.json index 46511e3a..fb3dcfe1 100644 --- a/public/locales/zh/common.json +++ b/public/locales/zh/common.json @@ -21,7 +21,7 @@ "blog": "博客", "Owner": "所有者", "Transaction Hash": "交易哈希", - "IPFS Address": "IPF 地址", + "IPFS Address": "IPFS 地址", "Creation": "创建", "Last Update": "最后更新", "Create Character": "创建角色", diff --git a/showcase.json b/showcase.json index 8a302e07..b6f77794 100644 --- a/showcase.json +++ b/showcase.json @@ -101,5 +101,6 @@ "hsinyau", "rootless", "haydenull", - "walk2future" + "walk2future", + "cryptonerdcn" ] diff --git a/src/components/dashboard/PagesManager.tsx b/src/components/dashboard/PagesManager.tsx index 7adc468f..2e780898 100644 --- a/src/components/dashboard/PagesManager.tsx +++ b/src/components/dashboard/PagesManager.tsx @@ -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() diff --git a/src/markdown/index.ts b/src/markdown/index.ts index 9051ab43..1ccff201 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -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) => { diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 55d43e01..08e793c4 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -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 ||