diff options
author | Samy Pessé <samypesse@gmail.com> | 2017-01-06 22:14:38 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2017-01-06 22:14:38 +0100 |
commit | 9440cdee28286c077adec26d5cf2bb679486575e (patch) | |
tree | a3bc87460b4399aaecf1e7fd6ec197e8901a8a18 /docs/internals | |
parent | cc7b2b2181ce35acdd080edc9adbf8cc6b3f93e4 (diff) | |
download | gitbook-origin/test-utils.zip gitbook-origin/test-utils.tar.gz gitbook-origin/test-utils.tar.bz2 |
Improve documentationorigin/test-utils
Diffstat (limited to 'docs/internals')
-rw-r--r-- | docs/internals/README.md | 33 | ||||
-rw-r--r-- | docs/internals/api.md | 59 | ||||
-rw-r--r-- | docs/internals/browser.md | 37 |
3 files changed, 129 insertions, 0 deletions
diff --git a/docs/internals/README.md b/docs/internals/README.md new file mode 100644 index 0000000..7ee13c3 --- /dev/null +++ b/docs/internals/README.md @@ -0,0 +1,33 @@ +# Using GitBook programmatically + +GitBook is mainly designed to work as a command line utility, but it can also be integrated into javascript application (Node.js or browser). + +Its API can be used for parsing a book, modifying the structure of a book, or generating outputs. + +### Installation + +``` +$ npm install gitbook +``` + +### Design + +The GitBook Core API is built around **promises** and **immutable** models. + +### Example + +```js +const GitBook = require('gitbook'); + +// Create a filesystem interface +const fs = GitBook.createNodeFS(__dirname + '/mybook'); + +// Create a book instance +const book = GitBook.Book.createForFS(fs); + +// Parse it +GitBook.Parse.parseBook(book) +.then(newBook => { + console.log('Done!'); +}) +``` diff --git a/docs/internals/api.md b/docs/internals/api.md new file mode 100644 index 0000000..0077adc --- /dev/null +++ b/docs/internals/api.md @@ -0,0 +1,59 @@ +# APIs + +- [Parsing](#parsing) +- [Manipulation](#manipulation) +- [Generation](#generation) + +## Parsing + +### `GitBook.Parse.parseBook` +`GitBook.Parse.parseBook(book: Book): Promise<Book>` + +Parse the whole book (languages, readme, summary), it returns a complete version of the book instance. + +### `GitBook.Parse.parse[Summary|Glossary|Languages]` +`GitBook.Parse.parse[X](book: Book): Promise<Book>` + +Parse a specific part of the book only, it returns a book instance with the updated part. + +These methods can be used in combinaison with a file watch to avoid parsing the whole books when a file is modified. + +## Manipulation + +### `GitBook.SummaryModifier.toText` +`GitBook.SummaryModifier.toText(summary: Summary, fileExt: String?): String` + +Serialize the summary as a string, the argument `fileExt` can be used to specify the parser: + +```js +const textDefault = GitBook.SummaryModifier.toText(summary); +const textAdoc = GitBook.SummaryModifier.toText(summary, '.adoc'); +const textAdoc = GitBook.SummaryModifier.toText(summary, '.md'); +``` + +### `GitBook.SummaryModifier.insertArticle` +`GitBook.SummaryModifier.insertArticle(summary: Summary, article: Article, level: String): Summary` + +Insert a summary `article` after the article identified with `level`. + +### `GitBook.SummaryModifier.unshiftArticle` +`GitBook.SummaryModifier.unshiftArticle(summary: Summary, article: Article): Summary` + +Insert a summary `article` at the beginning of it. + +### `GitBook.SummaryModifier.removeArticle` +`GitBook.SummaryModifier.removeArticle(summary: Summary, article: Article|String): Summary` + +Remove an article from the summary. + +## Generation + +### `GitBook.Output.getGenerator` +`GitBook.Output.getGenerator(type: String): Generator` + +Return a generator, `type` may be one of `["website", "json", "ebook"]`. + +### `GitBook.Output.generate` +`GitBook.Output.generate(generator: Generator, book: Book): Output` + +Generate a book using a generator. diff --git a/docs/internals/browser.md b/docs/internals/browser.md new file mode 100644 index 0000000..1ea51bb --- /dev/null +++ b/docs/internals/browser.md @@ -0,0 +1,37 @@ +# Parsing GitBook in the browser + +The GitBook core API can be integrated in applications running in a browser environment (web application or Electon). One good example is the official [GitBook Editor](https://www.gitbook.com/editor), built using Electon and the GitBook core library. + +The `gitbook` package can be imported during a browserify/webpack build. Only the parsing components will be bundled. **Generating an output** is not possible on a browser environment. + +### Mocking the filesystem + +Since books are a composition of multiple files in a directory, the parsing requires some kind of filesystem interface. + +On a node.js environment, GitBook provides a method to create the right interface: `GitBook.createNodeFS(bookFolder: String): FS`. + +On a browser application, the interface depends mostly on your application design. + +```js + +const appFS = GitBook.FS.create({ + // (String): Boolean + fsExists(filePath) { + ... + }, + // (String): Buffer + fsReadFile(filePath) { + ... + }, + // (String): { mtime: Date } + fsStatFile(filePath) { + ... + }, + // (String): Array<String> + fsReadDir(filePath) { + ... + } +}); +``` + +[Checkout the core FS interfaces](https://github.com/GitbookIO/gitbook/tree/master/packages/gitbook/src/fs) for greater example. |