Skip to content

Commit

Permalink
Dependency on fs module removed (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte Legenhausen committed Mar 26, 2019
1 parent b642ec7 commit fc487c9
Show file tree
Hide file tree
Showing 7 changed files with 2,291 additions and 766 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ after_success:

language: node_js
node_js:
- "4"
- "6"
- "8"
- "10"
37 changes: 37 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
# Changelog

## Version 5.0.0

### BREAKING CHANGES

#### fromFile removed

The function `fromFile` is removed. It was the main reason `html-to-text` could not be used in the browser [#164](https://github.com/werk85/node-html-to-text/pull/164).

You can get the `fromFile` functionality back by using the following code

```js
const fs = require('fs');
const { fromString } = require('html-to-text');

// Callback version
const fromFile = (file, options, callback) => {
if (!callback) {
callback = options;
options = {};
}
fs.readFile(file, 'utf8', (err, str) => {
if (err) return callback(err);
callback(null, fromString(str, options));
});
};

// Promise version
const fromFile = (file, option) => fs.promises.readFile(file, 'utf8').then(html => fromString(html, options));

// Sync version
const fromFileSync = (file, options) => fromString(fs.readFileSync(file, 'utf8'), options);
```

#### Supported NodeJS Versions
Node versions < 6 are no longer supported.


## Version 4.0.0

* Support dropped for node version < 4.
Expand Down
22 changes: 4 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,14 @@ npm install html-to-text -g
```

## Usage
You can read from a file via:

```javascript
var htmlToText = require('html-to-text');

htmlToText.fromFile(path.join(__dirname, 'test.html'), {
tables: ['#invoice', '.address']
}, (err, text) => {
if (err) return console.error(err);
console.log(text);
});
```

or directly from a string:

```javascript
var htmlToText = require('html-to-text');
```js
const htmlToText = require('html-to-text');

var text = htmlToText.fromString('<h1>Hello World</h1>', {
const text = htmlToText.fromString('<h1>Hello World</h1>', {
wordwrap: 130
});
console.log(text);
console.log(text); // Hello World
```

### Options:
Expand Down
14 changes: 0 additions & 14 deletions lib/html-to-text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
var fs = require('fs');
var util = require('util');

var includes = require('lodash/includes');
var trimEnd = require('lodash/trimEnd');
var htmlparser = require('htmlparser2');
Expand Down Expand Up @@ -186,17 +183,6 @@ function walk(dom, options, result) {
return result;
}

exports.fromFile = function(file, options, callback) {
if (!callback) {
callback = options;
options = {};
}
fs.readFile(file, 'utf8', function (err, str) {
if (err) return callback(err);
return callback(null, htmlToText(str, options));
});
};

exports.fromString = function(str, options) {
return htmlToText(str, options || {});
};

0 comments on commit fc487c9

Please sign in to comment.