diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-12-22 15:51:59 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-12-22 15:51:59 +0100 |
commit | c4e512477e3cbe1344caaa2f1cc56e4bb402ad79 (patch) | |
tree | ca43a054bf84a49b48c942b754153b5459eed3ee /packages/gitbook-markdown/src | |
parent | 6e0fd5d5d44fc2c97e075c4bbff188a0a7e797c1 (diff) | |
download | gitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.zip gitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.tar.gz gitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.tar.bz2 |
Import gitbook-markdown
Diffstat (limited to 'packages/gitbook-markdown/src')
-rw-r--r-- | packages/gitbook-markdown/src/index.js | 10 | ||||
-rw-r--r-- | packages/gitbook-markdown/src/page.js | 74 | ||||
-rw-r--r-- | packages/gitbook-markdown/src/toHTML.js | 35 | ||||
-rw-r--r-- | packages/gitbook-markdown/src/toMarkdown.js | 56 |
4 files changed, 175 insertions, 0 deletions
diff --git a/packages/gitbook-markdown/src/index.js b/packages/gitbook-markdown/src/index.js new file mode 100644 index 0000000..694d8c8 --- /dev/null +++ b/packages/gitbook-markdown/src/index.js @@ -0,0 +1,10 @@ +const HTMLParser = require('gitbook-html'); + +const toHTML = require('./toHTML'); +const toMarkdown = require('./toMarkdown'); +const page = require('./page'); + +module.exports = HTMLParser.createParser(toHTML, toMarkdown); + +// Add the custom page escaping +module.exports.page.prepare = page.prepare; diff --git a/packages/gitbook-markdown/src/page.js b/packages/gitbook-markdown/src/page.js new file mode 100644 index 0000000..70da324 --- /dev/null +++ b/packages/gitbook-markdown/src/page.js @@ -0,0 +1,74 @@ +const { State } = require('markup-it'); +const markdown = require('markup-it/lib/markdown'); + +const RAW_START = '{% raw %}'; +const RAW_END = '{% endraw %}'; + +/** + * Escape a code block's content using raw blocks + * + * @param {String} + * @return {String} + */ +function escape(str) { + return RAW_START + str + RAW_END; +} + + +/** + * Add templating "raw" to code blocks to + * avoid nunjucks processing their content. + * + * @param {String} src + * @return {String} + */ +function preparePage(src) { + let levelRaw = 0; + + const fromMD = State.create(markdown); + const document = fromMD.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 ( + (tokenType === MarkupIt.BLOCKS.CODE || tokenType === MarkupIt.STYLES.CODE) + && levelRaw === 0 + ) { + return escape(raw); + } + + return raw; + } + }); + + return textMarkdown; +} + +module.exports = { + prepare: preparePage +}; diff --git a/packages/gitbook-markdown/src/toHTML.js b/packages/gitbook-markdown/src/toHTML.js new file mode 100644 index 0000000..e887e11 --- /dev/null +++ b/packages/gitbook-markdown/src/toHTML.js @@ -0,0 +1,35 @@ +const { State } = require('markup-it'); +const markdown = require('markup-it/lib/markdown'); +const html = require('markup-it/lib/html'); + +/** + * Convert Markdown block to HTML + * + * @param {String} src (markdown) + * @return {String} (html) + */ +function convertMdToHTMLBlock(src) { + const fromMD = State.create(markdown); + const document = fromMD.deserializeToDocument(src); + + const toHTML = State.create(html); + return toHTML.serializeDocument(document); +} + +/** + * Convert Markdown inline to HTML + * + * @param {String} src (markdown) + * @return {String} (html) + */ +function convertMdToHTMLInline(src) { + const content = markdown.toInlineContent(src); + const textHtml = html.toInlineText(content); + + return textHtml; +} + +module.exports = { + block: convertMdToHTMLBlock, + inline: convertMdToHTMLInline +}; diff --git a/packages/gitbook-markdown/src/toMarkdown.js b/packages/gitbook-markdown/src/toMarkdown.js new file mode 100644 index 0000000..5b8deee --- /dev/null +++ b/packages/gitbook-markdown/src/toMarkdown.js @@ -0,0 +1,56 @@ +const escape = require('markdown-escape'); + +// Return N time a string +function ns(s, n) { + return Array(n + 1).join(s); +} + +/* + * This module provides markup rules for gitbook-html + * These rules are being used to generate SUMMARY/GLOSSARY/LANGS + */ +module.exports = { + onText(text) { + return escape(text); + }, + + onTitleStart(level) { + return ns('#', level) + ' '; + }, + onTitleEnd(level) { + return this.onBL(); + }, + + onParagraphStart() { + return this.onSection(); + }, + onParagraphEnd() { + return this.onSection(); + }, + + onLinkStart() { + return '['; + }, + onLinkEnd(href) { + return '](' + href + ')'; + }, + + onListStart(level) { + return ''; + }, + onListEnd() { + return ''; + }, + + onListItemStart(level) { + return ns(' ', level * 4) + '* '; + }, + onListItemEnd() { + return ''; + }, + + onHR() { + return '-----'; + } +}; + |