diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index 58341d22ef..67b0281ed1 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -3,7 +3,6 @@ const urlFn = require('url'); const moment = require('moment'); const { escapeHTML, htmlTag, stripHTML } = require('hexo-util'); -let cheerio; function meta(name, content, escape) { if (escape !== false && typeof content === 'string') { @@ -28,7 +27,6 @@ function og(name, content, escape) { } function openGraphHelper(options = {}) { - if (!cheerio) cheerio = require('cheerio'); const { config, page } = this; const { content } = page; @@ -59,12 +57,14 @@ function openGraphHelper(options = {}) { if (!images.length && content) { images = images.slice(); - const $ = cheerio.load(content); + if (content.includes(']*src=['"]([^'"]+)([^>]*>)/gi; + while ((img = imgPattern.exec(content)) !== null) { + images.push(img[1]); + } + } - $('img').each(function() { - const src = $(this).attr('src'); - if (src) images.push(src); - }); } let result = ''; diff --git a/test/scripts/helpers/open_graph.js b/test/scripts/helpers/open_graph.js index 99db154d5c..158f5caab8 100644 --- a/test/scripts/helpers/open_graph.js +++ b/test/scripts/helpers/open_graph.js @@ -118,7 +118,9 @@ describe('open_graph', () => { page: { content: [ '
123456789
', - '' + '', + '', + '' ].join('') }, config: hexo.config, @@ -163,7 +165,9 @@ describe('open_graph', () => { page: { content: [ '123456789
', - '' + '', + '', + '' ].join(''), photos: [] },