Skip to content

Instantly share code, notes, and snippets.

@agatan
Created July 11, 2021 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agatan/dd306e967c953739f41718ea44638c16 to your computer and use it in GitHub Desktop.
Save agatan/dd306e967c953739f41718ea44638c16 to your computer and use it in GitHub Desktop.
はてなブログから export した MT ファイルを Markdown に変換する Deno スクリプト
import * as path from "https://deno.land/std@v0.100.0/path/mod.ts";
const content = await Deno.readTextFile(Deno.args[0]);
const outDir = Deno.args[1] || "output";
await Deno.mkdir(outDir, { recursive: true });
console.log(`Export to ${outDir}`);
const articles = content.split("AUTHOR: agtn").slice(1);
function parseArticle(
article: string,
): { title: string; date: string; content: string } {
const title = (article.match(/^TITLE: (.*)$/m) as any)[1].trim();
const date = new Date((article.match(/^DATE: (.*)$/m) as any)[1].trim());
const content = article.split("-----\nBODY:\n")[1].trim();
return { title, date: date.toISOString(), content };
}
console.log(`${articles.length} articles found.`);
for (const article of articles) {
const { title, date, content } = parseArticle(article);
const outFilePath = path.join(
outDir,
title.replaceAll("/", "_").replaceAll(" ", "_") + ".md",
);
const contentWithFrontmatter = `---
title: "${title}"
date: ${date}
tags: []
---
${content}`;
await Deno.writeTextFile(outFilePath, contentWithFrontmatter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment