Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

pangu.js supports NodeJS #11

Open
SukkaW opened this issue Sep 2, 2019 · 4 comments
Open

pangu.js supports NodeJS #11

SukkaW opened this issue Sep 2, 2019 · 4 comments

Comments

@SukkaW
Copy link
Member

SukkaW commented Sep 2, 2019

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
Copy link
Member Author

SukkaW commented Sep 2, 2019

Related PR: #8 #9

@curbengh
Copy link
Contributor

curbengh commented Sep 3, 2019

There are four approaches:

  1. fix: only add space to text, not inside markdown links #8 uses remark (to parse markdown).
  2. fix: only add space to text, not links #9 uses cheerio
  3. refactor: replace cheerio with regex #12 uses regex
  4. refactor: replace cheerio with html-dom-parser #13 uses html-dom-parser

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

@SukkaW
Copy link
Member Author

SukkaW commented Sep 4, 2019

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

@stevenjoezhang
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants