Skip to content

Commit

Permalink
Update most dependencies and modernize the codebase (#23)
Browse files Browse the repository at this point in the history
* Update most dependencies

* Port away from deprecated APIs

* Update CI environment

* Fix html entities are double escaped in the excerpt

* Add tests for html entities encoding

* Fix eslint
  • Loading branch information
Aetf committed Apr 16, 2021
1 parent e8ed8f3 commit 16494e9
Show file tree
Hide file tree
Showing 8 changed files with 2,360 additions and 4,137 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
coverage/
coverage/
.nyc_output
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
node_modules/
*.log
coverage/
coverage/
.nyc_output
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ cache:
- node_modules

node_js:
- "10.15.0"
- "11.6.0"
- 12
- 14
- 15

script:
- npm run eslint
- npm run jscs
- npm run test-cover

after_script:
- npm install coveralls
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
- npm run test-cover-report
47 changes: 24 additions & 23 deletions lib/dom-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@
const domutils = require('domutils');
const CSSselect = require('css-select');

function Filter(hexo, excludes) {
this.hexo = hexo;
this.selectors = excludes.map(this._compile.bind(this));
}
class Filter {
constructor(hexo, excludes) {
this.hexo = hexo;
this.selectors = excludes.map(this._compile.bind(this));
}

Filter.prototype._compile = function(selector) {
try {
return CSSselect.compile(selector);
} catch (err) {
this.hexo.log.error('hexo-excerpt: Ignore invalid CSS selector: ' + selector);
return n => false;
_compile(selector) {
try {
return CSSselect.compile(selector);
} catch (err) {
this.hexo.log.error('hexo-excerpt: Ignore invalid CSS selector: ' + selector);
return () => false;
}
}
};

Filter.prototype.match = function(node) {
// not match any
return !this.selectors.some(s => s(node));
};
match(node) {
// not match any
return !this.selectors.some(s => s(node));
}

Filter.prototype.filter = function(nodes) {
// remove inner nodes that doesn't match
domutils.filter(n => !this.match(n), nodes).forEach(node => {
domutils.removeElement(node);
});
filter(nodes) {
// remove inner nodes that doesn't match
domutils.filter(n => !this.match(n), nodes)
.forEach(node => domutils.removeElement(node));

// only keep top level nodes that match
return nodes.filter(this.match.bind(this));
};
// only keep top level nodes that match
return nodes.filter(this.match.bind(this));
}
}

module.exports = Filter;
19 changes: 4 additions & 15 deletions lib/hexo-excerpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const htmlparser = require('htmlparser2');
const domutils = require('domutils');
const domrender = require("dom-serializer").default;
const defaults = require('lodash.defaults');

const Filter = require('./dom-filter');
Expand Down Expand Up @@ -41,18 +41,7 @@ module.exports = function(db) {
};
}

let nodes = [];

let parser = new htmlparser.Parser(new htmlparser.DomHandler((err, dom) => {
if (!err) {
nodes = dom;
}
}), {
decodeEntities: false
});

parser.write(post.content);
parser.done();
const nodes = htmlparser.parseDocument(post.content, { decodeEntities: false }).children;

// tracks how many tag nodes we found
let stopIndex = 1;
Expand All @@ -75,8 +64,8 @@ module.exports = function(db) {
// If the hideWholePostExcerpts option is set to true (the default), don't show
// excerpts for short posts (i.e. ones where the excerpt is the whole post)
if (moreNodes.length != 0 || !opts.hideWholePostExcerpts) {
post.excerpt = (excerptNodes.map(node => domutils.getOuterHTML(node))).join('');
post.more = (moreNodes.map(node => domutils.getOuterHTML(node))).join('');
post.excerpt = domrender(excerptNodes, { decodeEntities: false });
post.more = domrender(moreNodes, { decodeEntities: false });
}

return {
Expand Down
Loading

0 comments on commit 16494e9

Please sign in to comment.