Search code examples
javascripthelperassemble

assemble.io inlining js file


is there an easy way to inline files with assemble.io?

i found only this one http://assemble.io/helpers/helpers-code.html but this replaces all " with &#x2 7; and at the first and last line it inserts &#x60 ;&#x60 ;&#x60 ;js

{{embed 'src/js/loadJs.js'}}

results in

```js
...
function load(scripts){
    ...
    script = document.createElement('script');
    ...
}
```

Solution

  • The embed helper wraps the file content in ``` so it can be used in code examples.

    If you know that you're going to use the helper specifically for embedding javascript like in your example. I would create a new helper that just reads the content in:

    var fs = require('fs');
    function inline(src) {
      return fs.readFileSync(src, 'utf8');
    }
    

    You can add some error handling and checking around that, but it'll inline the file for you.

    You can either use Handlebars.SafeString around the content to indicate that the string should not be escaped, or you can use triple curly braces in the handlebars file: {{{inline "path/to/file.js"}}}