Goldsmith-Abs Goldsmith

golang goldsmith goldsmith-abs goldsmith-components mit license web

Goldsmith-Abs converts all relative URLs to absolute paths on your website. This is useful when working with plugins like Goldsmith-Layout and Goldsmith-Collection, which can render a page’s content from the context of a different directory (imagine an index page showing inline previews of blog posts). This plugin makes it easy to fix incorrect relative file references by making sure all paths are absolute before content is featured on other sections of your site.

Usage

When processing HTML documents, the Goldsmith-Abs plugin assumes that all relative file references are relative to the server root. For example, a relative reference to img/food.jpg from the file /blog/index.html will be resolved to the absolute path /blog/img/food.jpg. This is the default plugin behavior and no configuration is needed:

errs := goldsmith.Begin(srcDir).
    // ...
    Chain(abs.New()).
    // ...
End(dstDir)

If your website is not hosted at the server root, it is possible to specify a custom base path. When the plugin is used in this manner, the relative image URL featured in the example above will be resolved to /~account/blog/img/food.jpg. The root sever path can be set via the BaseUrl method:

errs := goldsmith.Begin(srcDir).
    // ...
    Chain(abs.New().BaseUrl("/~account")).
    // ...
End(dstDir)

It should be noted that this plugin will not modify absolute URLs in any way. If the image path from the previous example were /img/food.jpg instead of img/food.jpg, it would be left as such regardless of the value of BaseUrl.

It is also possible to specify custom HTML attributes to search for relative paths (src and href are the default), by using the Attrs method.

License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.