Skip to content

Commit

Permalink
fix: custom protocol URLs are wrongly turned into absolute URLs (refs:
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Sep 3, 2021
1 parent 7a12552 commit e6c1dc2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/utils/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const marked = require("marked");
const highlightjs = require("highlight.js");
const urljoin = require("url-join");

const httpRe = /^https?:\/\//i;
const urlWithProtocolRe = /.*:\/\//i;

// Convert GitHub URL to GitHub raw URL.
const convertToGitHubRawUrl = function(url) {
Expand All @@ -29,7 +29,7 @@ const markedRenderer = function({
if (href.startsWith("#")) {
return `<a href='${href}'>${text}</a>`;
}
if (!httpRe.test(href)) {
if (!urlWithProtocolRe.test(href)) {
if (href.startsWith("/")) {
href = urljoin(linkBaseUrl, href);
} else {
Expand All @@ -42,7 +42,7 @@ const markedRenderer = function({
};

renderer.image = (href, title, text) => {
if (!httpRe.test(href)) {
if (!urlWithProtocolRe.test(href)) {
if (href.startsWith("/")) {
href = urljoin(imageBaseUrl, href);
} else {
Expand Down Expand Up @@ -144,7 +144,7 @@ const postProcessHtml = function(html, { imageBaseRelativeUrl }) {
// 1x1 transparent base64 png pixel.
return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkqAcAAIUAgUW0RjgAAAAASUVORK5CYII=";
}
if (!httpRe.test(attr)) attr = urljoin(imageBaseRelativeUrl, attr);
if (!urlWithProtocolRe.test(attr)) attr = urljoin(imageBaseRelativeUrl, attr);
return attr;
});
return $.html()
Expand Down
13 changes: 13 additions & 0 deletions test/app-utils-markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ describe("app/utils/markdown.js", function() {
'<div><p><a rel="noopener noreferrer" href="http://example.com">link</a></p>\n</div>'
);
});
it("custom protocol link", async function() {
const markdown = "[link](unityhub://2021.1.19f1/5f5eb8bbdc25)";
const pkg = loadPackageSync("com.littlebigfun.addressable-importer");
const html = await renderMarkdownToHtml({
pkg,
markdown,
disableTitleParser: true
});
assert.equal(
html,
'<div><p><a rel="noopener noreferrer" href="unityhub://2021.1.19f1/5f5eb8bbdc25">link</a></p>\n</div>'
);
});
it("img + relative path", async function() {
const markdown = "![image](path-1.png)";
const pkg = loadPackageSync("com.littlebigfun.addressable-importer");
Expand Down

0 comments on commit e6c1dc2

Please sign in to comment.