Skip to content

Commit

Permalink
Merge pull request #3728 from curbengh/escape-html
Browse files Browse the repository at this point in the history
fix(paginator): compatibility with hexo-util@1.3.0
  • Loading branch information
curbengh authored Sep 27, 2019
2 parents 53ebe22 + 6fd5231 commit ffe4eaa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
18 changes: 10 additions & 8 deletions lib/plugins/helper/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const createLink = (options, ctx) => {

const createPageTag = (options, ctx) => {
const link = createLink(options, ctx);
const { current, transform } = options;
const { current, escape, transform } = options;

return i => {
if (i === current) {
return htmlTag('span', { class: 'page-number current' }, transform ? transform(i) : i);
return htmlTag('span', { class: 'page-number current' }, transform ? transform(i) : i, escape);
}
return htmlTag('a', { class: 'page-number', href: link(i) }, transform ? transform(i) : i);
return htmlTag('a', { class: 'page-number', href: link(i) }, transform ? transform(i) : i, escape);
};
};

Expand Down Expand Up @@ -44,7 +44,7 @@ const pagenasionPartShow = (tags, options, ctx) => {
const rightEnd = total - current <= endSize ? current + 1 : total - endSize + 1;
const leftMid = current - midSize <= endSize ? leftEnd + 1 : current - midSize;
const rightMid = current + midSize + endSize > total ? rightEnd - 1 : current + midSize;
const spaceHtml = htmlTag('span', { class: 'space' }, space);
const spaceHtml = htmlTag('span', { class: 'space' }, space, false);

const pageTag = createPageTag(options, ctx);

Expand Down Expand Up @@ -97,15 +97,17 @@ function paginatorHelper(options = {}) {
space: '&hellip;',
next_text: 'Next',
prev_text: 'Prev',
prev_next: true
prev_next: true,
escape: true
}, options);

const {
current,
total,
prev_text: prevText,
next_text: nextText,
prev_next: prevNext
prev_next: prevNext,
escape
} = options;

if (!current) return '';
Expand All @@ -116,7 +118,7 @@ function paginatorHelper(options = {}) {

// Display the link to the previous page
if (prevNext && current > 1) {
tags.push(htmlTag('a', { class: 'extend prev', rel: 'prev', href: link(current - 1)}, prevText));
tags.push(htmlTag('a', { class: 'extend prev', rel: 'prev', href: link(current - 1)}, prevText, escape));
}

if (options.show_all) {
Expand All @@ -127,7 +129,7 @@ function paginatorHelper(options = {}) {

// Display the link to the next page
if (prevNext && current < total) {
tags.push(htmlTag('a', { class: 'extend next', rel: 'next', href: link(current + 1) }, nextText));
tags.push(htmlTag('a', { class: 'extend next', rel: 'next', href: link(current + 1) }, nextText, escape));
}

return tags.join('');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"hexo-fs": "^2.0.0",
"hexo-i18n": "^1.0.0",
"hexo-log": "^0.2.0",
"hexo-util": "^1.2.0",
"hexo-util": "^1.3.1",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"micromatch": "^4.0.2",
Expand Down
22 changes: 22 additions & 0 deletions test/scripts/helpers/paginator.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,26 @@ describe('paginator', () => {

result.should.eql('');
});

it('escape', () => {
const result = paginator({
current: 2,
prev_text: '<foo>',
next_text: '<bar>',
escape: false
});

result.should.eql([
'<a class="extend prev" rel="prev" href="/">',
'<foo></a>',
'<a class="page-number" href="/">1</a>',
'<span class="page-number current">2</span>',
'<a class="page-number" href="/page/3/">3</a>',
'<a class="page-number" href="/page/4/">4</a>',
'<span class="space">&hellip;</span>',
'<a class="page-number" href="/page/10/">10</a>',
'<a class="extend next" rel="next" href="/page/3/">',
'<bar></a>'
].join(''));
});
});
12 changes: 6 additions & 6 deletions test/scripts/tags/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ describe('link', () => {
it('text + url', () => {
const $ = cheerio.load(link('Click here to Google https://google.com'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
});

it('text + url + external', () => {
let $ = cheerio.load(link('Click here to Google https://google.com true'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
$('a').attr('target').should.eql('_blank');

$ = cheerio.load(link('Click here to Google https://google.com false'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
$('a').attr('title').should.eql('');
$('a').attr('target').should.eql('');
Expand All @@ -30,22 +30,22 @@ describe('link', () => {
it('text + url + title', () => {
const $ = cheerio.load(link('Click here to Google https://google.com Google link'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
$('a').attr('title').should.eql('Google link');
});

it('text + url + external + title', () => {
let $ = cheerio.load(link('Click here to Google https://google.com true Google link'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
$('a').attr('target').should.eql('_blank');
$('a').attr('title').should.eql('Google link');

$ = cheerio.load(link('Click here to Google https://google.com false Google link'.split(' ')));

$('a').attr('href').should.eql('https://google.com');
$('a').attr('href').should.eql('https://google.com/');
$('a').html().should.eql('Click here to Google');
$('a').attr('target').should.eql('');
$('a').attr('title').should.eql('Google link');
Expand Down

0 comments on commit ffe4eaa

Please sign in to comment.