Skip to content

Instantly share code, notes, and snippets.

@Track3
Created November 5, 2022 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Track3/a44e438e6618c7830c40c1399c76e1be to your computer and use it in GitHub Desktop.
Save Track3/a44e438e6618c7830c40c1399c76e1be to your computer and use it in GitHub Desktop.
A simple Deno script to remove all markdown draft files based on its frontmatter
import { extract } from "https://deno.land/std@0.162.0/encoding/front_matter/any.ts";
import { join } from "https://deno.land/std@0.162.0/path/mod.ts";
const targetDir = "content";
const fileList: string[] = [];
function findFile(directory: string) {
for (const dirEntry of Deno.readDirSync(directory)) {
const fullPath = join(directory, dirEntry.name);
if (dirEntry.isFile === true) {
const fileExt: string = dirEntry.name.substring(
dirEntry.name.lastIndexOf(".") + 1,
);
if (fileExt === "md") {
fileList.push(fullPath);
}
}
if (dirEntry.isDirectory === true) {
findFile(fullPath);
}
}
}
findFile(targetDir);
for (const item of fileList) {
const fileContent = await Deno.readTextFile(item);
const data = extract(fileContent);
if (data.attrs.draft === true) {
console.log("Remove draft: " + item);
await Deno.remove(item);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment