Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed so root path isn't used anymore.
  • Loading branch information
jonbullock committed Aug 28, 2018
1 parent d5fafa8 commit 46a1960
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
35 changes: 14 additions & 21 deletions jbake-core/src/main/java/org/jbake/util/HtmlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,57 +32,50 @@ public static void fixImageSourceUrls(Map<String, Object> fileContents, JBakeCon
String htmlContent = fileContents.get(Attributes.BODY).toString();

boolean prependSiteHost = config.getBoolean(Keys.IMG_PATH_PREPEND_HOST);

String siteHost = (String) configuration.get("site.host");

String siteHost = config.getString(Keys.SITE_HOST);
String rootPath = fileContents.get(Attributes.ROOTPATH).toString();

String uri = fileContents.get(Attributes.URI).toString();

if(fileContents.get(Attributes.NO_EXTENSION_URI) != null){
if (fileContents.get(Attributes.NO_EXTENSION_URI) != null){
uri = fileContents.get(Attributes.NO_EXTENSION_URI).toString();

//remove trailing "/"
if(uri.endsWith("/")) {
uri = uri.substring(0, uri.length() - 1);
}

uri = uri.substring(0, uri.length() - 1);
}
}

if(uri.contains("/")){
if (uri.contains("/")){
//strip that file name, leaving end "/"
uri = uri.substring(0, uri.lastIndexOf("/") + 1);
uri = uri.substring(0, uri.lastIndexOf("/") + 1);
}

Document document = Jsoup.parseBodyFragment(htmlContent);

Elements allImgs = document.getElementsByTag("img");

for (Element img : allImgs) {
String source = img.attr("src");

// Now add the root path
if(!source.startsWith("http://")
&& !source.startsWith("https://")){
if (!source.startsWith("http://") && !source.startsWith("https://")){

if(!source.startsWith("/")){
source = rootPath + uri + source.replaceFirst("./", "");
if (!source.startsWith("/")){
source = uri + source.replaceFirst("./", "");
}

if (!siteHost.endsWith("/") && !source.startsWith("/")) {
siteHost = siteHost.concat("/");
}

if (!siteHost.endsWith("/") && !source.startsWith("/")) siteHost = siteHost.concat("/");

if(prependSiteHost) {
if (prependSiteHost) {
source = siteHost + source;
}

img.attr("src", source);

}
}


//Use body().html() to prevent adding <body></body> from parsed fragment.
fileContents.put(Attributes.BODY, document.body().html());
}

}
8 changes: 4 additions & 4 deletions jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void shouldNotAddSiteHost(){

String body = fileContent.get(Attributes.BODY).toString();

assertThat(body).contains("src=\"../../../blog/2017/05/first.jpg\"");
assertThat(body).contains("src=\"blog/2017/05/first.jpg\"");

}

Expand All @@ -66,7 +66,7 @@ public void shouldAddContentPath(){

String body = fileContent.get(Attributes.BODY).toString();

assertThat(body).contains("src=\"http://www.jbake.org/../../../blog/2017/05/first.jpg\"");
assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\"");

}

Expand All @@ -81,7 +81,7 @@ public void shouldAddContentPathForCurrentDirectory(){

String body = fileContent.get(Attributes.BODY).toString();

assertThat(body).contains("src=\"http://www.jbake.org/../../../blog/2017/05/first.jpg\"");
assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\"");

}

Expand Down Expand Up @@ -128,7 +128,7 @@ public void shouldAddContentPathForNoExtension(){

String body = fileContent.get(Attributes.BODY).toString();

assertThat(body).contains("src=\"http://www.jbake.org/../../../blog/2017/05/first.jpg\"");
assertThat(body).contains("src=\"http://www.jbake.org/blog/2017/05/first.jpg\"");
}

@Test
Expand Down

0 comments on commit 46a1960

Please sign in to comment.