diff --git a/lib/plugins/helper/format.js b/lib/plugins/helper/format.js index 260f288a33..e80f54b50f 100644 --- a/lib/plugins/helper/format.js +++ b/lib/plugins/helper/format.js @@ -1,6 +1,6 @@ 'use strict'; -const { stripHTML, wordWrap, truncate } = require('hexo-util'); +const { stripHTML, wordWrap, truncate, escapeHTML } = require('hexo-util'); const titlecase = require('titlecase'); exports.strip_html = stripHTML; @@ -14,3 +14,6 @@ exports.word_wrap = wordWrap; exports.wordWrap = wordWrap; exports.truncate = truncate; + +exports.escape_html = escapeHTML; +exports.escapeHTML = escapeHTML; diff --git a/lib/plugins/helper/index.js b/lib/plugins/helper/index.js index 90c40d428a..c7dc22b531 100644 --- a/lib/plugins/helper/index.js +++ b/lib/plugins/helper/index.js @@ -15,13 +15,14 @@ module.exports = ctx => { helper.register('search_form', require('./search_form')); - const { strip_html, trim, titlecase, word_wrap, truncate } = require('./format'); + const { strip_html, trim, titlecase, word_wrap, truncate, escape_html } = require('./format'); helper.register('strip_html', strip_html); helper.register('trim', trim); helper.register('titlecase', titlecase); helper.register('word_wrap', word_wrap); helper.register('truncate', truncate); + helper.register('escape_html', escape_html); helper.register('fragment_cache', require('./fragment_cache')(ctx)); diff --git a/test/scripts/helpers/escape_html.js b/test/scripts/helpers/escape_html.js new file mode 100644 index 0000000000..076a27285b --- /dev/null +++ b/test/scripts/helpers/escape_html.js @@ -0,0 +1,17 @@ +'use strict'; + +describe('escape_html', () => { + const { escapeHTML } = require('../../../lib/plugins/helper/format'); + + it('default', () => { + escapeHTML('

Hello "world".

').should.eql('<p class="foo">Hello "world".</p>'); + }); + + it('str must be a string', () => { + escapeHTML.should.throw('str must be a string!'); + }); + + it('avoid double escape', () => { + escapeHTML('<foo>bar { require('./debug'); require('./css'); require('./date'); + require('./escape_html'); require('./favicon_tag'); require('./feed_tag'); require('./fragment_cache');