-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Neil Kakkar
committed
Nov 3, 2018
1 parent
f1cad8f
commit ae02949
Showing
5 changed files
with
2,344 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
"extends": "airbnb-base", | ||
"rules": { | ||
"indent": ["error", 4] | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const request = require('request'); | ||
const TurndownService = require('turndown'); | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const cheerio = require('cheerio'); | ||
|
||
const asyncGetRequest = util.promisify(request.get).bind(request); | ||
const asyncWriteFile = util.promisify(fs.writeFile).bind(fs); | ||
|
||
const turndownService = new TurndownService(); | ||
|
||
async function convert(url) { | ||
try { | ||
const resp = await asyncGetRequest(url); | ||
if (resp.statusCode !== 200) { | ||
console.log('Error while fetching URL'); | ||
return resp.statusMessage; | ||
} | ||
const $ = cheerio.load(resp.body); | ||
const html = $('.postArticle-content').html(); | ||
const markdown = turndownService.turndown(html); | ||
const time = $('time').attr('datetime').split('T')[0]; | ||
const heading = $('h1', '.section-inner.sectionLayout--insetColumn') | ||
.html(); | ||
return Promise.resolve({ markdown, heading, time }); | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
} | ||
|
||
async function generateFile(data) { | ||
try { | ||
const fileName = `${data.time}-${data.heading}.md`; | ||
await asyncWriteFile(fileName, data.markdown); | ||
console.log(`Success - Written file ${fileName}`); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
|
||
module.exports = { | ||
convert, | ||
generateFile, | ||
}; |
Oops, something went wrong.