Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Books | Jobs | News | Tags

Converting Markdown Headers to Checklist

For those times when you write out a long markdown document that you want to convert to a checklist.

Converting Markdown Headers to Checklist

Python:

response = []
with open("sample.md") as f:
    lines = f.readlines()
    for line in lines:
        if line.startswith("#"):
          indentation = line.count("#") - 1
          newline = f"{' ' * 2 * indentation}- [ ]{line.replace('#', '')}"
          response.append(newline)

with open("checklist.md", "w") as f:
    f.writelines(response)

JavaScript:

const fs = require("fs");

function MarkdownHeadersToChecklist(markdown) {
  const lines = markdown.split("\n");
  const headers = lines.filter((line) => line.startsWith("#"));
  let checklist = [];
  for (const header of headers) {
    const indentation = header.split("#").length - 1;
    const spacer = " ".repeat(2 * indentation);
    const newline = `${spacer}- [ ]${header.replace("#", "")}`;
    checklist.push(newline);
  }
  return checklist.join("\n");
}

const markdown = fs.readFileSync("sample.md", "utf8");

const checklist = MarkdownHeadersToChecklist(markdown);

fs.writeFileSync("checklist.md", checklist);

Converting Markdown Headers to Checklist


Tags: python nodejs javascript markdown howto
← Back to home