Skip to content

Commit c07e768

Browse files
authored
Merge pull request #38 from cossssmin/refactor-2020
refactor: code refresh
2 parents 45330d2 + fdce2d4 commit c07e768

26 files changed

Lines changed: 8176 additions & 1850 deletions

.editorconfig

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
# This file is for unifying the coding style for different editors and IDEs.
2-
# More information at http://EditorConfig.org
3-
4-
# No .editorconfig files above the root directory
51
root = true
62

73
[*]
84
charset = utf-8
9-
indent_size = 4
10-
end_of_line = lf
5+
indent_size = 2
116
indent_style = space
127
trim_trailing_whitespace = true
138
insert_final_newline = true
149

15-
[*.{bemjson.js,deps.js}]
16-
indent_size = 4
17-
18-
[{bower,package}.json]
19-
indent_size = 2
20-
2110
[*.md]
2211
trim_trailing_whitespace = false

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
2-
npm-debug.log
32
coverage
3+
.nyc_output
4+
*.log

.jscsrc

Lines changed: 0 additions & 26 deletions
This file was deleted.

.jshintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.jshintrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

.npmignore

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict'
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const posthtml = require('posthtml');
6+
const parser = require('posthtml-parser');
7+
const {match} = require('posthtml/lib/api');
8+
const expressions = require('posthtml-expressions');
9+
10+
module.exports = (options = {}) => {
11+
options.root = options.root || './';
12+
options.encoding = options.encoding || 'utf-8';
13+
14+
return function posthtmlInclude(tree) {
15+
tree.parser = tree.parser || parser;
16+
tree.match = tree.match || match;
17+
18+
tree.match({tag: 'include'}, node => {
19+
let src = node.attrs.src || false;
20+
let locals = node.attrs.locals || false;
21+
let content;
22+
let subtree;
23+
let source;
24+
25+
if (src) {
26+
src = path.resolve(options.root, src);
27+
source = fs.readFileSync(src, options.encoding);
28+
29+
if (locals) {
30+
locals = JSON.parse(locals);
31+
const result = posthtml()
32+
.use(expressions({locals}))
33+
.process(source, {sync: true});
34+
source = result.html;
35+
}
36+
37+
subtree = tree.parser(source);
38+
subtree.match = tree.match;
39+
subtree.parser = tree.parser;
40+
content = source.includes('include') ? posthtmlInclude(subtree) : subtree;
41+
42+
if (tree.messages) {
43+
tree.messages.push({
44+
type: 'dependency',
45+
file: src
46+
});
47+
}
48+
}
49+
50+
return {
51+
tag: false,
52+
content
53+
};
54+
});
55+
56+
return tree;
57+
};
58+
};

0 commit comments

Comments
 (0)