From ff1b8976d2e9f3148ecc6a0f6ba5f8165e9f432a Mon Sep 17 00:00:00 2001 From: Ken Hawkins <khawkins98@gmail.com> Date: Wed, 17 Apr 2019 14:44:59 +0200 Subject: [PATCH] Cleanup --- fractal.js | 2 + tools/vf-frctl-extensions/markdown.js | 16 --- .../vf-frctl-theme/src/extensions/markdown.js | 105 +++++++++--------- 3 files changed, 55 insertions(+), 68 deletions(-) diff --git a/fractal.js b/fractal.js index a52e16deb..1155fbedc 100644 --- a/fractal.js +++ b/fractal.js @@ -30,7 +30,9 @@ module.exports = { }, paths: ["./components"], filters: { + // {{ "## Parse me" | marked }} marked: function(string) { + const renderMarkdown = require('marked'); return renderMarkdown(string); }, // A filter and non-async version of frctl's context extension from diff --git a/tools/vf-frctl-extensions/markdown.js b/tools/vf-frctl-extensions/markdown.js index 8f3505201..c82341843 100644 --- a/tools/vf-frctl-extensions/markdown.js +++ b/tools/vf-frctl-extensions/markdown.js @@ -83,19 +83,3 @@ module.exports = function(fractal){ return new MarkdownExtenstion(); }; - - - -/* -'use strict'; - -const renderMarkdown = require('marked'); -const nunjucks = require('nunjucks'); - - -module.exports = function(theme, env, app, fractal){ - - - - }; -*/ diff --git a/tools/vf-frctl-theme/src/extensions/markdown.js b/tools/vf-frctl-theme/src/extensions/markdown.js index 449c110bb..2f61e8b8c 100644 --- a/tools/vf-frctl-theme/src/extensions/markdown.js +++ b/tools/vf-frctl-theme/src/extensions/markdown.js @@ -14,73 +14,74 @@ Confused? Rule of thumb: if you use a component to make other components, name i </div> */ +// Note: The structure of this file's paramaters are slightly different so we can't easily reuse the /tools/vf-frctl-extensions/markdown.js + module.exports = function(theme, env, app, fractal){ - // port of https://github.com/zephraph/nunjucks-markdown - // Licence: https://github.com/zephraph/nunjucks-markdown/blob/master/LICENSE - function MarkdownExtenstion() { + // port of https://github.com/zephraph/nunjucks-markdown + // Licence: https://github.com/zephraph/nunjucks-markdown/blob/master/LICENSE + function MarkdownExtenstion() { - this.tags = ['markdown']; + this.tags = ['markdown']; - this.parse = function(parser, nodes, lexer) { - var tok = parser.nextToken(); + this.parse = function(parser, nodes, lexer) { + var tok = parser.nextToken(); - // Parse the markdown tag and collect any arguments - var args = parser.parseSignature(null, true); - parser.advanceAfterBlockEnd(tok.value); + // Parse the markdown tag and collect any arguments + var args = parser.parseSignature(null, true); + parser.advanceAfterBlockEnd(tok.value); - // If arguments, return the fileTag constructed node - if(args.children.length > 0) - return new nodes.CallExtension(this, 'fileTag', args); + // If arguments, return the fileTag constructed node + if(args.children.length > 0) + return new nodes.CallExtension(this, 'fileTag', args); - // Otherwise parse until the close block and move the parser to the next position - var body = parser.parseUntilBlocks('endmarkdown'); + // Otherwise parse until the close block and move the parser to the next position + var body = parser.parseUntilBlocks('endmarkdown'); - // I found Nunjucks to be incredibly convoluted on how to just get some data into the BlockTag function, - // this finally worked by faking another template node. - var tabStart = new nodes.NodeList(0, 0, [new nodes.Output(0, 0, [new nodes.TemplateData(0, 0, (tok.colno - 1))])]); + // I found Nunjucks to be incredibly convoluted on how to just get some data into the BlockTag function, + // this finally worked by faking another template node. + var tabStart = new nodes.NodeList(0, 0, [new nodes.Output(0, 0, [new nodes.TemplateData(0, 0, (tok.colno - 1))])]); - parser.advanceAfterBlockEnd(); + parser.advanceAfterBlockEnd(); - // Return the constructed blockTag node - return new nodes.CallExtension(this, 'run', args, [body, tabStart]); - }; + // Return the constructed blockTag node + return new nodes.CallExtension(this, 'run', args, [body, tabStart]); + }; - // Markdown rendering for the file tag. Use the nunjucks.render function to render - // the actual contents of the file. Pass the results through the markdown renderer. - this.fileTag = function(environment, file) { - return new nunjucks.runtime.SafeString(renderMarkdown(env.render(file, environment.ctx))); - } + // Markdown rendering for the file tag. Use the nunjucks.render function to render + // the actual contents of the file. Pass the results through the markdown renderer. + this.fileTag = function(environment, file) { + return new nunjucks.runtime.SafeString(renderMarkdown(env.render(file, environment.ctx))); + } - // Markdown rendering for the block. Pretty simple, just get the body text and pass - // it through the markdown renderer. - this.run = function(context, body, tabStart) { - - var body = body(); - var spacesRegex = /^[\s]+/; - var tabStart = tabStart(); // The column postion of the {% markdown %} tag. - - if (tabStart > 0) { // If the {% markdown %} tag is tabbed in, normalize the content to the same depth. - body = body.split(/\r?\n/); // Split into lines. - body = body.map(function(line) { - var startSpaces = line.match(spacesRegex); - if (startSpaces && startSpaces[0].length >= tabStart) { // If the content is not at the same or greater tab depth, do nothing.. - return line.slice(tabStart); // Subtract the column postion from the start of the string. - } else if (startSpaces) { - return line.slice(startSpaces[0].length); - } else { - return line; - } - }); - body = body.join("\n"); // Rejoin into one string. - } - - return new nunjucks.runtime.SafeString(renderMarkdown(body)); + // Markdown rendering for the block. Pretty simple, just get the body text and pass + // it through the markdown renderer. + this.run = function(context, body, tabStart) { + + var body = body(); + var spacesRegex = /^[\s]+/; + var tabStart = tabStart(); // The column postion of the {% markdown %} tag. + + if (tabStart > 0) { // If the {% markdown %} tag is tabbed in, normalize the content to the same depth. + body = body.split(/\r?\n/); // Split into lines. + body = body.map(function(line) { + var startSpaces = line.match(spacesRegex); + if (startSpaces && startSpaces[0].length >= tabStart) { // If the content is not at the same or greater tab depth, do nothing.. + return line.slice(tabStart); // Subtract the column postion from the start of the string. + } else if (startSpaces) { + return line.slice(startSpaces[0].length); + } else { + return line; + } + }); + body = body.join("\n"); // Rejoin into one string. } + return new nunjucks.runtime.SafeString(renderMarkdown(body)); } - env.engine.addExtension('markdown', new MarkdownExtenstion()); + } + env.engine.addExtension('markdown', new MarkdownExtenstion()); - }; +}; -- GitLab