Skip to content

Commit

Permalink
add basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Kakkar committed Nov 3, 2018
1 parent f1cad8f commit ae02949
Show file tree
Hide file tree
Showing 5 changed files with 2,344 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
"extends": "airbnb-base",
"rules": {
"indent": ["error", 4]
}
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
44 changes: 44 additions & 0 deletions index.js
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,
};
Loading

0 comments on commit ae02949

Please sign in to comment.