CLI to convert an existing document to a GitBook.
Install this globally and you'll have access to the gitbook-convert
command anywhere on your system.
$ npm install gitbook-convert -g
$ gitbook-convert [options] <file> [export-directory]
Short | Long | Description | Type | Default |
---|---|---|---|---|
-t | --document-title | Name used for the main document title | string | null |
-a | --assets-dir | Name of the document's assets export directory | string | assets |
-m | --max-depth | Maximum title depth to use to split your original document into sub-chapters | integer | 2 |
-p | --prefix | Prefix filenames by an incremental counter | flag | false |
-d | --debug | Log stack trace when an error occurs | flag | false |
After converting your document, the corresponding GitBook files will be placed in the provided export-directory
folder. The folder is created during conversion.
If export-directory
is not provided, a new export
folder is created in the current working directory. The GitBook files are then placed here.
If the --document-title
argument is not passed, the filename without the file extension will be used as the main document title.
Type | Extension |
---|---|
Microsoft Office Open XML Document | .docx |
OpenOffice / Open Document Format | .odt |
Docbook Markup Language | .xml |
HyperText Markup Language | .html |
This version of gitbook-convert
generates markdown files only. Support for asciidoc might be added later.
gitbook-convert
divides your original document into chapters and sub-chapters, if any, one per output file. To do this, gitbook-convert
automatically detects the headers in your document and uses the -m
flag to split it into sub-chapters.
When converting a Docbook file though, the depth is always detected automatically.
Thus, converting the following document named History of modern computers.docx with the default --max-depth
flag:
What the world used to be.
At the beginning was the big bang...
Strange creatures called “humans” had trouble living in peace...
What the world is now.
Computers came to rule the world...
The power supply went disconnected.
will produce the following output:
user @ cwd/export/history_of_modern_computers
README.md
SUMMARY.md
assets/
chapter_1/
README.md
the_beginning.md
the_following.md
chapter_2/
README.md
the_awakening.md
the_end.md
While using 1
for --max-depth
would produce:
user @ cwd/export/history_of_modern_computers
chapter_1.md
chapter_2.md
README.md
SUMMARY.md
assets/
The SUMMARY.md
file is created automatically.
For our first example:
# Summary
* [Introduction](README.md)
* [Chapter 1](chapter_1/README.md)
* [The beginning](chapter_1/the_beginning.md)
* [The following](chapter_1/the_following.md)
* [Chapter 2](chapter_2/README.md)
* [The awakening](chapter_2/the_awakening.md)
* [The end](chapter_2/the_end.md)
With --max-depth
set to 1
:
# Summary
* [Introduction](README.md)
* [Chapter 1](chapter_1.md)
* [Chapter 2](chapter_2.md)
The content of the README.md
file depends on your document structure. Anyways, the filename of your original document will be used as the main title here.
gitbook-convert
creates the default GitBook README.md
file:
# History of modern computers
This file serves as your book's preface, a great place to describe your book's content and ideas.
Otherwise, everything before the first main header is used as the README.md
content. If we modify our example to be:
A short history of modern computers.
At the beginning was the big bang...
...
The content of the README.md
file will be:
# History of modern computers
A short history of modern computers.
The behavior is the same when --max-depth
is set to higher levels. Each README.md
in the sub-chapters folders will contain the preface for the current chapter.
The appropriate converter for a document type is deduced from its extension.
For now, the converters should:
- be placed in
lib/converters
, - with its filename being the document-type extension, for example
/lib/converters/docx.js
, - added to the
lib/converters/index.js
file for reference and use.
The .docx
converter uses mwilliamson's mammoth.js to convert your document to HTML before generating the output.
gitbook-convert
will try to export your inline images in the /assets
folder, using the image title as the image filename if provided.
The .odt
converter uses odt2html to convert your document to HTML before generating the output. Because there was no node module out there to convert OpenOffice documents to HTML, we built our own.
gitbook-convert
will try to export your inline images in the /assets
folder, using the image name in the document as the image filename if provided.
gitbook-convert
requires xsltproc to be installed to process a Docbook. If you are using MacOS or a Linux distribution, it should be installed by default.
You can test that xsltproc is installed using:
$ which xsltproc
xsltproc uses the last version of docbook.xsl to convert your Docbook to HTML first. Since the Docbook XML markup is very large, gitbook-convert
will try to convert the meta-data as well as possible. Extended conversion might be added to the tool based on user requests.
When you install gitbook-convert
using npm, the docbook.xsl stylesheets are downloaded and installed along with the app.
We recommend using the tool with Docbook version 5. Here is a walk-through for converting an existing Docbook in version 4 to version 5.