Skip to content

Commit

Permalink
Merge pull request #499 from vivliostyle/support-github-and-aozorabun…
Browse files Browse the repository at this point in the history
…ko-urls

Support loading documents from GitHub and Aozorabunko URLs
  • Loading branch information
MurakamiShinyu authored Jan 29, 2019
2 parents b018533 + 385738b commit 5e12deb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/adapt/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ adapt.base.resolveURL = (relURL, baseURL) => {
if (relURL.toLowerCase().match("^javascript:")) {
return "#";
}
// Convert special URLs
let r;
if (r = (/^(https?:)\/\/github\.com\/([^/]+\/[^/]+)\/(blob\/|tree\/)?(.*)$/).exec(relURL)) {
// Convert GitHub URL to GitHub raw URL
relURL = `${r[1]}//raw.githubusercontent.com/${r[2]}/${r[3] ? '' : 'master/'}${r[4]}`;
} else if (r = (/^(https?:)\/\/www\.aozora\.gr\.jp\/(cards\/[^/]+\/files\/[^/.]+\.html)$/).exec(relURL)) {
// Convert Aozorabunko (X)HTML URL to GitHub raw URL
relURL = `${r[1]}//raw.githubusercontent.com/aozorabunko/aozorabunko/master/${r[2]}`;
}
return relURL;
}
if (baseURL.match(/^\w{2,}:\/\/[^\/]+$/))
Expand Down
15 changes: 13 additions & 2 deletions src/adapt/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,19 @@ adapt.net.ajax = (url, opt_type, opt_method, opt_data, opt_contentType) => {
request.send(opt_data);
}
else {
if (url.match(/file:\/\/.*(\.html$|\.htm$)/))
request.overrideMimeType("text/html");
if ((/^file:|^https?:\/\/[^/]+\.githubusercontent\.com/).test(url)) {
// File or GitHub raw URL
if ((/\/aozorabunko\/[^/]+\/cards\/[^/]+\/files\/[^/.]+\.html$/).test(url)) {
// Aozorabunko's (X)HTML support
request.overrideMimeType("text/html; charset=Shift_JIS");
} else if ((/\.(html|htm)$/).test(url)) {
request.overrideMimeType("text/html; charset=UTF-8");
} else if ((/\.(xhtml|xht|xml|opf)$/).test(url)) {
request.overrideMimeType("application/xml; charset=UTF-8");
} else if ((/\.(txt|css)$/).test(url)) {
request.overrideMimeType("text/plain; charset=UTF-8");
}
}
request.send(null);
}
} catch (e) {
Expand Down

0 comments on commit 5e12deb

Please sign in to comment.