Created
November 5, 2022 07:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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