hexo-filter-auto-spacing icon indicating copy to clipboard operation
hexo-filter-auto-spacing copied to clipboard

pangu.js supports NodeJS

Open SukkaW opened this issue 5 years ago • 4 comments

pangu.js added support for NodeJS and pangunode has been deprecated.

A possible usage:

const pangu = require('pangu');

data.title = pangu.spacing(data.title);
data.content = pangu.spacing(data.content);

SukkaW avatar Sep 02 '19 13:09 SukkaW

Related PR: #8 #9

SukkaW avatar Sep 02 '19 13:09 SukkaW

There are four approaches:

  1. https://github.com/hexojs/hexo-filter-auto-spacing/pull/8 uses remark (to parse markdown).
  2. https://github.com/hexojs/hexo-filter-auto-spacing/pull/9 uses cheerio
  3. https://github.com/hexojs/hexo-filter-auto-spacing/pull/12 uses regex
  4. https://github.com/hexojs/hexo-filter-auto-spacing/pull/13 uses html-dom-parser

Using this test post, currently cheerio yields the best result.

curbengh avatar Sep 03 '19 09:09 curbengh

@curbengh I think we might bring up a unit test first.

SukkaW avatar Sep 04 '19 10:09 SukkaW

Using jsdom allows pangu to get exactly the same behavior in Node.js as in the browser

const { JSDOM } = require('jsdom');
const dom = new JSDOM();
const { window } = dom;
const { document, Node, DocumentFragment, XPathResult } = window;
global.document = document;
global.Node = Node;
global.DocumentFragment = DocumentFragment;
global.XPathResult = XPathResult;
const pangu = require('pangu/src/browser/pangu');

hexo.extend.filter.register('after_post_render', data => {
	document.body.innerHTML = data.content;
	pangu.spacingPageBody();
	data.content = document.body.innerHTML;
	data.title = pangu.spacing(data.title);
}, 8);

Its disadvantage is that it takes more time and memory than other methods

stevenjoezhang avatar Feb 27 '20 14:02 stevenjoezhang