summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/src/page.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-12-22 15:51:59 +0100
committerSamy Pessé <samypesse@gmail.com>2016-12-22 15:51:59 +0100
commitc4e512477e3cbe1344caaa2f1cc56e4bb402ad79 (patch)
treeca43a054bf84a49b48c942b754153b5459eed3ee /packages/gitbook-markdown/src/page.js
parent6e0fd5d5d44fc2c97e075c4bbff188a0a7e797c1 (diff)
downloadgitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.zip
gitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.tar.gz
gitbook-c4e512477e3cbe1344caaa2f1cc56e4bb402ad79.tar.bz2
Import gitbook-markdown
Diffstat (limited to 'packages/gitbook-markdown/src/page.js')
-rw-r--r--packages/gitbook-markdown/src/page.js74
1 files changed, 74 insertions, 0 deletions
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
+};