Skip to content

Commit

Permalink
fix: handle invalid URL
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 31, 2020
1 parent 0abcf7d commit 46ea0d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ class Renderer extends MarkedRenderer {
}
}

let out = `<a href="${encodeURL(href)}"`;
let out = '<a href="';

try {
out += encodeURL(href);
} catch (e) {
out += href;
}

out += '"';

if (title) {
out += ` title="${title}"`;
}
Expand Down
12 changes: 11 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Marked renderer', () => {

result.should.eql([
'<h1 id="Hello-world"><a href="#Hello-world" class="headerlink" title="Hello world"></a>Hello world</h1>',
'<pre><code>' + escapeHTML(code) + '</code></pre>\n',
'<pre><code>' + escapeHTML(code) + '</code></pre>',
'<h2 id="Hello-world-1"><a href="#Hello-world-1" class="headerlink" title="Hello world"></a>Hello world</h2>',
'<p>hello</p>'
].join('') + '\n');
Expand Down Expand Up @@ -150,6 +150,16 @@ describe('Marked renderer', () => {
].join('\n'));
});

it('shouldn\'t encode when not a valid URL', () => {
const url = 'http://localhost:4000你好';

const body = `[foo](${url})`;

const result = r({text: body});

result.should.eql(`<p><a href="${url}">foo</a></p>\n`);
});

describe('autolink option tests', () => {
const hexo = new Hexo(__dirname, {silent: true});
const ctx = Object.assign(hexo, {
Expand Down

0 comments on commit 46ea0d6

Please sign in to comment.