Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node parse markdown files with frontmatter

const { readdir, readFile, writeFile } = require("fs/promises");
const matter = require("gray-matter");
const { stringify } = require("yaml");

const directory = "<YOUR-DIRECTORY>";

async function updateFrontMatter(filename) {
  const filepath = `${directory}/${filename}`;

  const { data: frontMatter, content } = matter(await readFile(filepath));

  // remove desc attribute
  if (frontMatter.desc === "") {
    delete frontMatter["desc"];
  }

  // parse created date attribute and convert it as timestamp
  if (typeof frontMatter.created === "string") {
    frontMatter.created = new Date(frontMatter.created).getTime();
  }

  const newContent = `---
${stringify(frontMatter)}---
${content}`;

  await writeFile(filepath, newContent);

  console.log(`- [x] ${filepath}`);
}

async function main() {
  const filenames = await readdir(directory);
  const markdownFilenames = filenames.filter((f) => f.endsWith(".md"));

  await Promise.all(markdownFilenames.map(updateFrontMatter));
}

main().catch(console.error);
Comment

PREVIOUS NEXT
Code Example
Javascript :: cpprestsdk json 
Javascript :: javascript filtrar array string 
Javascript :: "send data with window.location.href and get" 
Javascript :: nested json example 
Javascript :: how to merge data rn 
Javascript :: bytes to uppercase hex javascript 
Javascript :: html how to remove class with js 
Javascript :: append to a div and save the previous data after refresh page in javascript 
Javascript :: execute shell command from html button node js 
Javascript :: array of function 
Javascript :: How to create an array containing 1...N 
Javascript :: 5.1.3. Boolean Expressions&para; 
Javascript :: ajax status update switch toggle 
Javascript :: Get physical path in javascript 
Javascript :: upload image to server react next 
Javascript :: typeorm clear cache 
Javascript :: vercel route all pages to a file 
Javascript :: react hook for component size 
Javascript :: verify number of times request was made in cypress 
Javascript :: Example of Nullish coalescing assignment operator in es12 
Javascript :: highlight row javascript 
Javascript :: image opacity reduce js 
Javascript :: visable in viewport 
Javascript :: External javascript in React Native 
Javascript :: define nasty 
Javascript :: vue ignore not used error 
Javascript :: javascript return opposite boolean 
Javascript :: 24 hour datepicker 
Javascript :: refresh mathjax 
Javascript :: diable input javascript 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =