Created
February 19, 2017 14:41
-
-
Save jaketrent/87d0695ab865d4a739be59434261bbd0 to your computer and use it in GitHub Desktop.
Convert database copy data to markdown
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
const fs = require('fs') | |
// input format: | |
// 25 The Innovator’s Dilemma The simultaneous need and danger to reinvent oneself and one's business is the dilemma. Do we disrupt ourselves or do we double-down on core competencies? A dilemma indeed! http://i.imgur.com/exW4j8F.jpg 2014-08-20 2014-11-28 19:05:28.153598 2014-11-28 19:05:28.153598 Craig Christensen http://www.amazon.com/gp/product/B00E257S86/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=B00E257S86&linkCode=as2&tag=jaktre-20&linkId=5CDH2BHJK7TZT52R | |
const separateColumns = line => line.split('\t') | |
const nameColumns = line => ({ | |
id: line[0], | |
title: line[1], | |
description: line[2], | |
coverUrl: line[3], | |
completionDate: line[4], | |
reviewUrl: line[5], | |
createdAt: line[6], | |
updatedAt: line[7], | |
author: line[8], | |
affiliateUrl: line[9] | |
}) | |
const nonEmptyColumns = line => line.id !== '' | |
const deriveColumns = line => Object.assign({}, line, { | |
descriptionSummary: line.description.substr(0, 150), | |
fileName: __dirname + '/books/' + line.title.split(' ').join('-').toLowerCase() + '.md', | |
reviewUrl: line.reviewUrl.replace('http', 'https') | |
}) | |
const deriveFrontMatter = line => Object.assign({}, line, { | |
fileContents: `--- | |
affiliateUrl: "${line.affiliateUrl}" | |
author: "${line.author}" | |
date: "${line.completionDate}" | |
categories: | |
- "Book" | |
comments: true | |
description: "${line.descriptionSummary}" | |
draft: false | |
keywords: "" | |
layout: "book" | |
image: "${line.coverUrl}" | |
reviewUrl: "${line.reviewUrl}" | |
title: "${line.title}" | |
--- | |
${line.description} | |
`}) | |
const writeFiles = line => fs.writeFileSync(line.fileName, line.fileContents) | |
const file = fs.readFileSync(__dirname + '/latest.sql', 'utf8') | |
const lines = file | |
.split('\n') | |
.map(separateColumns) | |
.map(nameColumns) | |
.filter(nonEmptyColumns) | |
.map(deriveColumns) | |
.map(deriveFrontMatter) | |
.forEach(writeFiles) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment