Skip to content

Commit

Permalink
feat(helper): add escape_html (#4581)
Browse files Browse the repository at this point in the history
  • Loading branch information
awwong1 authored Nov 27, 2020
1 parent bd0a4ec commit 8993328
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/plugins/helper/format.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,3 +14,6 @@ exports.word_wrap = wordWrap;
exports.wordWrap = wordWrap;

exports.truncate = truncate;

exports.escape_html = escapeHTML;
exports.escapeHTML = escapeHTML;
3 changes: 2 additions & 1 deletion lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
17 changes: 17 additions & 0 deletions test/scripts/helpers/escape_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

describe('escape_html', () => {
const { escapeHTML } = require('../../../lib/plugins/helper/format');

it('default', () => {
escapeHTML('<p class="foo">Hello "world".</p>').should.eql('&lt;p class&#x3D;&quot;foo&quot;&gt;Hello &quot;world&quot;.&lt;&#x2F;p&gt;');
});

it('str must be a string', () => {
escapeHTML.should.throw('str must be a string!');
});

it('avoid double escape', () => {
escapeHTML('&lt;foo>bar</foo&gt;').should.eql('&lt;foo&gt;bar&lt;&#x2F;foo&gt;');
});
});
1 change: 1 addition & 0 deletions test/scripts/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('Helpers', () => {
require('./debug');
require('./css');
require('./date');
require('./escape_html');
require('./favicon_tag');
require('./feed_tag');
require('./fragment_cache');
Expand Down

0 comments on commit 8993328

Please sign in to comment.