html-to-text
Advanced tools
Comparing version 0.1.0 to 1.0.0
@@ -10,11 +10,25 @@ var _ = require('underscore'); | ||
return helper.wordwrap(elem.needsSpace ? ' ' + text : text, options.wordwrap); | ||
}; | ||
} | ||
function formatImage(elem, options) { | ||
var result = ''; | ||
if (elem.attribs.alt) { | ||
result += elem.attribs.alt; | ||
} | ||
if (elem.attribs.alt && elem.attribs.src) { | ||
result += ' '; | ||
} | ||
if (elem.attribs.src) { | ||
result += '[' + elem.attribs.src + ']'; | ||
} | ||
return (result); | ||
} | ||
function formatLineBreak(elem, fn, options) { | ||
return '\n' + fn(elem.children, options); | ||
}; | ||
} | ||
function formatParagraph(elem, fn, options) { | ||
return fn(elem.children, options) + '\n\n'; | ||
}; | ||
} | ||
@@ -34,2 +48,5 @@ function formatHeading(elem, fn, options) { | ||
var result = _s.strip(fn(elem.children || [], options)); | ||
if (!result) { | ||
result = ''; | ||
} | ||
// Get the href, if present | ||
@@ -39,11 +56,11 @@ if (elem.attribs && elem.attribs.href) { | ||
} | ||
if (result && href) { | ||
if (href) { | ||
result += ' [' + href + ']'; | ||
} | ||
return formatText({ raw: result || href, needsSpace: elem.needsSpace }, options); | ||
}; | ||
} | ||
function formatHorizontalLine(elem, fn, options) { | ||
return _s.repeat('-', options.wordwrap) + '\n\n'; | ||
}; | ||
} | ||
@@ -60,3 +77,3 @@ function formatListItem(prefix, elem, fn, options) { | ||
return prefix + text + '\n'; | ||
}; | ||
} | ||
@@ -69,17 +86,20 @@ function formatUnorderedList(elem, fn, options) { | ||
return result + '\n'; | ||
}; | ||
} | ||
function formatOrderedList(elem, fn, options) { | ||
var result = ''; | ||
// Calculate the maximum length to i. | ||
var maxLength = elem.children.length.toString().length; | ||
_.each(elem.children, function(elem, i) { | ||
var index = i + 1; | ||
// Calculate the needed spacing for nice indentation. | ||
var spacing = maxLength - index.toString().length; | ||
var prefix = ' ' + index + '. ' + _s.repeat(' ', spacing); | ||
result += formatListItem(prefix, elem, fn, options); | ||
}); | ||
// Make sure there are list items present | ||
if (elem.children && elem.children.length) { | ||
// Calculate the maximum length to i. | ||
var maxLength = elem.children.length.toString().length; | ||
_.each(elem.children, function(elem, i) { | ||
var index = i + 1; | ||
// Calculate the needed spacing for nice indentation. | ||
var spacing = maxLength - index.toString().length; | ||
var prefix = ' ' + index + '. ' + _s.repeat(' ', spacing); | ||
result += formatListItem(prefix, elem, fn, options); | ||
}); | ||
} | ||
return result + '\n'; | ||
}; | ||
} | ||
@@ -111,3 +131,3 @@ function tableToString(table) { | ||
return text + '\n'; | ||
}; | ||
} | ||
@@ -148,5 +168,6 @@ function formatTable(elem, fn, options) { | ||
return tableToString(table); | ||
}; | ||
} | ||
exports.text = formatText; | ||
exports.image = formatImage; | ||
exports.lineBreak = formatLineBreak; | ||
@@ -153,0 +174,0 @@ exports.paragraph = formatParagraph; |
@@ -81,2 +81,5 @@ var fs = require('fs'); | ||
switch(elem.name.toLowerCase()) { | ||
case 'img': | ||
result += format.image(elem, options); | ||
break; | ||
case 'a': | ||
@@ -83,0 +86,0 @@ // Inline element needs a leading space if `result` currently |
{ | ||
"name": "html-to-text", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Advanced html to plain text converter", | ||
@@ -40,4 +40,4 @@ "main": "index.js", | ||
], | ||
"engines": { | ||
"node": "~0.8.0" | ||
"engines": { | ||
"node": ">= 0.8.0 <0.12" | ||
}, | ||
@@ -44,0 +44,0 @@ "bin": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
30643
402
0