Skip to content

Commit

Permalink
Switched debugging output to use the 'debug' package instead of manua…
Browse files Browse the repository at this point in the history
…l logging.
  • Loading branch information
m4bwav committed May 15, 2016
1 parent af75848 commit fdf6404
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: node_js

node_js:
- node

- 'stable'

after_success: npm run coverage

sudo: false
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Promise = require('bluebird');
var he = require('he');
var debug = require('debug')('index');
var filterValidUrlsAndLookupTitles = require('./lib/filter-valid-urls-and-lookup-titles.js');
var parseUrlsFromMarkdownAndFilter = require('./lib/parse-urls-from-markdown-and-filter.js');
var replaceParsedPlainLinksWithTitles = require('./lib/replace-parsed-plain-links-with-titles.js');
Expand All @@ -25,9 +26,8 @@ exports.replacePlainLinks = function (markdown, callback, options) {

Promise.all(lookupPromises)
.then(function (res) {
if (options.d && options.d.indexOf && options.d.indexOf('promisecomplete') !== -1) {
console.log(res);
}
debug(res);

replaceParsedPlainLinksWithTitles(res, decodedMarkdown, callback, options);
}
);
Expand Down
17 changes: 4 additions & 13 deletions lib/filter-valid-urls-and-lookup-titles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ var isUrl = require('is-url');
var getTitleAtUrl = require('get-title-at-url');
var iterate = require('array-iterate');
var parseDomain = require('parse-domain');
var debug = require('debug')('filter-valid-urls-and-lookup-titles');

function generateTitleMarkdown(url, title, source) {
return '"[' + title + '](' + url + ')", *' + source + '*';
}

function geneneratePromiseForTitleLookup(url, options) {
if (options.d && options.d.indexOf && options.d.indexOf('urlresponse') !== -1) {
console.log('Creating promise to find url: ' + url);
}
function geneneratePromiseForTitleLookup(url) {
debug('Creating promise to find url: ' + url);

return new Promise(function (resolve) {
getTitleAtUrl(url, function (title) {
if (options.d && options.d.indexOf && options.d.indexOf('urlresponse') !== -1) {
console.log('title: ' + title + ' found for url: ' + url);
}
debug('title: ' + title + ' found for url: ' + url);

if (!title) {
resolve();
Expand All @@ -43,12 +40,6 @@ module.exports = function (urls, markdown, options) {
iterate(urls, function (currentUrl) {
var urlIndexTracker = 0;

if (options.d) {
var nextIndexOf = loweredMarkdown.indexOf(currentUrl, urlIndexTracker);
if (nextIndexOf === -1) {
console.log('\nCould not find url: ' + currentUrl + ' that was previously found in the markdown during parsing, urlIndexTracker: ' + urlIndexTracker);
}
}
while (loweredMarkdown.indexOf(currentUrl, urlIndexTracker) !== -1) {
var currentUrlIndex = urlIndexTracker;
var currentUrlStart = loweredMarkdown.indexOf(currentUrl, currentUrlIndex);
Expand Down
21 changes: 7 additions & 14 deletions lib/parse-urls-from-markdown-and-filter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
var getUrls = require('get-urls');
var iterate = require('array-iterate');
var debug = require('debug')('parse-urls-from-markdown-and-filter');

module.exports = function (markdown, options) {
module.exports = function (markdown) {
var urls = getUrls(markdown, {stripWWW: false, stripFragment: false, normalizeProtocol: false, removeQueryParameters: []});
var outputResults = [];

if (options.d && options.d.indexOf && options.d.indexOf('urlread') !== -1) {
console.log('getUrls output:');
console.log(urls);
}
debug('getUrls output:');
debug(urls);

iterate(urls, function (currentUrl) {
if (!currentUrl) {
Expand All @@ -17,21 +16,15 @@ module.exports = function (markdown, options) {

if (currentUrl.endsWith(')') && currentUrl.indexOf('(') === -1) {
currentUrl = currentUrl.substring(0, currentUrl.length - 1);
if (options.d && options.d.indexOf && options.d.indexOf('urlread') !== -1) {
console.log('new url: ' + currentUrl);
}
debug('new url: ' + currentUrl);
}

if (currentUrl.toLowerCase().endsWith('.jpg')) {
if (options.d && options.d.indexOf && options.d.indexOf('urlread') !== -1) {
console.log('skipping url: ' + currentUrl);
}
debug('skipping url: ' + currentUrl);
return true;
}
if (outputResults.indexOf(currentUrl) === -1) {
if (options.d && options.d.indexOf && options.d.indexOf('urlread') !== -1) {
console.log('pushing url: ' + currentUrl);
}
debug('pushing url: ' + currentUrl);
outputResults.push(currentUrl.toLowerCase());
}
});
Expand Down
32 changes: 14 additions & 18 deletions lib/replace-parsed-plain-links-with-titles.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var iterate = require('array-iterate');
var isUrl = require('is-url');
var debug = require('debug')('replace-parsed-plain-links-with-titles');
var replaceLinksInString = require('./replace-links-in-string.js');

module.exports = function (urlTitleList, markdown, callback, options) {
module.exports = function (urlTitleList, markdown, callback) {
iterate(urlTitleList, function (urlTitleMarkdownPair) {
if (!urlTitleMarkdownPair) {
return true;
Expand All @@ -16,26 +17,23 @@ module.exports = function (urlTitleList, markdown, callback, options) {
return true;
}

if (options.d && options.d.indexOf && options.d.indexOf('replacement') !== -1) {
console.log('Attempting to replace url: ' + currentUrl);
var nextIndexOf = markdown.indexOf(currentUrl, urlIndexTracker);
console.log('Next index of url: ' + currentUrl + ' is at position: ' + nextIndexOf + ' during replacement');
}
debug('Attempting to replace url: ' + currentUrl);
var nextIndexOf = markdown.indexOf(currentUrl, urlIndexTracker);
debug('Next index of url: ' + currentUrl + ' is at position: ' + nextIndexOf + ' during replacement');

while (markdown.indexOf(currentUrl, urlIndexTracker) !== -1) {
var currentUrlIndex = urlIndexTracker;
var currentUrlStart = markdown.indexOf(currentUrl, currentUrlIndex);
if (options.d && options.d.indexOf && options.d.indexOf('replacement') !== -1) {
console.log('Looking for url during replacement: ' + currentUrl + ' after position: ' + currentUrlIndex);
}
debug('Looking for url during replacement: ' + currentUrl + ' after position: ' + currentUrlIndex);

if ((currentUrlStart + currentUrl.length) < markdown.length) {
var endOfPlusOneUrl = currentUrlStart + currentUrl.length + 1;
var urlPlusOneChar = markdown.substring(currentUrlStart, endOfPlusOneUrl);

if (!urlPlusOneChar.endsWith(')') && isUrl(urlPlusOneChar)) {
urlIndexTracker = currentUrlStart + currentUrl.length;
if (options.d && options.d.indexOf && options.d.indexOf('replacement') !== -1) {
console.log('Skipping replacement of url: ' + currentUrl + ' for short version of url.');
}
debug('Skipping replacement of url: ' + currentUrl + ' for short version of url.');

continue;
}

Expand All @@ -44,9 +42,8 @@ module.exports = function (urlTitleList, markdown, callback, options) {

if (twoCharPrecendingString === '](') {
urlIndexTracker = currentUrlStart + currentUrl.length;
if (options.d && options.d.indexOf && options.d.indexOf('replacement') !== -1) {
console.log('Skipping replacement of url: ' + currentUrl + ' for ]( prefix.');
}
debug('Skipping replacement of url: ' + currentUrl + ' for ]( prefix.');

continue;
}
}
Expand All @@ -56,9 +53,8 @@ module.exports = function (urlTitleList, markdown, callback, options) {

if (precendingString === ']: ') {
urlIndexTracker = currentUrlStart + currentUrl.length;
if (options.d && options.d.indexOf && options.d.indexOf('replacement') !== -1) {
console.log('Skipping replacement of url: ' + currentUrl + ' for ]: prefix.');
}
debug('Skipping replacement of url: ' + currentUrl + ' for ]: prefix.');

continue;
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"meow": "3.7.0",
"bluebird": "3.3.5",
"parse-domain": "0.2.1",
"he": "1.1.0"
"he": "1.1.0",
"debug": "2.2.0"
},
"devDependencies": {
"ava": "*",
Expand Down

0 comments on commit fdf6404

Please sign in to comment.