diff options
Diffstat (limited to 'packages/gitbook-markdown')
-rw-r--r-- | packages/gitbook-markdown/package.json | 3 | ||||
-rw-r--r-- | packages/gitbook-markdown/src/page.js | 146 | ||||
-rw-r--r-- | packages/gitbook-markdown/src/toHTML.js | 20 | ||||
-rw-r--r-- | packages/gitbook-markdown/test/glossary.js | 2 | ||||
-rw-r--r-- | packages/gitbook-markdown/test/inline.js | 2 |
5 files changed, 123 insertions, 50 deletions
diff --git a/packages/gitbook-markdown/package.json b/packages/gitbook-markdown/package.json index 7eb48f8..9cb7916 100644 --- a/packages/gitbook-markdown/package.json +++ b/packages/gitbook-markdown/package.json @@ -7,7 +7,8 @@ "dependencies": { "gitbook-html": "4.0.0", "markdown-escape": "^1.0.1", - "markup-it": "3.2.0" + "markup-it": "3.2.0", + "slate": "^0.16.8" }, "scripts": { "test": "mocha --reporter list --bail" diff --git a/packages/gitbook-markdown/src/page.js b/packages/gitbook-markdown/src/page.js index 70da324..5bd20e2 100644 --- a/packages/gitbook-markdown/src/page.js +++ b/packages/gitbook-markdown/src/page.js @@ -1,21 +1,99 @@ -const { State } = require('markup-it'); +const { State, Block, Text, Inline, INLINES, BLOCKS, MARKS } = require('markup-it'); const markdown = require('markup-it/lib/markdown'); -const RAW_START = '{% raw %}'; -const RAW_END = '{% endraw %}'; +const RAW_START = 'raw'; +const RAW_END = 'endraw'; /** - * Escape a code block's content using raw blocks - * - * @param {String} - * @return {String} + * 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 escape(str) { - return RAW_START + str + RAW_END; +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 + * @return {Node} + */ +function annotateNode(parent, levelRaw) { + let { nodes } = parent; + + nodes = nodes.reduce((out, node) => { + if (node.type === BLOCKS.CODE) { + return out.concat(escapeCodeBlock(node)); + } + + else if (node.kind == 'text') { + return out.concat(escapeTextNode(node)); + } + + return out.concat([node]); + }, []); + + return parent.merge({ nodes }); +} + +/** * Add templating "raw" to code blocks to * avoid nunjucks processing their content. * @@ -25,48 +103,30 @@ function escape(str) { function preparePage(src) { let levelRaw = 0; - const fromMD = State.create(markdown); - const document = fromMD.deserializeToDocument(src); + const state = State.create(markdown); + let document = state.deserializeToDocument(src); document = document.mapDescendants((node) => { - - }); - - - const content = markdown.toContent(src, { - math: true, - template: true - }); - - const textMarkdown = markdown.toText(content, { - annotate(state, raw, token) { - const tokenType = token.getType(); - - if (tokenType === MarkupIt.ENTITIES.TEMPLATE) { - const type = token.getData().get('type'); - const expr = token.getAsPlainText(); - - if (type === 'expr') { - if (expr === 'raw') { - levelRaw = levelRaw + 1; - } else if (expr == 'endraw') { - levelRaw = 0; - } + if (node.type === INLINES.TEMPLATE) { + const { type, text } = node.data; + + if (type === 'expr') { + if (text === 'raw') { + levelRaw = levelRaw + 1; + } else if (text == 'endraw') { + levelRaw = 0; } } - if ( - (tokenType === MarkupIt.BLOCKS.CODE || tokenType === MarkupIt.STYLES.CODE) - && levelRaw === 0 - ) { - return escape(raw); - } - - return raw; + return node; + } else if (node.kind !== 'text') { + return annotateNode(node, levelRaw); + } else { + return node; } }); - return textMarkdown; + return state.serializeDocument(document); } module.exports = { diff --git a/packages/gitbook-markdown/src/toHTML.js b/packages/gitbook-markdown/src/toHTML.js index e887e11..e115b71 100644 --- a/packages/gitbook-markdown/src/toHTML.js +++ b/packages/gitbook-markdown/src/toHTML.js @@ -1,4 +1,5 @@ -const { State } = require('markup-it'); +const { State, Block, BLOCKS } = require('markup-it'); +const { Document } = require('slate'); const markdown = require('markup-it/lib/markdown'); const html = require('markup-it/lib/html'); @@ -23,10 +24,21 @@ function convertMdToHTMLBlock(src) { * @return {String} (html) */ function convertMdToHTMLInline(src) { - const content = markdown.toInlineContent(src); - const textHtml = html.toInlineText(content); + const fromMD = State.create(markdown); + const document = fromMD.deserializeToDocument(src); - return textHtml; + // Create a document with a single unstyled node + const newDocument = Document.create({ + nodes: [ + Block.create({ + type: BLOCKS.TEXT, + nodes: document.nodes.get(0).nodes + }) + ] + }); + + const toHTML = State.create(html); + return toHTML.serializeDocument(newDocument); } module.exports = { diff --git a/packages/gitbook-markdown/test/glossary.js b/packages/gitbook-markdown/test/glossary.js index b38a78d..9bf2064 100644 --- a/packages/gitbook-markdown/test/glossary.js +++ b/packages/gitbook-markdown/test/glossary.js @@ -1,6 +1,6 @@ const fs = require('fs'); const path = require('path'); -const expect = require('assert'); +const expect = require('expect'); const glossary = require('../src').glossary; diff --git a/packages/gitbook-markdown/test/inline.js b/packages/gitbook-markdown/test/inline.js index 834fd3f..1556322 100644 --- a/packages/gitbook-markdown/test/inline.js +++ b/packages/gitbook-markdown/test/inline.js @@ -4,6 +4,6 @@ const inline = require('../src').inline; describe('Inline', () => { it('should render inline markdown', () => { const parsed = inline('Hello **World**'); - expect(parsed.content).toBe('Hello <strong>World</strong>'); + expect(parsed.content).toBe('Hello <b>World</b>'); }); }); |