Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 1 min read

I got tired of copy pasting Markdown, so I made a tiny build script

Like a lot of folks, I write my drafts in Markdown first. It's fast, fits Git perfectly, and lets you focus on typing without fiddling with custom CMS editors. But actually pushing those drafts live usually turns into

Like a lot of folks, I write my drafts in Markdown first. It's fast, fits Git perfectly, and lets you focus on typing without fiddling with custom CMS editors.

But actually pushing those drafts live usually turns into a headache. Copy-pasting raw files into random Web editors almost always mangles headings, screws up code snippets, or gets the metadata wrong.

So, instead of dealing with manual formatting, I threw together a quick Node.js script. It grabs a raw Markdown file, extracts the frontmatter, and spits out clean, ready-to-go JSON and HTML. No heavy frameworks, no bloated CLI tools.

Here’s the whole 30-line setup:


javascript
import fs from "node:fs";
import path from "node:path";
import matter from "gray-matter";
import { marked } from "marked";

function buildPost(filePath, outDir) {
  const raw = fs.readFileSync(filePath, "utf8");
  const { data, content } = matter(raw);

  // Turn markdown into html
  const htmlBody = marked.parse(content);

  // Quick reading time estimate
  const words = content.trim().split(/\s+/).length;
  const time = Math.max(1, Math.ceil(words / 200));

  const output = {
    title: data.title || "Draft",
    tags: data.tags || [],
    readTime: `${time}m`,
    html: htmlBody,
  };

  fs.mkdirSync(outDir, { recursive: true });
  const slug = path.basename(filePath, ".md");
  fs.writeFileSync(
    path.join(outDir, `${slug}.json`),
    JSON.stringify(output, null, 2)
  );

  console.log(`Success! Built: ${output.title} (${output.readTime})`);
}

buildPost("./posts/my-post.md", "./dist");
πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.