Skip to content

Commit 59a2688

Browse files
Apply plugins to tree, tested with include
1 parent e0ab977 commit 59a2688

7 files changed

+70
-9
lines changed

package-lock.json

+28-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"markdown-it-toc-done-right": "^4.2.0",
4646
"posthtml": "^0.16.6",
4747
"posthtml-beautify": "^0.7.0",
48+
"posthtml-include": "^1.7.4",
4849
"posthtml-markdownit": "^1.3.0",
4950
"xo": "^0.37.1"
5051
}

src/index.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ module.exports = (options = {}) => tree => {
6565
}
6666

6767
options.roots = Array.isArray(options.folders) ? options.folders : [options.folders];
68-
// options.roots.forEach((root, index) => {
69-
// options.roots[index] = path.resolve(options.root, root);
70-
// });
7168
options.namespaces = Array.isArray(options.namespaces) ? options.namespaces : [options.namespaces];
7269
options.namespaces.forEach((namespace, index) => {
7370
options.namespaces[index].root = path.resolve(namespace.root);
@@ -109,6 +106,10 @@ function processTree(options) {
109106
let processCounter = 0;
110107

111108
return function (tree) {
109+
if (options.plugins.length > 0) {
110+
tree = applyPluginsToTree(tree, options.plugins);
111+
}
112+
112113
match.call(tree, options.matcher, currentNode => {
113114
if (!currentNode.attrs) {
114115
currentNode.attrs = {};
@@ -149,6 +150,10 @@ function processTree(options) {
149150
// const plugins = [...options.plugins, expressions(options.expressions)];
150151
nextNode = expressions(options.expressions)(nextNode);
151152

153+
if (options.plugins.length > 0) {
154+
nextNode = applyPluginsToTree(nextNode, options.plugins);
155+
}
156+
152157
// Process <yield> tag
153158
const content = match.call(nextNode, {tag: options.yield}, nextNode => {
154159
// Fill <yield> with current node content or default <yield> content or empty
@@ -183,3 +188,10 @@ function processTree(options) {
183188
return tree;
184189
};
185190
}
191+
192+
function applyPluginsToTree(tree, plugins) {
193+
return plugins.reduce((tree, plugin) => {
194+
tree = plugin(tree);
195+
return tree;
196+
}, tree);
197+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div><include src="components/included-file.html"></include></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<module href="/components/module.html"><p>Module file</p></module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Included file</p>

test/test-plugins.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict';
22

3-
// const test = require('ava');
4-
// const plugin = require('../src');
5-
// const posthtml = require('posthtml');
6-
// const clean = html => html.replace(/(\n|\t)/g, '').trim();
3+
const test = require('ava');
4+
const plugin = require('../src');
5+
const posthtml = require('posthtml');
6+
const clean = html => html.replace(/(\n|\t)/g, '').trim();
77

88
// test('Must work with posthtml-extend syntax', async t => {
99
// const actual = `<extends src="layouts/extend.html"><block name="content">My Content</block></extends>`;
@@ -31,3 +31,22 @@
3131
//
3232
// t.is(html, expected);
3333
// });
34+
35+
test('Must include file using posthtml-include', async t => {
36+
const actual = `<component src="components/component-with-include.html"></component>`;
37+
const expected = `<div><p>Included file</p></div>`;
38+
39+
const html = await posthtml([plugin({root: './test/templates', tag: 'component', attribute: 'src', plugins: [require('posthtml-include')({root: './test/templates', encoding: 'utf8'})]})]).process(actual).then(result => clean(result.html));
40+
41+
t.is(html, expected);
42+
});
43+
44+
// Unfortunately it's not working
45+
// test('Must include file using posthtml-modules', async t => {
46+
// const actual = `<component src="components/component-with-module.html"></component>`;
47+
// const expected = `<div><p>Module file</p></div>`;
48+
//
49+
// const html = await posthtml([plugin({root: './test/templates', tag: 'component', attribute: 'src', plugins: [require('posthtml-modules')({root: './test/templates'})]})]).process(actual).then(result => clean(result.html));
50+
//
51+
// t.is(html, expected);
52+
// });

0 commit comments

Comments
 (0)