-
-
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.
refactor: organize
utils
folder (#96)
Oraganize unused functions and restructure folder structures. * refactor: move markdownToText function to markdownToJsx.js module * rename: markdownToJsx.js to markup.js * refactor: delete default export of markdownToJsx in markup.js * refactor: update comments and change the function order in markup.js * rename: move dirTree.js module to archives folder * refactor: delete unnecessay readFile function in src/utils/fs * refactor: create MarkdownDocument type and update readMarkdownFile func * refactor: readMarkdownFilesFromDir function * refactor: readMarkdownTagTree function in utils/fs * rename: move utils/fs/index.js to utils/fs.js
- Loading branch information
1 parent
e3db53c
commit bfa7f40
Showing
10 changed files
with
145 additions
and
170 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
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
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,26 +1,12 @@ | ||
/** | ||
* Represents a Markdown file structure with its content and data(front matter). | ||
* Represents a Markdown file structure with its basename, content and data(front matter). | ||
*/ | ||
export type Markdown = { | ||
export type MarkdownDocument = { | ||
basename: string; | ||
// The property obtained from the front matter | ||
content: string; | ||
// The property obtained from the front matter | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { promises as fs } from 'fs'; | ||
import { basename, join } from 'path'; | ||
|
||
import matter from 'gray-matter'; | ||
|
||
import { EXTENSION } from '@/constants/path'; | ||
|
||
const { md } = EXTENSION; | ||
|
||
/** | ||
* @typedef {import('@/types').MarkdownDocument} MarkdownDocument | ||
*/ | ||
|
||
/** | ||
* Asynchronously reads a Markdown file and returns a `MarkdownDocument` type object. | ||
* | ||
* @async | ||
* @param {string} pathToMarkdownFile Path to a Markdown file. | ||
* @returns {Promise<MarkdownDocument>} A promise that resolves to a `MarkdownDocument` type object. | ||
* @see {@link MarkdownDocument} | ||
*/ | ||
export async function readMarkdownFile(pathToMarkdownFile) { | ||
const { content, data } = matter(await fs.readFile(pathToMarkdownFile, 'utf-8')); | ||
|
||
return { | ||
basename: basename(pathToMarkdownFile), | ||
content, | ||
data, | ||
}; | ||
} | ||
|
||
/** | ||
* Asynchronously reads a directory and returns a list(array) of `MarkdownDocument` type object. | ||
* | ||
* @async | ||
* @param {string} dirPath Path to a directory containing markdown files. | ||
* @returns {Promise<MarkdownDocument[]>} A promise that resolves to a list(array) of `MarkdownDocument` type object. | ||
* @see {@link MarkdownDocument} | ||
*/ | ||
export async function readMarkdownFilesFromDir(dirPath) { | ||
const markdownDocuments = []; | ||
const markdownFilePaths = (await fs.readdir(dirPath)).filter(filePath => | ||
filePath.endsWith(md), | ||
); | ||
|
||
for (const markdownFilePath of markdownFilePaths) { | ||
const markdownDocument = await readMarkdownFile(join(dirPath, markdownFilePath)); | ||
markdownDocuments.push(markdownDocument); | ||
} | ||
|
||
return markdownDocuments; | ||
} | ||
|
||
/** | ||
* Asynchronously reads a directory and generates a tag tree from markdown files. | ||
* | ||
* @async | ||
* @param {string} dirPath Path to a directory containing markdown files. | ||
* @returns {Promise<{[key: string]: MarkdownDocument[]}>} A promise that resolves to a tag tree. | ||
* @see {@link MarkdownDocument} | ||
*/ | ||
export async function readMarkdownTagTree(dirPath) { | ||
const tagTree = {}; // Initialize an empty object to store the tag tree. | ||
const markdownDocuments = await readMarkdownFilesFromDir(dirPath); | ||
|
||
for (const markdownDocument of markdownDocuments) { | ||
const { | ||
data: { tags }, | ||
} = markdownDocument; | ||
|
||
tags.forEach(tag => { | ||
tagTree[tag] ??= []; | ||
tagTree[tag].push(markdownDocument); | ||
}); | ||
} | ||
|
||
return tagTree; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.