Skip to content
Snippets Groups Projects
Commit ff1b8976 authored by Ken Hawkins's avatar Ken Hawkins
Browse files

Cleanup

parent 330b1a9b
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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){
};
*/
......@@ -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());
};
};
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment