mirror of
https://github.com/marko-js/marko.git
synced 2026-02-01 16:07:13 +00:00
Introducing the <include-html> tag
This commit is contained in:
parent
690c1d1433
commit
da4c341c78
17
taglibs/core/include-html-tag-browser.js
Normal file
17
taglibs/core/include-html-tag-browser.js
Normal file
@ -0,0 +1,17 @@
|
||||
'use strict';
|
||||
module.exports = function codeGenerator(el, codegen) {
|
||||
let argument = el.argument;
|
||||
if (!argument) {
|
||||
return;
|
||||
}
|
||||
|
||||
let builder = codegen.builder;
|
||||
let pathExpression = builder.parseExpression(argument);
|
||||
if (pathExpression.type !== 'Literal' || typeof pathExpression.value !== 'string') {
|
||||
codegen.addError('Argument to the <include-text> tag should be a string value: <include-text("./foo.txt")/>');
|
||||
return;
|
||||
}
|
||||
|
||||
var path = pathExpression.value;
|
||||
return builder.text(builder.literal('<include-html> cannot be compiled in the browser (path="' + path + '")'));
|
||||
};
|
||||
30
taglibs/core/include-html-tag.js
Normal file
30
taglibs/core/include-html-tag.js
Normal file
@ -0,0 +1,30 @@
|
||||
'use strict';
|
||||
|
||||
var resolveFrom = require('resolve-from');
|
||||
var fs = require('fs');
|
||||
|
||||
module.exports = function codeGenerator(el, codegen) {
|
||||
let argument = el.argument;
|
||||
if (!argument) {
|
||||
return;
|
||||
}
|
||||
|
||||
let builder = codegen.builder;
|
||||
let pathExpression = builder.parseExpression(argument);
|
||||
if (pathExpression.type !== 'Literal' || typeof pathExpression.value !== 'string') {
|
||||
codegen.addError('Argument to the <include-html> tag should be a string value: <include-html("./foo.txt")/>');
|
||||
return;
|
||||
}
|
||||
|
||||
var path = pathExpression.value;
|
||||
var dirname = codegen.context.dirname;
|
||||
try {
|
||||
path = resolveFrom(dirname, path);
|
||||
} catch(e) {
|
||||
codegen.addError('File not found: ' + path);
|
||||
return;
|
||||
}
|
||||
|
||||
var txt = fs.readFileSync(path, { encoding: 'utf8' });
|
||||
return builder.text(builder.literal(txt), false /* do not escape since this is HTML*/);
|
||||
};
|
||||
@ -88,9 +88,27 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"<include-html>": {
|
||||
"code-generator": "./include-html-tag",
|
||||
"autocomplete": [
|
||||
{
|
||||
"displayText": "include-html(<path>)",
|
||||
"snippet": "include-html(${1:\"./foo.html\"})",
|
||||
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
|
||||
},
|
||||
{
|
||||
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
|
||||
}
|
||||
]
|
||||
},
|
||||
"<include-text>": {
|
||||
"code-generator": "./include-text-tag",
|
||||
"autocomplete": [
|
||||
{
|
||||
"displayText": "include-text(<path>)",
|
||||
"snippet": "include-text(${1:\"./foo.txt\"})",
|
||||
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
|
||||
},
|
||||
{
|
||||
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
{
|
||||
"browser": {
|
||||
"./include-text-tag.js": "./include-text-tag-browser.js"
|
||||
"./include-text-tag.js": "./include-text-tag-browser.js",
|
||||
"./include-html-tag.js": "./include-html-tag-browser.js"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user