diff --git a/components/reader-page/DrawerTOC.tsx b/components/reader-page/DrawerTOC.tsx index 4910319..a7a67d5 100644 --- a/components/reader-page/DrawerTOC.tsx +++ b/components/reader-page/DrawerTOC.tsx @@ -1,7 +1,7 @@ import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; import { useActiveHeading } from "@/hooks/useActiveHeading"; import useDrawerTOCState from "@/stores/useDrawerTOCState"; -import type { TTOCItem } from "@/types/toc.type"; +import type { TTOCItem } from "@/types/docs.type"; import Link from "next/link"; import { MdMenuBook } from "react-icons/md"; import { twMerge } from "tailwind-merge"; diff --git a/components/reader-page/TOC.tsx b/components/reader-page/TOC.tsx index 7e2e0af..14617b2 100644 --- a/components/reader-page/TOC.tsx +++ b/components/reader-page/TOC.tsx @@ -1,5 +1,5 @@ import { useActiveHeading } from "@/hooks/useActiveHeading"; -import type { TTOCItem } from "@/types/toc.type"; +import type { TTOCItem } from "@/types/docs.type"; import Link from "next/link"; import { twMerge } from "tailwind-merge"; diff --git a/components/utils/PostList.tsx b/components/utils/PostList.tsx index 735514d..2fa0d0e 100644 --- a/components/utils/PostList.tsx +++ b/components/utils/PostList.tsx @@ -1,5 +1,5 @@ import { normalizeDate } from "@/lib/date"; -import type { TPostListItem } from "@/types/post-list"; +import type { TPostListItem } from "@/types/docs.type"; import { nanoid } from "nanoid"; import Link from "next/link"; import { Badge } from "../ui/badge"; diff --git a/lib/post-process.ts b/lib/post-process.ts index daaa62e..dab429a 100644 --- a/lib/post-process.ts +++ b/lib/post-process.ts @@ -1,25 +1,25 @@ import fs from "fs"; import path from "path"; import { PostFilesDirectory } from "@/consts/consts"; -import type { TFrontmatter } from "@/types/frontmatter.type"; -import type { TPostListItem, TPostsByTag } from "@/types/post-list"; +import type { TPostFrontmatter } from "@/types/frontmatter.type"; +import type { TPostListItem, TPostsByTag } from "@/types/docs.type"; import { serialize } from "next-mdx-remote/serialize"; import { titleCase } from "title-case"; import { isEmptyString, nullifyEmptyArray, nullifyEmptyString } from "./utils"; -async function extractFrontmatters(filepath: string): Promise { +async function extractFrontmatters(filepath: string): Promise { const source = fs.readFileSync(filepath, "utf-8"); const mdxSource = await serialize(source, { parseFrontmatter: true, mdxOptions: { format: "md" }, }); - const frontmatter = mdxSource.frontmatter as TFrontmatter; + const frontmatter = mdxSource.frontmatter as TPostFrontmatter; const normalizedTags = frontmatter.tags ?.filter((tagname) => !isEmptyString(tagname)) .map((tagname) => tagname.toUpperCase()); - const normalizedResult: TFrontmatter = { + const normalizedResult: TPostFrontmatter = { title: titleCase(frontmatter.title), subtitle: nullifyEmptyString(frontmatter.subtitle), coverURL: nullifyEmptyString(frontmatter.coverURL), diff --git a/lib/toc.ts b/lib/toc.ts index a031767..d1e6b4e 100644 --- a/lib/toc.ts +++ b/lib/toc.ts @@ -1,4 +1,4 @@ -import type { TTOCItem } from "@/types/toc.type"; +import type { TTOCItem } from "@/types/docs.type"; import { JSDOM } from "jsdom"; /** diff --git a/lib/utils.ts b/lib/utils.ts index 10a0afc..34f6a93 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,4 +1,3 @@ -import type { NonEmptyArray } from "@/types/utils.type"; import { type ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; @@ -84,11 +83,11 @@ export function isEmptyArray(value: any[] | null | undefined): boolean { * @param value - The array value to be nullified if empty. * @returns The nullified array if it is empty, otherwise returns the original array. */ -export function nullifyEmptyArray(value: T[] | null | undefined): NonEmptyArray | null { +export function nullifyEmptyArray(value: T[] | null | undefined): T[] | null { if (isEmptyArray(value)) { return null; } - return value as NonEmptyArray; + return value!; } /** diff --git a/pages/api/search/[keyword].ts b/pages/api/search/[keyword].ts index 4681dc9..fcfb647 100644 --- a/pages/api/search/[keyword].ts +++ b/pages/api/search/[keyword].ts @@ -1,6 +1,6 @@ import { SearchIndex } from "@/lib/search"; import { isEmptyString } from "@/lib/utils"; -import type { TSearchResultItem } from "@/types/search-result"; +import type { TSearchResultItem } from "@/types/docs.type"; import type { NextApiRequest, NextApiResponse } from "next"; type ResponseData = TSearchResultItem[]; diff --git a/pages/blog/[id].tsx b/pages/blog/[id].tsx index 0cf0591..843c3d5 100644 --- a/pages/blog/[id].tsx +++ b/pages/blog/[id].tsx @@ -13,9 +13,8 @@ import { Config } from "@/data/config"; import { normalizeDate } from "@/lib/date"; import { getPostFileContent, sortedPosts } from "@/lib/post-process"; import { makeTOCTree } from "@/lib/toc"; -import type { TFrontmatter } from "@/types/frontmatter.type"; -import type { TPostListItem } from "@/types/post-list"; -import type { TTOCItem } from "@/types/toc.type"; +import type { TPostFrontmatter } from "@/types/frontmatter.type"; +import type { TPostListItem, TTOCItem } from "@/types/docs.type"; import { nanoid } from "nanoid"; import type { GetStaticPaths, GetStaticProps } from "next"; import { MDXRemote, type MDXRemoteSerializeResult } from "next-mdx-remote"; @@ -36,7 +35,7 @@ import { titleCase } from "title-case"; type ReaderPageProps = { compiledSource: MDXRemoteSerializeResult; tocList: TTOCItem[]; - frontMatter: TFrontmatter; + frontMatter: TPostFrontmatter; postId: string; nextPostListItem: TPostListItem | null; prevPostListItem: TPostListItem | null; @@ -205,7 +204,7 @@ export const getStaticProps: GetStaticProps = async (context) = const postIndexInAllPosts = sortedPosts.allPostList.findIndex((item) => item.id === postId); - const frontMatter: TFrontmatter = sortedPosts.allPostList[postIndexInAllPosts].frontMatter; + const frontMatter: TPostFrontmatter = sortedPosts.allPostList[postIndexInAllPosts].frontMatter; const nextPostListItem = postIndexInAllPosts !== sortedPosts.allPostList.length - 1 diff --git a/pages/index.tsx b/pages/index.tsx index 29cc2e9..9de5ead 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -10,7 +10,7 @@ import { LatestPostCountInHomePage } from "@/consts/consts"; import { Config } from "@/data/config"; import { sortedPosts } from "@/lib/post-process"; import { generateRSSFeed } from "@/lib/rss"; -import type { TPostListItem } from "@/types/post-list"; +import type { TPostListItem } from "@/types/docs.type"; import type { GetStaticProps } from "next"; import Link from "next/link"; import { LuPenTool } from "react-icons/lu"; diff --git a/pages/posts/[[...slug]].tsx b/pages/posts/[[...slug]].tsx index 9cad524..4219053 100644 --- a/pages/posts/[[...slug]].tsx +++ b/pages/posts/[[...slug]].tsx @@ -11,7 +11,7 @@ import { PostCountPerPagination } from "@/consts/consts"; import { Config } from "@/data/config"; import { sortedPosts } from "@/lib/post-process"; import { isEmptyArray, paginateArray } from "@/lib/utils"; -import type { TPostListItem } from "@/types/post-list"; +import type { TPostListItem } from "@/types/docs.type"; import { nanoid } from "nanoid"; import type { GetStaticPaths, GetStaticProps } from "next"; import Link from "next/link"; diff --git a/pages/search.tsx b/pages/search.tsx index 469fc98..6a1b089 100644 --- a/pages/search.tsx +++ b/pages/search.tsx @@ -8,7 +8,7 @@ import { NavBar } from "@/components/utils/NavBar"; import { SEO } from "@/components/utils/SEO"; import { Config } from "@/data/config"; import { isEmptyString } from "@/lib/utils"; -import type { TSearchResultItem } from "@/types/search-result"; +import type { TSearchResultItem } from "@/types/docs.type"; import axios from "axios"; import { isArray } from "lodash"; import { nanoid } from "nanoid"; diff --git a/pages/tags/[...slug].tsx b/pages/tags/[...slug].tsx index 81f9da0..09c8082 100644 --- a/pages/tags/[...slug].tsx +++ b/pages/tags/[...slug].tsx @@ -10,7 +10,7 @@ import { PostCountPerPagination } from "@/consts/consts"; import { Config } from "@/data/config"; import { sortedPosts } from "@/lib/post-process"; import { paginateArray } from "@/lib/utils"; -import type { TPostListItem } from "@/types/post-list"; +import type { TPostListItem } from "@/types/docs.type"; import type { GetStaticPaths, GetStaticProps } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; diff --git a/scripts/newpost.ts b/scripts/newpost.ts index 2ece3ff..9de023c 100644 --- a/scripts/newpost.ts +++ b/scripts/newpost.ts @@ -1,6 +1,7 @@ import { PostFilesDirectory } from "@/consts/consts"; import { getCurrentTime } from "@/lib/date"; import { stringifyFrontmatter } from "@/lib/frontmatter"; +import type { TPostFrontmatter } from "@/types/frontmatter.type"; import { type ChildProcessWithoutNullStreams, type SpawnSyncReturns, spawn, spawnSync } from "child_process"; import colors from "colors"; import fs from "fs"; @@ -103,7 +104,7 @@ inquirer.prompt(questions).then((answers) => { const time = `${year}-${month}-${day}`; // Stringify the frontmatters - const stringifiedFrontmatter = stringifyFrontmatter({ + const frontmatter: TPostFrontmatter = { title: title, subtitle: subtitle, summary: "", @@ -114,7 +115,8 @@ inquirer.prompt(questions).then((answers) => { noPrompt: noPrompt, allowShare: allowShare, closed: false, - }); + }; + const stringifiedFrontmatter = stringifyFrontmatter(frontmatter); // Output the new post file const postFileName = `${year}-${month}-${day}-${_.kebabCase(answers.inputTitle)}.md`; diff --git a/types/docs.type.ts b/types/docs.type.ts new file mode 100644 index 0000000..d636bf4 --- /dev/null +++ b/types/docs.type.ts @@ -0,0 +1,23 @@ +import type { TPostFrontmatter } from "./frontmatter.type"; + +export type TPostListItem = { + id: string; + frontMatter: TPostFrontmatter; +}; + +export type TPostsByTag = { + [tagName: string]: TPostListItem[]; +}; + +export type TSearchResultItem = { + id: string; + title: string; + summary: string | null; + tags: string[] | null; +}; + +export type TTOCItem = { + level: number; + title: string; + anchorId: string; +}; diff --git a/types/frontmatter.type.ts b/types/frontmatter.type.ts index 690df3d..308f5fa 100644 --- a/types/frontmatter.type.ts +++ b/types/frontmatter.type.ts @@ -1,9 +1,7 @@ -import type { NonEmptyArray } from "./utils.type"; - -export type TFrontmatter = { +export type TPostFrontmatter = { title: string; time: string; - tags: NonEmptyArray | null; + tags: string[] | null; subtitle: string | null; summary: string | null; coverURL: string | null; diff --git a/types/post-list.ts b/types/post-list.ts deleted file mode 100644 index a774aa8..0000000 --- a/types/post-list.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { TFrontmatter } from "./frontmatter.type"; - -export type TPostListItem = { - id: string; - frontMatter: TFrontmatter; -}; - -export type TPostsByTag = { - [tagName: string]: TPostListItem[]; -}; diff --git a/types/search-result.ts b/types/search-result.ts deleted file mode 100644 index e56e1e3..0000000 --- a/types/search-result.ts +++ /dev/null @@ -1,6 +0,0 @@ -export type TSearchResultItem = { - id: string; - title: string; - summary: string | null; - tags: string[] | null; -}; diff --git a/types/toc.type.ts b/types/toc.type.ts deleted file mode 100644 index 6eb153e..0000000 --- a/types/toc.type.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type TTOCItem = { - level: number; - title: string; - anchorId: string; -}; diff --git a/types/utils.type.ts b/types/utils.type.ts deleted file mode 100644 index 5ae448c..0000000 --- a/types/utils.type.ts +++ /dev/null @@ -1 +0,0 @@ -export type NonEmptyArray = [T, ...T[]];