diff --git a/README.md b/README.md index 780250c..099a355 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,31 @@ # rmr-lib +A well-tested library of code for working with internet metadata: + + const + RMR = require('rmr-lib'), + mime = RMR.mime.fromPath('script.js')), // 'text/javascript' + ext = RMR.mime.extensionFor('application/json'), // 'json' + isURL = RMR.url.isA('https://google.com'); // true + RMR.meta.retrieve('http://readmeansrun.com').then(function(data) { + /*{ + "title": "READMEANSRUN", + "apple-touch-icon": { + "mime": "image/png", + "url": "https://readmeansrun.com/apple-touch-icon.png" + }, + "og:title": "READMEANSRUN", + "og:description": "READMEANSRUN makes websites and takes pictures", + "og:image": { + "mime": "image/png", + "url": "https://readmeansrun.com/assets/img/og-image.png" + }, + "favicon": { + "mime": "image/x-icon", + "url": "https://readmeansrun.com/favicon.ico" + } + }*/ + }).catch(function(err)) { + console.log('🚫', err); + }; \ No newline at end of file diff --git a/index.js b/index.js index f5c39cf..3eb5a00 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ const /** @param markup {String} - the contents of the HTML document that will be parsed - @param baseURL {String} - (optional) the base URL that should be applied to all relative paths within the document + @param options {Object} - `baseURL` (optional) the base URL that should be applied to all relative paths within the document */ const parseMetadata = function(markup, options) { @@ -129,8 +129,6 @@ const parseMetadata = function(markup, options) { // if both icons have been retrieved, resolve if (Object.keys(addresses).length === 0) { resolve(OBJ); } - - async.map(Object.keys(addresses), function(key, callback) { request.head({ url : addresses[key], @@ -159,11 +157,32 @@ const parseMetadata = function(markup, options) { /** - @param url {String} - - @param options {Object} - + { + "title": "READMEANSRUN", + "apple-touch-icon": { + "mime": "image/png", + "url": "https://readmeansrun.com/apple-touch-icon.png" + }, + "og:title": "READMEANSRUN", + "og:description": "READMEANSRUN makes websites and takes pictures", + "og:image": { + "mime": "image/png", + "url": "https://readmeansrun.com/assets/img/og-image.png" + }, + "favicon": { + "mime": "image/x-icon", + "url": "https://readmeansrun.com/favicon.ico" + } + } + + + @param address {String} - the URL whose metadata should be retrieved + @param options {Object} - `baseURL` */ const retrieveMetadata = function(address, options) { + var ARGS = arguments; + return new Promise(function(resolve, reject) { request.head({ @@ -192,6 +211,12 @@ const retrieveMetadata = function(address, options) { return; } + options = ARGS.length == 2 ? options : { + baseURL : response.request.uri.href, + icons : true + }; + + parseMetadata(body, options ? options : { baseURL : response.request.uri.href, icons : true }).then(function(obj) { resolve(obj); }).catch(function(err) { @@ -310,7 +335,7 @@ module.exports = { }; -/* + if (require.main === module) { if (process.argv.length == 3) { @@ -323,4 +348,3 @@ if (require.main === module) { console.log('🚫 No URL provided'); } } -*/ \ No newline at end of file diff --git a/test.js b/test.js index cc3bd6a..783456d 100644 --- a/test.js +++ b/test.js @@ -25,11 +25,11 @@ describe('RMR.mime', function() { expect(RMR.mime.extensionFor('application/pdf')).to.equal('pdf'); expect(RMR.mime.extensionFor('text/html')).to.equal('html'); expect(RMR.mime.extensionFor('text/css')).to.equal('css'); - - expect(RMR.mime.extensionFor('text/javascript')).to.equal('js'); - + + expect(RMR.mime.extensionFor('text/javascript')).to.equal('js'); + expect(RMR.mime.extensionFor('application/json')).to.equal('json'); - + expect(RMR.mime.extensionFor('asdfadsf')).to.equal(null); });