Skip to content

add root options #2

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

Merged
merged 1 commit into from
Nov 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ posthtml()

## Options

__encoding__: default `utf-8`
__root__: Root folder path for include. Default `./`

__encoding__: Default `utf-8`
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var toTree = require('posthtml/lib/parser').toTree;
var parser = require('posthtml-parser');
var fs = require('fs');
var path = require('path');

module.exports = function posthtmlInclude(options) {
options = options || {};
options.root = options.root || './';
options.encoding = options.encoding || 'utf-8';

return function(tree) {
tree.match({ tag: 'include' }, function(node) {
var src = node.attrs.src || false,
content;
if (src) {
content = toTree(fs.readFileSync(path.resolve(src), options.encoding || 'utf-8'));
}
if (src) content = parser(fs.readFileSync(path.resolve(options.root, src), options.encoding));
return {
tag: false,
content: content
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Include file in HTML",
"main": "index.js",
"dependencies": {
"posthtml": "^0.3.0"
"posthtml-parser": "^0.1.1"
},
"devDependencies": {
"chai": "^3.2.0",
Expand Down
18 changes: 12 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ var plugin = require('..');
var posthtml = require('posthtml');
var expect = require('chai').expect;

var HTMLINCLUDE = '<html><head><title>Test</title></head><body><include src="./test/blocks/button/button.html"></body></html>',
HTML = '<html><head><title>Test</title></head><body><div class="button"><div class="button__text">Text</div></div>\n</body></html>';

function test(input, output, options, done) {
posthtml()
.use(plugin(options))
Expand All @@ -19,12 +16,21 @@ function test(input, output, options, done) {
}

describe('Simple test', function() {
it('include html', function(done) {
it('default include html', function(done) {
test(
HTMLINCLUDE,
HTML,
'<html><head><title>Test</title></head><body><include src="./test/blocks/button/button.html"></body></html>',
'<html><head><title>Test</title></head><body><div class="button"><div class="button__text">Text</div></div>\n</body></html>',
{ encoding: 'utf-8' },
done
);
});

it('root options', function(done) {
test(
'<include src="./button/button.html">',
'<div class="button"><div class="button__text">Text</div></div>\n',
{ root: './test/blocks/' },
done
);
});
});