ReasonJun

fs / path / gray-matter 본문

Frontend/Library

fs / path / gray-matter

ReasonJun 2023. 6. 17. 14:56
728x90

The fs module, the path module, and the gray-matter module are all popular JavaScript modules that are used for working with files and directories.

  • The fs module provides a set of functions for reading, writing, and manipulating files and directories. It is a core module in the Node.js platform, so it is always available when you are working with Node.js.
  • The path module provides a set of functions for working with file paths. It can be used to parse, normalize, and join file paths. It is also a core module in the Node.js platform.
  • The gray-matter module is a third-party module that provides a simple way to parse front matter from Markdown files. Front matter is a section of text that is typically placed at the top of a Markdown file and contains metadata about the file. The gray-matter module can be used to extract the metadata from a Markdown file and store it in a JavaScript object.

Here is an example of how to use the fs, path, and gray-matter modules to read a Markdown file and extract the metadata from it:

const fs = require("fs");
const path = require("path");
const grayMatter = require("gray-matter");

const filePath = path.join(__dirname, "my-file.md");
const fileContents = fs.readFileSync(filePath, "utf8");
const matterResult = grayMatter(fileContents);

const metadata = matterResult.data;
console.log(metadata.title);
console.log(metadata.date);

This code will read the my-file.md file and extract the title and date metadata from it. The output of the code will be the title and date of the file.

728x90

'Frontend > Library' 카테고리의 다른 글

Date-fns : (a comprehensive set of date and time utilities)  (0) 2023.06.19
remark / remark-html  (0) 2023.06.17
Styled Components  (0) 2023.06.13
Axios  (0) 2023.06.13
{JSON} Placeholder  (0) 2023.06.13
Comments