Skip to content

Commit

Permalink
Merge pull request #99 from curbengh/newlines-regex
Browse files Browse the repository at this point in the history
fix(autodiscovery): handle multi-line head tag correctly
  • Loading branch information
curbengh committed Oct 19, 2019
2 parents 4ea5b4c + 46e5b39 commit d169992
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "hexo",
"root": true
"root": true,
"parserOptions": {
"ecmaVersion": 2018
}
}
2 changes: 1 addition & 1 deletion lib/autodiscovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function autodiscoveryInject(data) {

const autodiscoveryTag = `<link rel="alternate" href="${url_for.call(this, feed.path)}" title="${config.title}" type="application/${type}+xml">`;

return data.replace(/<head>(?!<\/head>).+?<\/head>/, (str) => str.replace('</head>', `${autodiscoveryTag}</head>`));
return data.replace(/<head>(?!<\/head>).+?<\/head>/s, (str) => str.replace('</head>', `${autodiscoveryTag}</head>`));
}

module.exports = autodiscoveryInject;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
"nyc": "14.1.1"
},
"engines": {
"node": ">=8.6.0"
"node": ">=8.10.0"
}
}
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ describe('Autodiscovery', () => {
$('link[rel="alternate"]').attr('type').should.eql('application/rss+xml');

result.should.eql('<head><link><link rel="alternate" href="/rss2.xml" title="foo" type="application/rss+xml"></head>');

hexo.config.feed = {
type: 'atom',
path: 'atom.xml',
autodiscovery: true
};
});

it('multi-line head tag', () => {
const content = '<head>\n<link>\n</head>';
const result = autoDiscovery(content);

result.should.eql('<head>\n<link>\n<link rel="alternate" href="/atom.xml" title="foo" type="application/atom+xml"></head>');
});
});

0 comments on commit d169992

Please sign in to comment.