Multi-language code includes for Gitbook.
npm install gitbook-plugin-melchiorbook.json, then run gitbook install if you are building your book locally.``js`
{
"plugins": [ "melchior@1.1.0" ],
"pluginsConfig": {
"melchior": {
"includes": "_includes"
}
}
}
- create a folder for your includes that matches the folder in your plugins config (default: _includes)melchior
- put your includes somewhere in that folder
- provide a name for your include, in any number of languages
- pipe the name through , and voila!
``
my-wonderful-book
├─ book.json
└─ _includes
└─ hello
├─ world.html
├─ world.js
└─ world.py
md
This is a code block with tabs for each language:{{ 'hello/world' | melchior }}
`$3
…or you can order and filter tabs by passing in language names.`md
This is a code block with tabs for each language:{{ 'hello/world' | melchior('Python', 'JavaScript', 'HTML') }}
`$3
If you don't want to use Gitbook, an off-label use of this module includes its use (specifically, use of the melchior export of index.js) as a perfectly normal function, passing in the base directory for your includes.When used as a standalone function, I happen to prefer an array for the second arg, so that's also an option. Either way you'll get the same output. You can also just not pass anything after the include name and it'll continue to work in Lazy Mode.
`javascript
const gpm = require('gitbook-plugin-melchior');
const render = require('nunjucks').renderString;
const melchior = gpm.melchior(__dirname);const str1 =
{{ melchior('hello/world', 'Python', 'JavaScript') }};
const str2 = {{ melchior('hello/world', [ 'Python', 'JavaScript' ]) }};
const str3 = {{ melchior('hello/world') }};const res1 = render(str1, { melchior });
const res2 = render(str2, { melchior });
const res3 = render(str3, { melchior });
console.log(res1 === res2); // true, but no such guarantee for res3.
`Issues
- uses synchronous fs methods everywhere. Sorry, not sorry.
- for modes other than Wild-West Mode, this wildly abuses the idea of a filter. I would've made a function, but you can't easily add things to the rendering ctx` with Gitbook.