From 883db1a360228c02690f04dafaab24c9c46eb038 Mon Sep 17 00:00:00 2001 From: Menci Date: Sat, 23 Mar 2019 15:19:22 +0800 Subject: [PATCH] Replace cheerio with jsdom to fix 's xlink: bug --- package.json | 1 + utility.js | 17 +++++++---------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index b8ed0a3..e42d618 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "gravatar": "^1.8.0", "javascript-time-ago": "^1.0.30", "js-yaml": "^3.9.0", + "jsdom": "^14.0.0", "jsondiffpatch": "0.2.5", "jsonwebtoken": "^8.4.0", "katex": "^0.10.1", diff --git a/utility.js b/utility.js index 21936c2..d6f764c 100644 --- a/utility.js +++ b/utility.js @@ -26,6 +26,7 @@ let querystring = require('querystring'); let gravatar = require('gravatar'); let filesize = require('file-size'); let AsyncLock = require('async-lock'); +let JSDOM = require('jsdom').JSDOM; module.exports = { resolvePath(s) { @@ -41,20 +42,16 @@ module.exports = { .split('').join('
') .split('
').join('
').split('
').join(''); - let cheerio = require('cheerio'); - let $ = cheerio.load(''); - let body = $('body'); - body.html(s); + let jsdom = new JSDOM(), document = jsdom.window.document; + document.body.innerHTML = s; - let a = $('img:only-child'); + let a = document.querySelectorAll('p > img:only-child'); for (let img of Array.from(a)) { - if (!img.prev && !img.next) { - $(img).css('display', 'block'); - $(img).css('margin', '0 auto'); - } + img.style.display = 'block'; + img.style.margin = '0 auto'; } - return body.html(); + return document.body.innerHTML; }; return new Promise((resolve, reject) => { if (!keys) {