Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filters to read non-text include files #3213

Merged
merged 10 commits into from May 19, 2020
16 changes: 12 additions & 4 deletions packages/pug-filters/lib/handle-filters.js
Expand Up @@ -24,9 +24,13 @@ function handleFilters(ast, filters, options, filterAliases) {
var firstFilter = node.filters.pop();
var attrs = getAttributes(firstFilter, options);
var filename = (attrs.filename = node.file.fullPath);
var str = node.file.str;
node.type = 'Text';
node.val = filterFileWithFallback(firstFilter, filename, str, attrs);
node.val = filterFileWithFallback(
firstFilter,
filename,
node.file,
attrs
);
node.filters
.slice()
.reverse()
Expand Down Expand Up @@ -55,10 +59,14 @@ function handleFilters(ast, filters, options, filterAliases) {
}
}

function filterFileWithFallback(filter, filename, text, attrs) {
function filterFileWithFallback(filter, filename, file, attrs) {
var filterName = getFilterName(filter);
if (filters && filters[filterName]) {
return filters[filterName](text, attrs);
if (filters[filterName].renderBuffer) {
return filters[filterName].renderBuffer(file.raw, attrs);
} else {
return filters[filterName](file.str, attrs);
}
} else {
return filterWithFallback(filter, filename, attrs, 'renderFile');
}
Expand Down
10 changes: 6 additions & 4 deletions packages/pug-load/index.js
Expand Up @@ -21,16 +21,18 @@ function load(ast, options) {
if (file.type !== 'FileReference') {
throw new Error('Expected file.type to be "FileReference"');
}
var path, str;
var path, str, raw;
try {
path = options.resolve(file.path, file.filename, options);
file.fullPath = path;
str = options.read(path, options);
raw = options.read(path, options);
str = raw.toString('utf8');
} catch (ex) {
ex.message += '\n at ' + node.filename + ' line ' + node.line;
throw ex;
}
file.str = str;
file.raw = raw;
if (node.type === 'Extends' || node.type === 'Include') {
file.ast = load.string(
str,
Expand All @@ -56,7 +58,7 @@ load.file = function loadFile(filename, options) {
options = assign(getOptions(options), {
filename: filename,
});
var str = options.read(filename);
var str = options.read(filename).toString('utf8');
return load.string(str, options);
};

Expand All @@ -80,7 +82,7 @@ load.resolve = function resolve(filename, source, options) {
return filename;
};
load.read = function read(filename, options) {
return fs.readFileSync(filename, 'utf8');
return fs.readFileSync(filename);
};

load.validateOptions = function validateOptions(options) {
Expand Down
69 changes: 69 additions & 0 deletions packages/pug-load/test/__snapshots__/index.test.js.snap
Expand Up @@ -29,6 +29,22 @@ Object {
"fullPath": "<dirname>/bar.pug",
"line": 1,
"path": "bar.pug",
"raw": Object {
"data": Array [
98,
108,
111,
99,
107,
32,
98,
105,
110,
103,
10,
],
"type": "Buffer",
},
"str": "block bing
",
"type": "FileReference",
Expand Down Expand Up @@ -99,6 +115,22 @@ Object {
"fullPath": "<dirname>/bing.pug",
"line": 4,
"path": "bing.pug",
"raw": Object {
"data": Array [
46,
98,
105,
110,
103,
32,
98,
111,
110,
103,
10,
],
"type": "Buffer",
},
"str": ".bing bong
",
"type": "FileReference",
Expand All @@ -122,6 +154,43 @@ Object {
"fullPath": "<dirname>/script.js",
"line": 6,
"path": "script.js",
"raw": Object {
"data": Array [
100,
111,
99,
117,
109,
101,
110,
116,
46,
119,
114,
105,
116,
101,
40,
39,
104,
101,
108,
108,
111,
32,
119,
111,
114,
108,
100,
33,
39,
41,
59,
10,
],
"type": "Buffer",
brewingcode marked this conversation as resolved.
Show resolved Hide resolved
},
"str": "document.write('hello world!');
",
"type": "FileReference",
Expand Down