Created
July 11, 2021 12:56
-
-
Save agatan/dd306e967c953739f41718ea44638c16 to your computer and use it in GitHub Desktop.
はてなブログから export した MT ファイルを Markdown に変換する Deno スクリプト
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 * 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