[fix] reorganize the typing files

This commit is contained in:
PrinOrange
2024-09-26 16:48:47 +08:00
parent 7e7b86c1e5
commit f00a79fcf3
19 changed files with 49 additions and 50 deletions

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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<TFrontmatter> {
async function extractFrontmatters(filepath: string): Promise<TPostFrontmatter> {
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),

View File

@@ -1,4 +1,4 @@
import type { TTOCItem } from "@/types/toc.type";
import type { TTOCItem } from "@/types/docs.type";
import { JSDOM } from "jsdom";
/**

View File

@@ -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<T>(value: T[] | null | undefined): NonEmptyArray<T> | null {
export function nullifyEmptyArray<T>(value: T[] | null | undefined): T[] | null {
if (isEmptyArray(value)) {
return null;
}
return value as NonEmptyArray<T>;
return value!;
}
/**

View File

@@ -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[];

View File

@@ -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<ReaderPageProps> = 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

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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<TAnswer>(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<TAnswer>(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`;

23
types/docs.type.ts Normal file
View File

@@ -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;
};

View File

@@ -1,9 +1,7 @@
import type { NonEmptyArray } from "./utils.type";
export type TFrontmatter = {
export type TPostFrontmatter = {
title: string;
time: string;
tags: NonEmptyArray<string> | null;
tags: string[] | null;
subtitle: string | null;
summary: string | null;
coverURL: string | null;

View File

@@ -1,10 +0,0 @@
import type { TFrontmatter } from "./frontmatter.type";
export type TPostListItem = {
id: string;
frontMatter: TFrontmatter;
};
export type TPostsByTag = {
[tagName: string]: TPostListItem[];
};

View File

@@ -1,6 +0,0 @@
export type TSearchResultItem = {
id: string;
title: string;
summary: string | null;
tags: string[] | null;
};

View File

@@ -1,5 +0,0 @@
export type TTOCItem = {
level: number;
title: string;
anchorId: string;
};

View File

@@ -1 +0,0 @@
export type NonEmptyArray<T> = [T, ...T[]];