diff options
author | Samy Pessé <samypesse@gmail.com> | 2014-07-10 17:59:34 -0700 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2014-07-10 17:59:34 -0700 |
commit | a9f473260cdbf7944a8d95c4292a93897f9210eb (patch) | |
tree | 83e78b9028850606722da6ec66c45e453ee28693 /lib/parse/code_include.js | |
parent | 520045441b9401fadfba2b1b03696bd15b60027d (diff) | |
parent | d16577e8bf2e404cbc2cabb78fc1bf6c8a36d3ea (diff) | |
download | gitbook-a9f473260cdbf7944a8d95c4292a93897f9210eb.zip gitbook-a9f473260cdbf7944a8d95c4292a93897f9210eb.tar.gz gitbook-a9f473260cdbf7944a8d95c4292a93897f9210eb.tar.bz2 |
Merge pull request #356 from GitbookIO/feature/includes
Support importing code snippets
Diffstat (limited to 'lib/parse/code_include.js')
-rw-r--r-- | lib/parse/code_include.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/parse/code_include.js b/lib/parse/code_include.js new file mode 100644 index 0000000..f4656f1 --- /dev/null +++ b/lib/parse/code_include.js @@ -0,0 +1,20 @@ +var fs = require('fs'); +var path = require('path'); + +module.exports = function(code, folder) { + folder = folder || ''; + + return code.replace(/{{([\s\S]+?)}}/g, function(match, filename) { + // Normalize filename + var fname = path.join(folder, filename.trim()); + + // Try including snippet from FS + try { + // Trim trailing newlines/space of imported snippets + return fs.readFileSync(fname, 'utf8').trimRight(); + } catch(err) {} + + // If fails leave content as is + return match; + }); +}; |