Skip to content

Commit

Permalink
refactor: constants (#105)
Browse files Browse the repository at this point in the history
Merge `constants/github.js` and `constants/path.js` into `constants/index.js`.

* refactor: create constants/index.js

* delete: constants/path.js

* delete: constants/github.js
  • Loading branch information
lumirlumir authored Oct 16, 2024
1 parent 9f89675 commit bd1de5f
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 56 deletions.
8 changes: 3 additions & 5 deletions src/app/categories/[tag]/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import Link from 'next/link';
import { Fragment, Suspense } from 'react';
import { FaBook, FaTag, FaRegCalendarPlus, FaRegCalendarXmark } from 'react-icons/fa6';

import { DOCS, EXTENSION } from '@/constants/path';
import { PATH_DOCS, EXT_MD_REGEXP } from '@/constants';
import { compareMarkdownDocument } from '@/utils/compare';
import { readMarkdownTagTree } from '@/utils/fs';
import { markdownToText } from '@/utils/markup';

const { mdRegExp } = EXTENSION;

export default async function Page({ params, searchParams }) {
const { sort = 'updated', order = 'desc' } = searchParams;

const tagTree = await readMarkdownTagTree(DOCS);
const tagTree = await readMarkdownTagTree(PATH_DOCS);

return (
<Suspense key={sort + order} fallback={<span>loading...</span>}>
Expand All @@ -22,7 +20,7 @@ export default async function Page({ params, searchParams }) {
.map(({ basename, data: { title, description, created, updated, tags } }) => (
<div key={basename}>
<h2>
<Link href={`/posts/${basename.replace(mdRegExp, '')}`}>
<Link href={`/posts/${basename.replace(EXT_MD_REGEXP, '')}`}>
{markdownToText(title)}
</Link>
</h2>
Expand Down
8 changes: 4 additions & 4 deletions src/app/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import Home from '@/components/aside/Home';

import Title from '@/components/header/Title';

import { USER } from '@/constants/github';
import { GITHUB_USER_NAME, GITHUB_USER_BIO } from '@/constants';

// TODO: import '@/styles/global.scss';

export const metadata = {
title: {
template: `%s | ${USER.name}`,
default: USER.name,
template: `%s | ${GITHUB_USER_NAME}`,
default: GITHUB_USER_NAME,
},
description: USER.bio,
description: GITHUB_USER_BIO,
};

export default function RootLayout({ children }) {
Expand Down
4 changes: 2 additions & 2 deletions src/app/page.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Article from '@/components/layouts/Article';
import { USER } from '@/constants/github';
import { GITHUB_USER_NAME } from '@/constants';

export default function Page() {
return <Article>{`Hello, It's ${USER.name}'s blog!`}</Article>;
return <Article>{`Hello, It's ${GITHUB_USER_NAME}'s blog!`}</Article>;
}
10 changes: 4 additions & 6 deletions src/app/posts/[markdown]/page.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { join } from 'path';

import { DOCS, EXTENSION } from '@/constants/path';
import { PATH_DOCS, EXT_MD, EXT_MD_REGEXP } from '@/constants';
import { readMarkdownFile, readMarkdownFilesFromDir } from '@/utils/fs';
import { markdownToText, markdownToJsx } from '@/utils/markup';

/* Custom Declaration */
const { md, mdRegExp } = EXTENSION;

function getFilePath(params) {
return join(DOCS, `${params.markdown}${md}`);
return join(PATH_DOCS, `${params.markdown}${EXT_MD}`);
}

/* Next.js Declaration */
// Control what happens when a dynamic segment is visited that was not generated with `generateStaticParams`.
export const dynamicParams = false;

export async function generateStaticParams() {
const markdownDocuments = await readMarkdownFilesFromDir(DOCS);
const markdownDocuments = await readMarkdownFilesFromDir(PATH_DOCS);
const paths = markdownDocuments.map(markdownDocument => markdownDocument.basename);

return paths.map(path => ({
markdown: path.replace(mdRegExp, ''),
markdown: path.replace(EXT_MD_REGEXP, ''),
}));
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/aside/Categories/Categories.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Link from 'next/link';

import { DOCS } from '@/constants/path';
import { PATH_DOCS } from '@/constants';
import { MARKDOWN_DOCUMENT_DATA_TAG_META } from '@/data';
import { readMarkdownTagTree } from '@/utils/fs';

/* React Declaration */
export default async function Categories() {
const tagTree = await readMarkdownTagTree(DOCS);
const tagTree = await readMarkdownTagTree(PATH_DOCS);

return (
<ul>
Expand Down
4 changes: 2 additions & 2 deletions src/components/header/Title/Title.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';

import { USER } from '@/constants/github';
import { GITHUB_USER_NAME, GITHUB_USER_HTML_URL } from '@/constants';

export default function Title() {
return <Link href={USER.htmlUrl}>{USER.name}</Link>;
return <Link href={GITHUB_USER_HTML_URL}>{GITHUB_USER_NAME}</Link>;
}
4 changes: 2 additions & 2 deletions src/components/section/Giscus/Giscus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import GiscusOriginal from '@giscus/react';

import { REPOSITORY } from '@/constants/github';
import { GITHUB_REPO_FULL_NAME } from '@/constants';

export default function Giscus() {
return (
<GiscusOriginal
repo={REPOSITORY.fullName}
repo={GITHUB_REPO_FULL_NAME}
repoId="R_kgDOLa_QgA"
category="comments"
categoryId="DIC_kwDOLa_QgM4ChivI"
Expand Down
17 changes: 0 additions & 17 deletions src/constants/github.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { join } from 'path';

// GitHub User, Ref: https://docs.github.com/en/rest/users/users
export const GITHUB_USER_LOGIN = 'lumirlumir';
export const GITHUB_USER_NAME = '루밀LuMir';
export const GITHUB_USER_BIO = 'PLAY KEYBOARD, STRIKE A CODE🎨';
export const GITHUB_USER_HTML_URL = `https://github.com/${GITHUB_USER_LOGIN}`;

// GitHub Repository, Ref: https://docs.github.com/en/rest/repos/repos
export const GITHUB_REPO_NAME = 'web-blog';
export const GITHUB_REPO_FULL_NAME = `${GITHUB_USER_LOGIN}/${GITHUB_REPO_NAME}`;

// Path
export const PATH_DOCS = join(process.cwd(), 'src', 'posts', 'docs');

// Extension
export const EXT_MD = '.md';
export const EXT_MD_REGEXP = new RegExp(`${EXT_MD}$`, 'i');
10 changes: 0 additions & 10 deletions src/constants/path.js

This file was deleted.

6 changes: 2 additions & 4 deletions src/utils/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { basename, join } from 'path';

import matter from 'gray-matter';

import { EXTENSION } from '@/constants/path';

const { md } = EXTENSION;
import { EXT_MD } from '@/constants';

/**
* @typedef {import('@/types').MarkdownDocument} MarkdownDocument
Expand Down Expand Up @@ -40,7 +38,7 @@ export async function readMarkdownFile(pathToMarkdownFile) {
export async function readMarkdownFilesFromDir(dirPath) {
const markdownDocuments = [];
const markdownFilePaths = (await fs.readdir(dirPath)).filter(filePath =>
filePath.endsWith(md),
filePath.endsWith(EXT_MD),
);

for (const markdownFilePath of markdownFilePaths) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/markup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import parse from 'html-react-parser';

import { REPOSITORY } from '@/constants/github';
import { GITHUB_REPO_FULL_NAME } from '@/constants';
import { readMarkdownFile } from './fs';

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function markdownToHtml(markdownContent) {
body: JSON.stringify({
text: markdownContent,
mode: 'gfm',
context: REPOSITORY.fullName,
context: GITHUB_REPO_FULL_NAME,
}),
});

Expand Down

0 comments on commit bd1de5f

Please sign in to comment.