Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Process all image/link URLs during github rewrites #416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/plugin/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function replaceTagAttribute (html, tag, attribute, buildUrl, repository) {
// Skip fully-qualified URLs, #hash fragments, and protocol-relative URLs
if (!url.host && url.path && !url.path.match(/^\/\//)) {
var newUrl = buildUrl(repository, url)
return html.slice(0, attr.index) + attr[0] + quote + newUrl + quote + substring.slice(src[0].length + 1)
var htmlPrefix = html.slice(0, attr.index) + attr[0] + quote + newUrl + quote
var htmlPostfix = substring.slice(src[0].length + 1)
var processedPostfix = replaceTagAttribute(htmlPostfix, tag, attribute, buildUrl, repository)
return htmlPrefix + (processedPostfix || htmlPostfix)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Smiley! :)
# Heading with nested image/link: <a href="nested/link/image"><img src="nested/link/image/image.png"></a>. Nice.

<img src="html/block.png" />
<br />
<img src="html/block2.png" />

<p><a href="html/block.html">Link in an HTML block</a></p>
<p><a href="html/block.html">Link in an HTML block</a>
and <a href="html/block2.html">second link</a></p>

6 changes: 6 additions & 0 deletions test/repo-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ describe('when package repo is on github', function () {
it('rewrites slashy relative links hrefs to absolute (HTML)', function () {
assert(~fixtures.github.indexOf('<a href="nested/link/image">'))
assert(~fixtures.github.indexOf('<a href="html/block.html">Link in an HTML block</a>'))
assert(~fixtures.github.indexOf('<a href="html/block2.html">second link</a>'))
assert($("a[href='https://github.com/mark/wahlberg/blob/HEAD/nested/link/image']").length)
assert($("a[href='https://github.com/mark/wahlberg/blob/HEAD/html/block.html']").length)
assert($("a[href='https://github.com/mark/wahlberg/blob/HEAD/html/block2.html']").length)
assert($short("a[href='https://github.com/mark/wahlberg/blob/HEAD/nested/link/image']").length)
assert($short("a[href='https://github.com/mark/wahlberg/blob/HEAD/html/block.html']").length)
assert($short("a[href='https://github.com/mark/wahlberg/blob/HEAD/html/block2.html']").length)
})

it('leaves protocol-relative URLs alone', function () {
Expand Down Expand Up @@ -101,10 +104,13 @@ describe('when package repo is on github', function () {
it('replaces slashy relative img URLs with github URLs (HTML)', function () {
assert(~fixtures.github.indexOf('<img src="nested/link/image/image.png">'))
assert(~fixtures.github.indexOf('<img src="html/block.png" />'))
assert(~fixtures.github.indexOf('<img src="html/block2.png" />'))
assert($("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/nested/link/image/image.png']").length)
assert($("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/html/block.png']").length)
assert($("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/html/block2.png']").length)
assert($short("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/nested/link/image/image.png']").length)
assert($short("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/html/block.png']").length)
assert($short("img[src='https://raw.githubusercontent.com/mark/wahlberg/HEAD/html/block2.png']").length)
})

it('leaves protocol relative URLs alone', function () {
Expand Down