diff --git a/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java b/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java index 842bb7bd8..c425e1ed8 100644 --- a/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java +++ b/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java @@ -32,57 +32,50 @@ public static void fixImageSourceUrls(Map 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 from parsed fragment. fileContents.put(Attributes.BODY, document.body().html()); } - } diff --git a/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java b/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java index 278a5256e..a107dd1c6 100644 --- a/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java +++ b/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java @@ -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\""); } @@ -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\""); } @@ -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\""); } @@ -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