refactor(open_graph): drop cheerio and use regex

https://github.com/hexojs/hexo/pull/3680
This commit is contained in:
curben 2019-08-12 17:41:48 +09:30
parent 8c01309128
commit 5f06b4d260
No known key found for this signature in database
GPG Key ID: 5D9DB57A25D34EE3
1 changed files with 6 additions and 8 deletions

View File

@ -13,7 +13,6 @@
const moment = require('moment') const moment = require('moment')
const { escapeHTML, htmlTag, stripHTML } = require('hexo-util') const { escapeHTML, htmlTag, stripHTML } = require('hexo-util')
let cheerio
function meta (name, content, escape) { function meta (name, content, escape) {
if (escape !== false && typeof content === 'string') { if (escape !== false && typeof content === 'string') {
@ -69,13 +68,12 @@ function openGraphHelper (options = {}) {
if (!images.length && content && content.includes('<img')) { if (!images.length && content && content.includes('<img')) {
images = images.slice() images = images.slice()
if (!cheerio) cheerio = require('cheerio') // https://github.com/hexojs/hexo/pull/3680
const $ = cheerio.load(content) let img
const imgPattern = /<img [^>]*src=['"]([^'"]+)([^>]*>)/gi
$('img').each((index, element) => { while ((img = imgPattern.exec(content)) !== null) {
const src = $(element).attr('data-src') images.push(img[1])
if (src) images.push(src) }
})
} }
if (description) { if (description) {