summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/lib/page.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-06-29 13:42:12 +0200
committerSamy Pessé <samypesse@gmail.com>2016-12-22 15:00:59 +0100
commitba25a60e913466cee035a63752380baba32229a6 (patch)
tree74404023d2b60e12d4287e527dce0f653c35ca24 /packages/gitbook-markdown/lib/page.js
parentc619a1a742fa73544c3dbe897f5cb544c270dea9 (diff)
downloadgitbook-ba25a60e913466cee035a63752380baba32229a6.zip
gitbook-ba25a60e913466cee035a63752380baba32229a6.tar.gz
gitbook-ba25a60e913466cee035a63752380baba32229a6.tar.bz2
Start switching to markup-it
Diffstat (limited to 'packages/gitbook-markdown/lib/page.js')
-rw-r--r--packages/gitbook-markdown/lib/page.js75
1 files changed, 29 insertions, 46 deletions
diff --git a/packages/gitbook-markdown/lib/page.js b/packages/gitbook-markdown/lib/page.js
index 5ea7c21..12848ed 100644
--- a/packages/gitbook-markdown/lib/page.js
+++ b/packages/gitbook-markdown/lib/page.js
@@ -1,73 +1,56 @@
var _ = require('lodash');
-var kramed = require('kramed');
-var annotate = require('kramed/lib/annotate/');
+var MarkupIt = require('markup-it');
+var gitbookSyntax = require('markup-it/syntaxes/gitbook');
var RAW_START = '{% raw %}';
-var RAW_END = '{% endraw %}';
+var RAW_END = '{% endraw %}';
+var gitbook = new MarkupIt(gitbookSyntax);
/**
- Escape a code block's content using raw blocks
-
- @param {String}
- @return {String}
-*/
+ * Escape a code block's content using raw blocks
+ *
+ * @param {String}
+ * @return {String}
+ */
function escape(str) {
return RAW_START + str + RAW_END;
}
-/**
- Combines annotated nodes
-
- @param {Array}
- @return {String}
-*/
-function combine(nodes) {
- return _.map(nodes, 'raw').join('');
-}
/**
- Add templating "raw" to code blocks to
- avoid nunjucks processing their content.
-
- @param {String} src
- @return {String}
-*/
+ * Add templating "raw" to code blocks to
+ * avoid nunjucks processing their content.
+ *
+ * @param {String} src
+ * @return {String}
+ */
function preparePage(src) {
- var lexed = annotate.blocks(src);
var levelRaw = 0;
- function escapeCodeElement(el) {
- if (el.type == 'code' && levelRaw == 0) {
- el.raw = escape(el.raw);
- } else if (el.type == 'tplexpr') {
- var expr = el.matches[0];
- if(expr === 'raw') {
+ return gitbook.annotate(src, function(token) {
+ var tokenType = token.getType();
+
+ if (tokenType === MarkupIt.BLOCKS.TEMPLATE && tokenType === MarkupIt.STYLES.TEMPLATE) {
+ var expr = token.getData().get('type');
+ if (expr === 'raw') {
levelRaw = levelRaw + 1;
- } else if(expr === 'endraw') {
+ } else if (expr == 'endraw') {
levelRaw = 0;
}
+
+ return true;
}
- return el;
- }
- var escaped = _.map(lexed, function(el) {
- // Only escape paragraphs and headings
- if(el.type == 'paragraph' || el.type == 'heading') {
- var line = annotate.inline(el.raw);
+ if (token.isBlock() && )
- // Escape inline code blocks
- line = line.map(escapeCodeElement);
- // Change raw source code
- el.raw = combine(line);
- return el;
- } else {
- return escapeCodeElement(el);
+ if (tokenType !== MarkupIt.BLOCKS.CODE && tokenType !== MarkupIt.STYLES.CODE && levelRaw == 0) {
+ return;
}
- });
- return combine(escaped);
+ return escape(token.getRaw());
+ });
}
module.exports = {