Skip to content

Instantly share code, notes, and snippets.

@crucialfelix
Created April 21, 2022 08:26
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 crucialfelix/1ad2ff0a26c3b0dcf099fa24cda6894f to your computer and use it in GitHub Desktop.
Save crucialfelix/1ad2ff0a26c3b0dcf099fa24cda6894f to your computer and use it in GitHub Desktop.
#!/usr/bin/env deno run
/**
* Copy markdown posts from org folders to publishing folder
*
* Usage:
* deno run --allow-read --allow-write deno-export-markdown-posts.ts ~/org/blogging/mattermind/ ~/github/crucialfelix.github.io/content/posts
*/
import { join } from "https://deno.land/std@0.122.0/path/mod.ts";
if (Deno.args.length !== 2) {
console.error(
"Usage: deno-export-markdown-posts.ts <input-dir> <output-dir>"
);
Deno.exit(1);
}
const [inputDir, outputDir] = Deno.args;
const dirEntries = Deno.readDirSync(inputDir);
const preExistingFiles = new Set(
[...Deno.readDirSync(outputDir)].map((entry) => entry.name)
);
let numFiles = 0;
for (const dirEntry of dirEntries) {
if (dirEntry.isFile && !dirEntry.name.startsWith("_")) {
const filepath = join(inputDir, dirEntry.name);
const file = Deno.readFileSync(filepath);
const content = new TextDecoder("utf-8").decode(file);
// replace internal links [[...]]
const contentReplaced = content.replace(
/\[\[([^\]]+)\]\]/g,
(match, p1) => {
return p1;
}
);
// set all as draft
// https://dotland-fresh.deno.dev/x/frontmatter@v0.1.4
console.log("Saving", dirEntry.name);
Deno.writeFileSync(
join(outputDir, dirEntry.name),
new TextEncoder().encode(contentReplaced)
);
numFiles++;
preExistingFiles.delete(dirEntry.name);
}
}
if (preExistingFiles.size > 0) {
console.log("Removing previous files:", preExistingFiles);
for (const file of preExistingFiles) {
Deno.removeSync(join(outputDir, file));
}
}
console.log("Copied", numFiles, "files to", outputDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment