Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23502

Daniel Roy Greenfeld: Converting Markdown Headers to Checklist

$
0
0

For those times when you write out a long markdown document that you want to convert to a checklist. A future modification will be to link to the original GitHub or Notion headers so that you can click on the checklist item and jump to the original content.

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


Viewing all articles
Browse latest Browse all 23502

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>