@ifvictr/

Ghost to Markdown w/ frontmatter

Nodejs

No description

fork
loading
Files
  • index.js
  • posts
  • package-lock.json
  • posts.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const fs = require('fs')
const escapeStr = require('js-string-escape')
const strftime = require('strftime')

fs.readFile('./posts.json', 'utf8', (err, data) => {
    const posts = JSON.parse(data)
    posts.map(post => {
        console.log(post)
        const timestamp = strftime('%Y%m%d%H%M%S', new Date(post.published_at)) // Not using created_at because it indicates when post was added to DB, not when the post was made by the user
        const filename = `./posts/${timestamp}-${post.slug}.md`
        console.log(post)
        fs.writeFile(filename, buildContents(post), { flag: 'wx' }, err => {
            if (err) {
                console.log(err)
                return
            }
            console.log('success')
        })
    })
})

const buildContents = post => `---
name: "${escapeStr(post.title)}"
slug: ${post.slug}
featured_image_url: ${post.feature_image || 'null'}
created_at: "${post.created_at}"
published_at: ${'"' + post.published_at + '"' || 'null'}
featured_at: null
tags: []
---

${JSON.parse(post.mobiledoc).cards[0][1].markdown}`
node v10.16.0