Last active
October 5, 2015 10:37
-
-
Save haohcraft/33fd7f0e5cfae2a3d33f to your computer and use it in GitHub Desktop.
This is a wrapper for the API at http://fuckyeahmarkdown.com/
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
/** | |
* This is a wrapper for the API at http://fuckyeahmarkdown.com/ | |
* where the article in the web will transform to a readable markdown text | |
* @author haohcraft@gmail.com | |
*/ | |
/** | |
* Module dependencies | |
*/ | |
'usr strict'; | |
var request = require('request'); | |
var mergeInto = require('react/lib/mergeInto'); | |
var _ = require('underscore'); | |
var MARKED_BASE_URL = "http://fuckyeahmarkdown.com/go/?"; | |
var OPTS_DEFAULT = { | |
u: "", // the target url | |
output: "markdown", // output type: markdown or json | |
read: 1, // 0 for non-Readability, 1 for yes | |
md: 0, // 0 for non-Markdownify, 1 for yes | |
preview: 0, // 0 for Markdowned text, 1 for Htmled | |
showframe: 0 // determines whether or not the output is encased in the HTML frame for viewing/copying | |
}; | |
var dataFromUrl; | |
var res; | |
/** | |
* Expose | |
*/ | |
module.exports = Markify; | |
function Markify (res) { | |
this.res = res; | |
}; | |
Markify.parseToMarkdown = function(url) { | |
console.log("Calling parseToMarkdown"); | |
var options = { | |
u: url | |
}; | |
parseTo(options, getData); | |
}; | |
Markify.parseToReadableHTML = function(url) { | |
console.log("Calling parseToReadableHTML"); | |
var options = { | |
u: url, | |
preview: 1 | |
}; | |
parseTo(options, getData); | |
}; | |
Markify.parseToJson = function(url) { | |
console.log("Calling parseToJson"); | |
var options = { | |
u: url, | |
output: "json" | |
}; | |
parseTo(options, getData); | |
console.log("parseToJson data: ", this.dataFromUrl); | |
return; | |
}; | |
/** | |
* [getData description] | |
* @param {[type]} url [description] | |
* @return {[type]} [description] | |
*/ | |
function getData(url) { | |
console.log("getData from: ", url); | |
request(url, function(err, resp, data) { | |
if (!err) { | |
console.log("data content: ", data); | |
var json = JSON.parse(data); | |
this.res.send(201, { | |
flag: 1, | |
msg: "love it", | |
candidateData: json | |
}); | |
} else { | |
this.res.send(201,{ | |
flag: 0, | |
msg: "", | |
candidateData: "" | |
}); | |
} | |
}.bind(this)); | |
} | |
/** | |
* [parseTo description] | |
* @param {[type]} opts [description] | |
* @param {Function} callback [description] | |
* @return {[type]} [description] | |
*/ | |
function parseTo (opts, callback) { | |
var defaultOpts = OPTS_DEFAULT; | |
var url; | |
if (opts != null) { | |
mergeInto(defaultOpts, opts); | |
} | |
url = buildUrl(defaultOpts); | |
callback(url); | |
} | |
/** | |
* Build the request url | |
* @param {[type]} opts the options | |
* @return {[type]} the newly built url | |
*/ | |
function buildUrl (opts) { | |
var url = MARKED_BASE_URL; | |
_.map(opts, function(val, key) { | |
url = url.concat("&", key, "=", val); | |
}); | |
console.log("buildUrl: ", url); | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment