diff --git a/jbake-core/src/main/java/org/jbake/app/ConfigUtil.java b/jbake-core/src/main/java/org/jbake/app/ConfigUtil.java index 8ae065bcc..29293467c 100644 --- a/jbake-core/src/main/java/org/jbake/app/ConfigUtil.java +++ b/jbake-core/src/main/java/org/jbake/app/ConfigUtil.java @@ -181,6 +181,10 @@ public interface Keys { * The configured base URI for the hosted content */ String SITE_HOST = "site.host"; + /** + * Property to define if image paths should be prepended with site.host url + */ + String IMG_PATH_PREPEND_HOST = "img.path.prepend.host"; } private final static Logger LOGGER = LoggerFactory.getLogger(ConfigUtil.class); 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 82d9ba30e..141d82e3b 100644 --- a/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java +++ b/jbake-core/src/main/java/org/jbake/util/HtmlUtil.java @@ -29,8 +29,12 @@ public class HtmlUtil { public static void fixImageSourceUrls(Map fileContents, CompositeConfiguration config){ String htmlContent = fileContents.get(Attributes.BODY).toString(); + + boolean prependSiteHost = config.getBoolean(Keys.IMG_PATH_PREPEND_HOST); String siteHost = config.getString(Keys.SITE_HOST); + + String rootPath = fileContents.get(Attributes.ROOTPATH).toString(); String uri = fileContents.get(Attributes.URI).toString(); @@ -56,21 +60,21 @@ public static void fixImageSourceUrls(Map fileContents, Composit for (Element img : allImgs) { String source = img.attr("src"); - if(source.startsWith("./")){ - // image relative to current content is specified, - // lets add current url to it. - source = source.replaceFirst("./", uri); - } - // Now add the root path if(!source.startsWith("http://") && !source.startsWith("https://")){ + + if(!source.startsWith("/")){ + source = rootPath + uri + source.replaceFirst("./", ""); + } if (!siteHost.endsWith("/") && !source.startsWith("/")) siteHost = siteHost.concat("/"); + + if(prependSiteHost) { + source = siteHost + source; + } - String fullUrl = siteHost + source; - - img.attr("src", fullUrl); + img.attr("src", source); } } diff --git a/jbake-core/src/main/resources/default.properties b/jbake-core/src/main/resources/default.properties index 01a45d96f..91fd905df 100644 --- a/jbake-core/src/main/resources/default.properties +++ b/jbake-core/src/main/resources/default.properties @@ -97,3 +97,5 @@ uri.noExtension=false uri.noExtension.prefix= # String used to separate the header from the body header.separator=~~~~~~ +# Prepend site.host to image paths +img.path.prepend.host=true 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 68b87e3e3..6c0b0416f 100644 --- a/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java +++ b/jbake-core/src/test/java/org/jbake/util/HtmlUtilTest.java @@ -29,7 +29,7 @@ public void shouldNotAddBodyHTMLElement(){ Map fileContent = new HashMap(); fileContent.put(Attributes.ROOTPATH, "../../../"); fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); + fileContent.put(Attributes.BODY, "
Test
"); HtmlUtil.fixImageSourceUrls(fileContent, config); @@ -40,34 +40,50 @@ public void shouldNotAddBodyHTMLElement(){ } + @Test + public void shouldNotAddSiteHost(){ + Map fileContent = new HashMap(); + fileContent.put(Attributes.ROOTPATH, "../../../"); + fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); + fileContent.put(Attributes.BODY, "
Test
"); + config.setProperty(ConfigUtil.Keys.IMG_PATH_PREPEND_HOST,false); + + HtmlUtil.fixImageSourceUrls(fileContent, config); + + String body = fileContent.get(Attributes.BODY).toString(); + + assertThat(body).contains("src=\"../../../blog/2017/05/first.jpg\""); + + } + @Test - public void shouldAddRootpath(){ + public void shouldAddContentPath(){ Map fileContent = new HashMap(); fileContent.put(Attributes.ROOTPATH, "../../../"); fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); + fileContent.put(Attributes.BODY, "
Test
"); HtmlUtil.fixImageSourceUrls(fileContent, config); 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 - public void shouldAddContentpath(){ + public void shouldAddContentPathForCurrentDirectory(){ Map fileContent = new HashMap(); fileContent.put(Attributes.ROOTPATH, "../../../"); fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); - fileContent.put(Attributes.BODY, "
Test
"); - + fileContent.put(Attributes.BODY, "
Test
"); + HtmlUtil.fixImageSourceUrls(fileContent, config); - + 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 @@ -86,12 +102,12 @@ public void shouldNotAddRootPath(){ } @Test - public void shouldAddRootPathForNoExtension(){ + public void shouldNotAddRootPathForNoExtension(){ Map fileContent = new HashMap(); fileContent.put(Attributes.ROOTPATH, "../../../"); fileContent.put(Attributes.URI, "blog/2017/05/first_post.html"); fileContent.put(Attributes.NO_EXTENSION_URI, "blog/2017/05/first_post/"); - fileContent.put(Attributes.BODY, "
Test
"); + fileContent.put(Attributes.BODY, "
Test
"); HtmlUtil.fixImageSourceUrls(fileContent, config); @@ -113,7 +129,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