diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/gitbook-core/src/components/PageContent.js | 94 | ||||
-rw-r--r-- | packages/gitbook-core/src/index.js | 4 | ||||
-rw-r--r-- | packages/gitbook-plugin-theme-default/src/components/Page.js | 6 | ||||
-rw-r--r-- | packages/gitbook/package.json | 1 | ||||
-rw-r--r-- | packages/gitbook/src/blocks/include.js | 29 | ||||
-rw-r--r-- | packages/gitbook/src/parsers/annotateCodeBlocks.js | 130 | ||||
-rw-r--r-- | packages/gitbook/src/parsers/asciidoc.js | 10 | ||||
-rw-r--r-- | packages/gitbook/src/parsers/markdown.js | 13 | ||||
-rw-r--r-- | packages/gitbook/src/utils/error.js | 8 |
9 files changed, 130 insertions, 165 deletions
diff --git a/packages/gitbook-core/src/components/PageContent.js b/packages/gitbook-core/src/components/PageContent.js new file mode 100644 index 0000000..b0998ef --- /dev/null +++ b/packages/gitbook-core/src/components/PageContent.js @@ -0,0 +1,94 @@ +const React = require('react'); +const ImmutablePropTypes = require('react-immutable-proptypes'); + +const NODES = { + // Blocks + heading_one: props => <h1>{props.children}</h1>, + heading_two: props => <h2>{props.children}</h2>, + heading_three: props => <h3>{props.children}</h3>, + heading_four: props => <h4>{props.children}</h4>, + heading_five: props => <h5>{props.children}</h5>, + heading_six: props => <h6>{props.children}</h6>, + paragraph: props => <p>{props.children}</p> +}; + +const MARKS = { + BOLD: props => <b>{props.children}</b>, + ITALIC: props => <em>{props.children}</em>, + STRIKETHROUGH: props => <del>{props.children}</del>, + CODE: props => <code>{props.children}</code> +}; + + +/** + * Render a text node. + * @type {ReactClass} + */ +const Text = React.createClass({ + propTypes: { + node: React.PropTypes.object.isRequired + }, + + render() { + const { node } = this.props; + const ranges = node.get('ranges'); + + return ( + <div> + + </div> + ); + } +}); + +/** + * Inner content of a page. + * @type {ReactClass} + */ +const Node = React.createClass({ + propTypes: { + node: React.PropTypes.object.isRequired + }, + + render() { + const { node } = this.props; + + const children = node + .get('children') + .map((child) => { + if (child.kind == 'text') { + return <Text node={child} />; + } else { + return <Node node={child} />; + } + }) + .toArray(); + + return React.createElement( + NODES[node.type], + { + node + }, + children + ); + } +}); + +/** + * Inner content of a page. + * @type {ReactClass} + */ +const PageContent = React.createClass({ + propTypes: { + document: React.PropTypes.object.isRequired + }, + + render() { + const { document } = this.props; + return ( + <Node node={document} /> + ); + } +}); + +module.exports = PageContent; diff --git a/packages/gitbook-core/src/index.js b/packages/gitbook-core/src/index.js index 5b92bc3..e8c1a5b 100644 --- a/packages/gitbook-core/src/index.js +++ b/packages/gitbook-core/src/index.js @@ -10,7 +10,7 @@ const { Flex, Box } = require('reflexbox'); const { InjectedComponent, InjectedComponentSet } = require('./components/InjectedComponent'); const { ImportLink, ImportScript, ImportCSS } = require('./components/Import'); -const HTMLContent = require('./components/HTMLContent'); +const PageContent = require('./components/PageContent'); const Link = require('./components/Link'); const Image = require('./components/Image'); const Icon = require('./components/Icon'); @@ -47,7 +47,7 @@ module.exports = { I18nProvider, InjectedComponent, InjectedComponentSet, - HTMLContent, + PageContent, Head, Panel, Provider, diff --git a/packages/gitbook-plugin-theme-default/src/components/Page.js b/packages/gitbook-plugin-theme-default/src/components/Page.js index cbce704..5ad8901 100644 --- a/packages/gitbook-plugin-theme-default/src/components/Page.js +++ b/packages/gitbook-plugin-theme-default/src/components/Page.js @@ -1,6 +1,10 @@ const GitBook = require('gitbook-core'); const { React } = GitBook; +/** + * Container for the page. + * @type {ReactClass} + */ const Page = React.createClass({ propTypes: { page: GitBook.PropTypes.Page @@ -16,7 +20,7 @@ const Page = React.createClass({ <GitBook.InjectedComponentSet matching={{ role: 'page:header' }} props={this.props} /> <GitBook.InjectedComponent matching={{ role: 'page:container' }} props={this.props}> - <GitBook.HTMLContent html={page.content} /> + <GitBook.PageContent document={page.document} /> </GitBook.InjectedComponent> <GitBook.InjectedComponentSet matching={{ role: 'page:footer' }} props={this.props} /> diff --git a/packages/gitbook/package.json b/packages/gitbook/package.json index 0ecbf21..b5eed9f 100644 --- a/packages/gitbook/package.json +++ b/packages/gitbook/package.json @@ -47,7 +47,6 @@ "mkdirp": "0.5.1", "moment": "2.13.0", "npm": "3.10.9", - "nunjucks": "2.5.2", "object-path": "^0.9.2", "omit-keys": "^0.1.0", "open": "0.0.5", diff --git a/packages/gitbook/src/blocks/include.js b/packages/gitbook/src/blocks/include.js new file mode 100644 index 0000000..55cd06e --- /dev/null +++ b/packages/gitbook/src/blocks/include.js @@ -0,0 +1,29 @@ +const Git = require('../utils/git'); + +/** + * Fetch a reference "ref" relative to "file". + * @param {Book} book + * @param {File} file + * @param {String} ref + * @return {Promise<Slate.Fragment>} + */ +function fetchReference(book, file, ref) { + +} + +/** + * Include another file. It replaces the templating node with content from the other file. + * + * @param {Slate.Block} node + * @return {Promise<List<Block>>} + */ +function include(book, file, node) { + const ref = node.data.get('props').get(0); + + return fetchReference(book, file, ref) + .then((fragment) => { + + }); +} + +module.exports = include; diff --git a/packages/gitbook/src/parsers/annotateCodeBlocks.js b/packages/gitbook/src/parsers/annotateCodeBlocks.js deleted file mode 100644 index 92ce83a..0000000 --- a/packages/gitbook/src/parsers/annotateCodeBlocks.js +++ /dev/null @@ -1,130 +0,0 @@ -const { Block, Text, Inline, INLINES, BLOCKS, MARKS } = require('markup-it'); - -const RAW_START = 'raw'; -const RAW_END = 'endraw'; - -/** - * Create a templating node. - * @param {String} expr - * @return {Node} - */ -function createTemplatingNode(expr) { - return Inline.create({ - type: INLINES.TEMPLATE, - data: { - type: 'expr', - text: expr - } - }); -} - -/** - * Escape a code block. - * @param {Block} block - * @return {Array<Node>} blocks - */ -function escapeCodeBlock(block) { - return [ - Block.create({ - type: BLOCKS.TEXT, - nodes: [ - createTemplatingNode(RAW_START) - ] - }), - block, - Block.create({ - type: BLOCKS.TEXT, - nodes: [ - createTemplatingNode(RAW_END) - ] - }) - ]; -} - - -/** - * Escape a text node. - * @param {Text} node - * @return {Array<Node>} nodes - */ -function escapeTextNode(node) { - const ranges = node.getRanges(); - - const nodes = ranges.reduce((result, range) => { - const hasCode = range.marks.some(mark => mark.type == MARKS.CODE); - const text = Text.createFromRanges([ range ]); - - if (hasCode) { - return result.concat([ - createTemplatingNode(RAW_START), - text, - createTemplatingNode(RAW_END) - ]); - } - - return result.concat([ text ]); - }, []); - - return nodes; -} - -/** - * Annotate a block container. - * @param {Node} parent - * @param {Number} levelRaw - * @return {Node} node - * @return {Number} levelRaw - */ -function annotateNode(parent, levelRaw) { - let { nodes } = parent; - - nodes = nodes.reduce((out, node) => { - if (node.type === INLINES.TEMPLATE) { - const { type, text } = node.data.toJS(); - - if (type === 'expr') { - if (text === 'raw') { - levelRaw = levelRaw + 1; - } else if (text == 'endraw') { - levelRaw = 0; - } - } - - return out.concat([ node ]); - } - - else if (node.type === BLOCKS.CODE) { - return out.concat( - levelRaw == 0 ? escapeCodeBlock(node) : [ node ] - ); - } - - else if (node.kind == 'text') { - return out.concat( - levelRaw == 0 ? escapeTextNode(node) : [ node ] - ); - } - - const result = annotateNode(node, levelRaw); - levelRaw = result.levelRaw; - return out.concat([result.node]); - }, []); - - return { - levelRaw, - node: parent.merge({ nodes }) - }; -} - -/** - * Add templating "raw" to code blocks to - * avoid nunjucks processing their content. - * - * @param {Document} document - * @return {Document} - */ -function annotateCodeBlocks(document) { - return annotateNode(document, 0).node; -} - -module.exports = annotateCodeBlocks; diff --git a/packages/gitbook/src/parsers/asciidoc.js b/packages/gitbook/src/parsers/asciidoc.js index 7425f65..830a834 100644 --- a/packages/gitbook/src/parsers/asciidoc.js +++ b/packages/gitbook/src/parsers/asciidoc.js @@ -41,15 +41,6 @@ function toHTML(text) { } /** - * Prepare a document for parsing - * @param {String} text - * @return {String} text - */ -function prepare(text) { - return text; -} - -/** * Render asciidoc to inline HTML. * @param {String} text * @return {String} html @@ -64,7 +55,6 @@ function toInlineHTML(text) { module.exports = { name: 'asciidoc', FILE_EXTENSIONS, - prepare, toDocument, toText, toHTML, diff --git a/packages/gitbook/src/parsers/markdown.js b/packages/gitbook/src/parsers/markdown.js index e110eb7..1dff741 100644 --- a/packages/gitbook/src/parsers/markdown.js +++ b/packages/gitbook/src/parsers/markdown.js @@ -1,7 +1,6 @@ const { State } = require('markup-it'); const markdown = require('markup-it/lib/markdown'); const html = require('markup-it/lib/html'); -const annotateCodeBlocks = require('./annotateCodeBlocks'); const FILE_EXTENSIONS = [ '.md', @@ -30,17 +29,6 @@ function toDocument(text) { } /** - * Prepare a document for parsing - * @param {String} text - * @return {String} text - */ -function prepare(text) { - let doc = toDocument(text); - doc = annotateCodeBlocks(doc); - return toText(doc); -} - -/** * Render markdown to HTML. * @param {String} text * @return {String} html @@ -67,7 +55,6 @@ function toInlineHTML(text) { module.exports = { name: 'markdown', FILE_EXTENSIONS, - prepare, toText, toDocument, toHTML, diff --git a/packages/gitbook/src/utils/error.js b/packages/gitbook/src/utils/error.js index 925b5ff..2d65b17 100644 --- a/packages/gitbook/src/utils/error.js +++ b/packages/gitbook/src/utils/error.js @@ -55,13 +55,6 @@ const RequireInstallError = TypedError({ }); // Error for nunjucks templates -const TemplateError = WrappedError({ - message: 'Error compiling template "{filename}": {origMessage}', - type: 'template', - filename: null -}); - -// Error for nunjucks templates const PluginError = WrappedError({ message: 'Error with plugin "{plugin}": {origMessage}', type: 'plugin', @@ -92,7 +85,6 @@ module.exports = { FileNotFoundError, FileOutOfScopeError, - TemplateError, PluginError, ConfigurationError, EbookError |