Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert video tags to links #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ You can configure the behaviour of html-to-text with the following options:
* `hideLinkHrefIfSameAsText` by default links are translated the following `<a href='link'>text</a>` => becomes => `text [link]`. If this option is set to true and `link` and `text` are the same, `[link]` will be hidden and only `text` visible.
* `ignoreHref` ignore all document links if `true`.
* `ignoreImage` ignore all document images if `true`.
* `ignoreVideo` ignore all document videos if `true`.
* `preserveNewlines` by default, any newlines `\n` in a block of text will be removed. If `true`, these newlines will not be removed.
* `decodeOptions` defines the text decoding options given to `he.decode`. For more informations see the [he](https://github.com/mathiasbynens/he) module.
* `uppercaseHeadings` by default, headings (`<h1>`, `<h2>`, etc) are uppercased. Set to `false` to leave headings as they are.
Expand Down
4 changes: 3 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var argv = optimist
.default('wordwrap', 80)
.default('ignore-href', false)
.default('ignore-image', false)
.default('ignore-video', false)
.argv;

var text = '';
Expand All @@ -25,7 +26,8 @@ process.stdin.on('end', function end() {
tables: interpretTables(argv.tables),
wordwrap: argv.wordwrap,
ignoreHref: argv['ignore-href'],
ignoreImage: argv['ignore-image']
ignoreImage: argv['ignore-image'],
ignoreVideo: argv['ignore-video']
});
process.stdout.write(text + '\n', 'utf-8');
});
Expand Down
16 changes: 15 additions & 1 deletion example/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,19 @@ <h2>Pretty printed Source Code</h2>
console.log(text);
});
</pre>

<hr />
<h2>Img tag</h2>
<img src="http://example.com/image.png" />

<hr />
<h2>Video tag (src attribute)</h2>
<video src="http://example.com/video-with-src-attribute.mp4"></video>

<hr />
<h2>Video tag (source tag)</h2>
<video>
<source src="http://example.com/video-with-source-tag.mp4">
</video>
</body>
</html>
</html>
22 changes: 22 additions & 0 deletions lib/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ function formatImage(elem, options) {
return (result);
}

function formatVideo(elem, options) {
if (options.ignoreVideo) {
return '';
}

var result = '', attribs = elem.attribs || {};

if (attribs.src) {
result += '[' + attribs.src + ']';
} else if (elem.children.length > 0) {
_.each(elem.children, function(el) {
var elAttribs = el.attribs || {};
if (el.type === 'tag' && el.name.toLowerCase() === 'source') {
result += '[' + el.attribs.src + ']';
}
});
}

return (result);
}

function formatLineBreak(elem, fn, options) {
return '\n' + fn(elem.children, options);
}
Expand Down Expand Up @@ -219,6 +240,7 @@ function formatTable(elem, fn, options) {

exports.text = formatText;
exports.image = formatImage;
exports.video = formatVideo;
exports.lineBreak = formatLineBreak;
exports.paragraph = formatParagraph;
exports.anchor = formatAnchor;
Expand Down
3 changes: 3 additions & 0 deletions lib/html-to-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ function walk(dom, options, result) {
case 'img':
result += format.image(elem, options);
break;
case 'video':
result += format.video(elem, options);
break;
case 'a':
// Inline element needs its leading space to be trimmed if `result`
// currently ends with whitespace
Expand Down