From b1d15c0ef929c1a2ffd1d897b3377ba1c5cbc2ab Mon Sep 17 00:00:00 2001 From: Dan Burzo Date: Wed, 5 Aug 2020 01:05:17 +0300 Subject: [PATCH] Add option.serializer, fixes #605 (#607) --- .eslintrc.js | 2 +- Readability.js | 5 ++++- test/test-readability.js | 11 +++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7824556..9ab0905 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -202,4 +202,4 @@ module.exports = { // Only check typeof against valid results "valid-typeof": 2, }, -} +}; diff --git a/Readability.js b/Readability.js index 7a3f5c7..00f6cc2 100644 --- a/Readability.js +++ b/Readability.js @@ -50,6 +50,9 @@ function Readability(doc, options) { this._charThreshold = options.charThreshold || this.DEFAULT_CHAR_THRESHOLD; this._classesToPreserve = this.CLASSES_TO_PRESERVE.concat(options.classesToPreserve || []); this._keepClasses = !!options.keepClasses; + this._serializer = options.serializer || function(el) { + return el.innerHTML; + }; // Start with all flags set this._flags = this.FLAG_STRIP_UNLIKELYS | @@ -2057,7 +2060,7 @@ Readability.prototype = { title: this._articleTitle, byline: metadata.byline || this._articleByline, dir: this._articleDir, - content: articleContent.innerHTML, + content: this._serializer(articleContent), textContent: textContent, length: textContent.length, excerpt: metadata.excerpt, diff --git a/test/test-readability.js b/test/test-readability.js index 3860de2..b2154f4 100644 --- a/test/test-readability.js +++ b/test/test-readability.js @@ -258,6 +258,17 @@ describe("Readability API", function() { expect(parser._cleanClasses.called).eql(false); }); + it("should use custom content serializer sent as option", function() { + var dom = new JSDOM("My cat: "); + var expected_xhtml = "
My cat:
"; + var xml = new dom.window.XMLSerializer(); + var content = new Readability(dom.window.document, { + serializer: function(el) { + return xml.serializeToString(el.firstChild); + } + }).parse().content; + expect(content).eql(expected_xhtml); + }); }); });