Closed ccozens closed 1 year ago
Current progress attached:
const fs = require("fs");
const path = require("path");
const matter = require("gray-matter");
const moment = require("moment");
// Define the input and output directories
const inputDir = "./input";
const outputDir = "./output";
// Read the input directory and process each file
fs.readdir(inputDir, (err, files) => {
if (err) {
console.error(err);
return;
}
files.forEach((file) => {
// Read the input file
const inputPath = path.join(inputDir, file);
const inputContent = fs.readFileSync(inputPath, "utf8");
// Parse the input file using gray-matter
const { content, data } = matter(inputContent);
// Extract the title from the first heading, first looking for '# Title' and then '#Title'
let title = data.title;
const titleMatch =
content.match(/^#\s+(.*)$/m) ?? content.match(/^#(.*)$/m);
if (titleMatch) {
const heading = titleMatch[1];
// If the heading is a link, then extract the text only and set as title
if (heading.startsWith("[") && heading.endsWith(")")) {
title = heading.substring(1, heading.indexOf("]"));
} else {
// if heading wasnt a link, then set the heading as title
title = heading;
}
// update heading style to be h2
content.replace(/^#\s+(.*)$/m, "## $1");
}
// Set the date to the last modified time of the file
const stats = fs.statSync(inputPath);
const date = moment(stats.mtime).format("YYYY-MM-DD");
// Set the description and tags
const description = "description";
const tags = ["todo"];
// Generate the output content with front matter
const frontMatterData = { title, date, description, tags };
const outputContent = data
? matter.stringify(content, frontMatterData, { orig: data.orig })
: matter.stringify(content, frontMatterData);
// Write the output file
const outputPath = path.join(outputDir, file);
fs.writeFileSync(outputPath, outputContent);
});
});
update-markdown requires gray-matter and moment installed, and:
Would be good to amend to take in args, eg so can set custom tags and descriptions.
Also input dir? Currently defaults to ./input
and output ./output
I'm keeping notes in md files while making the blog. At some point, I'll want to include them as posts, so I'll need front matter. Rather than try and work out what format that should be, I'll batch update them to create / standardise it at some point.
Currently loosely thinking:
It needs to:
format frontmatter:
title: description: date: yyyy-mm-dd tags: (tabspace)- tag1 (tabspace)- tag2