Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle invalid URL #163

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch binding is optional in Node 10+, tested with eslint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It is available since Node.js 10.0.0.

Might remove all of them later.

out += href;
}

out += '"';

if (title) {
out += ` title="${title}"`;
}
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
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