-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from lumirlumir/feat-category
feat: category
- Loading branch information
Showing
42 changed files
with
263 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { join } from 'path'; | ||
|
||
import Link from 'next/link'; | ||
|
||
import { DOCS, EXTENSION } from '@/constants/path'; | ||
import { readDirTree } from '@/utils/fs/dirTree'; | ||
|
||
/* Custom Declaration */ | ||
const { md, mdRegExp } = EXTENSION; | ||
|
||
function renderDirTree(dirTree, basePath = '') { | ||
return ( | ||
<ul> | ||
{dirTree.map(dirTreeNode => { | ||
const currPath = join(basePath, dirTreeNode.name); | ||
|
||
return ( | ||
<li key={currPath}> | ||
{dirTreeNode.name.endsWith(md) ? ( | ||
<Link href={`/posts/${currPath.replace(mdRegExp, '')}`}> | ||
{dirTreeNode.name.replace(mdRegExp, '')} | ||
</Link> | ||
) : ( | ||
<> | ||
<span>{dirTreeNode.name}</span> | ||
{renderDirTree(dirTreeNode.children, currPath)} | ||
</> | ||
)} | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
); | ||
} | ||
|
||
/* React Declaration */ | ||
export default async function Categories() { | ||
const dirTree = await readDirTree(DOCS); | ||
|
||
return <>{renderDirTree(dirTree)}</>; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Link from 'next/link'; | ||
|
||
import { DOCS, EXTENSION } from '@/constants/path'; | ||
import { readTagTree } from '@/utils/fs/tagTree'; | ||
import markdownToText from '@/utils/markdownToText'; | ||
|
||
const { mdRegExp } = EXTENSION; | ||
|
||
export default async function Page({ params }) { | ||
const tagTree = await readTagTree(DOCS); | ||
|
||
return tagTree[params.tag].map(({ basename, data: { title, description, tags } }) => ( | ||
<div key={basename}> | ||
<h2> | ||
<Link href={`/posts/${basename.replace(mdRegExp, '')}`}> | ||
{markdownToText(title)} | ||
</Link> | ||
</h2> | ||
<p>{markdownToText(description)}</p> | ||
<p>{tags.join(', ')}</p> | ||
<hr /> | ||
</div> | ||
)); | ||
} |
File renamed without changes.
30 changes: 13 additions & 17 deletions
30
src/app/docs/[...categories]/page.jsx → src/app/posts/[markdown]/page.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,20 @@ | ||
import { join } from 'path'; | ||
|
||
import Link from 'next/link'; | ||
|
||
import { DOCS } from '@/constants/path'; | ||
import { getDirTree } from '@/utils/dirTree'; | ||
import { readTagTree } from '@/utils/fs/tagTree'; | ||
|
||
/* React Declaration */ | ||
export default async function Categories() { | ||
const dirTree = await getDirTree(DOCS); | ||
|
||
return <>{renderDirTree(dirTree)}</>; | ||
} | ||
const tagTree = await readTagTree(DOCS); | ||
|
||
function renderDirTree(dirTree, basePath = '') { | ||
return ( | ||
<ul> | ||
{dirTree.map(dirTreeNode => { | ||
const currPath = join(basePath, dirTreeNode.name); | ||
|
||
return ( | ||
<li key={currPath}> | ||
{dirTreeNode.name.endsWith('.md') ? ( | ||
<Link href={`/docs/${currPath.replace('.md', '')}`}> | ||
{dirTreeNode.name.replace('.md', '')} | ||
</Link> | ||
) : ( | ||
<> | ||
<span>{dirTreeNode.name}</span> | ||
{renderDirTree(dirTreeNode.children, currPath)} | ||
</> | ||
)} | ||
</li> | ||
); | ||
})} | ||
{Object.keys(tagTree).map(key => ( | ||
<li key={key}> | ||
<Link href={`/categories/${key}`}>{key}</Link> | ||
<span> ({tagTree[key].length})</span> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
import { join } from 'path'; | ||
|
||
export const DOCS = join(process.cwd(), 'src', 'posts', 'docs'); | ||
|
||
export const EXTENSION = Object.freeze({ | ||
md: '.md', | ||
get mdRegExp() { | ||
return new RegExp(`${this.md}$`, 'i'); | ||
}, | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Represents a Markdown file structure with its content and data(front matter). | ||
*/ | ||
export type Markdown = { | ||
content: string; | ||
data: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
|
||
/** | ||
* Represents a node in a directory tree. | ||
*/ | ||
export type DirTreeNode = { | ||
name: string; // The name of the node. | ||
children?: DirTreeNode[]; // The array of child nodes if the node is a directory. This is a recursive structure. | ||
}; | ||
|
||
/** | ||
* Represents a node in a tag tree. | ||
*/ | ||
export type TagTreeNode = { | ||
[key: string]: { | ||
basename: string; | ||
} & Markdown; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// @ts-check | ||
import { promises as fs } from 'fs'; | ||
|
||
import matter from 'gray-matter'; | ||
|
||
/** | ||
* @typedef {import('fs').ObjectEncodingOptions} ObjectEncodingOptions | ||
*/ | ||
|
||
/** | ||
* Asynchronously reads the contents of a file. | ||
* | ||
* @async | ||
* @param {string} filePath The path to the file. | ||
* @returns {Promise<string>} The content of the file. | ||
*/ | ||
export async function readFile(filePath) { | ||
return fs.readFile(filePath, 'utf-8'); | ||
} | ||
|
||
/** | ||
* Asynchronously reads a Markdown file and returns either the content or data(front matter). | ||
* | ||
* @async | ||
* @param {string} filePath The path to the Markdown file. | ||
* @param {'content' | 'data' | 'all'} [option='all'] The type of data to return. | ||
* @returns {Promise<string | {[key: string]: any} | {content: string, data: {[key: string]: any}}>} The content or data(front matter) of the file. | ||
* @throws {TypeError} If the option is invalid. | ||
*/ | ||
export async function readFileForMarkdown(filePath, option = 'all') { | ||
const { content, data } = matter(await readFile(filePath)); | ||
|
||
switch (option) { | ||
case 'content': | ||
return content; | ||
case 'data': | ||
return data; | ||
case 'all': | ||
return { | ||
content, | ||
data, | ||
}; | ||
default: | ||
throw TypeError(); | ||
} | ||
} | ||
|
||
/** | ||
* Asynchronously reads a directory and returns a list of file paths with the specified extension. | ||
* | ||
* @async | ||
* @param {string} dirPath The path to the directory. | ||
* @param {string} extension The file extension to filter by. `extension` cannot be a RegExp. It must be a string. | ||
* @param {ObjectEncodingOptions & {withFileTypes?: false | undefined; recursive?: boolean | undefined;}} [options = { recursive: true }] Optional `readdir` options. | ||
* @returns {Promise<string[]>} An array of file paths. | ||
*/ | ||
export async function readDirByExtension( | ||
dirPath, | ||
extension, | ||
options = { recursive: true }, | ||
) { | ||
const filePaths = await fs.readdir(dirPath, options); | ||
|
||
return filePaths.filter(filePath => filePath.endsWith(extension)); | ||
} |
Oops, something went wrong.