Skip to content

Commit e870b4d

Browse files
feat(): add extend option
1 parent bb57d4d commit e870b4d

File tree

6 files changed

+123
-18
lines changed

6 files changed

+123
-18
lines changed

README.md

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
# PostHTML
2-
View Engine for ExpressJS
1+
# [PostHTML](https://github.com/posthtml/posthtml)
2+
View Engine for [ExpressJS](expressjs.com)
3+
4+
[PostHTML Plugins Catalog](https://maltsev.github.io/posthtml-plugins/)
35

46
# Install
57

6-
```
8+
```bash
79
(sudo) npm i -S express
810
(sudo) npm i -S express-posthtml
911
```
1012

1113
# Usage
1214
## Engine
13-
Register PostHML as ExpressJS View Engine
15+
Register PostHTML as ExpressJS View Engine
1416

15-
```
17+
```javascript
1618
app.engine('html', require('express-posthtml'))
1719

1820
app.set('views', /* Path to views */)
@@ -21,22 +23,41 @@ app.set('view engine', 'html')
2123

2224
## Plugins
2325
### Global
24-
All Views will be render with the same plugin setup
26+
All Views will be render with the same plugin setup.
2527

26-
```
28+
```javascript
2729
app.set('view options', [ PostHTML Plugins ])
2830
```
2931

32+
```javascript
33+
res.render('file')
34+
```
35+
3036
### Local
31-
View specific plugin setup to add plugins separately as needed.
37+
View specific setup adding plugins separately as needed.
3238

39+
```javascript
40+
app.set('view options', [])
3341
```
34-
res.render('file', {plugins: [ PostHTML Plugins ]})
42+
43+
```javascript
44+
res.render('file', { plugins: [ PostHTML Plugins ] })
3545
```
3646

37-
# Example
47+
### Extend
48+
If views share common plugins (e.g [BEM](https://github.com/rajdee/posthtml-bem)), but view specific additions are necessary, use the extend option. This 'extends' the global setup with the local plugins as specified in the respective route.
3849

50+
```javascript
51+
app.set('view options', [ PostHTML Global Plugins ])
3952
```
53+
54+
```javascript
55+
res.render('file', { plugins: [ PostHTML Local Plugins ], extend: true, })
56+
```
57+
58+
# Example
59+
60+
```javascript
4061
var express = require('express')
4162

4263
var app = express()
@@ -47,12 +68,20 @@ app.engine('html', require('express-posthtml'))
4768
// Settings
4869
app.set('views', /* Path to views */)
4970
app.set('view engine', 'html')
50-
app.set('view options', [/* PostHTML Plugins */]) // Global
51-
71+
app.set('view options', [/* PostHTML Plugins */]) // Global Setup
5272

73+
// Global Use
5374
app.get('/', (req, res) => {
54-
res.render('index', { plugins: [/* PostHTML Plugins */] } ) // Local
75+
res.render('file')
76+
})
77+
// Local
78+
app.get('/local', (req, res) => {
79+
res.render('file', { plugins: [/* PostHTML Plugins */] } )
5580
})
81+
// Extend
82+
app.get('/extend', (req, res) => {
83+
res.render('file', { plugins: [/* PostHTML Plugins */], extend: true } )
84+
})
5685

5786
app.listen(3000, () => {
5887
console.log('Server started!')

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ var fs = require('fs')
22
var posthtml = require('posthtml')
33

44
module.exports = function (path, options, cb) {
5+
options.extend = options.extend || false
6+
57
var plugins
68

7-
if (!options.plugins) {
9+
if (!options.plugins && options.extend === false) {
810
plugins = options.settings['view options'] || []
11+
} else if (options.extend === true) {
12+
plugins = options.plugins.concat(options.settings['view options'])
913
} else {
1014
plugins = options.plugins || []
1115
}
1216

1317
fs.readFile(path, function (err, content) {
1418
if (err) return cb(new Error(err))
19+
1520
posthtml(plugins)
1621
.process(content.toString())
1722
.then((result) => {

test/bem.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>BEM</title>
7+
</head>
8+
9+
<body>
10+
11+
<div block="block">
12+
<h1>Block</h1>
13+
<div elem="element">
14+
<h2>Element</h2>
15+
</div>
16+
<div elem="element" mods="modifier">
17+
<h2>Element with modifier</h2>
18+
</div>
19+
</div>
20+
21+
<div block="block" mods="modifier" each="3">
22+
<h1>Block with modifier</h1>
23+
<div elem="element">
24+
<h2>Element</h2>
25+
</div>
26+
<div elem="element" mods="modifier">
27+
<h2>Element with Modifier</h2>
28+
</div>
29+
</div>
30+
31+
</body>
32+
33+
</html>

test/include.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="uft-8">
6+
<title>Include</title>
7+
</head>
8+
9+
<body>
10+
<div include="./include/partial.html"></div>
11+
</body>
12+
13+
</html>

test/include/partial.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="uft-8">
6+
<title>Document</title>
7+
</head>
8+
9+
<body>
10+
11+
</body>
12+
13+
</html>

test/server.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@ var express = require('express')
33
var app = express()
44

55
var bem = require('posthtml-bem')
6+
var each = require('posthtml-each')
67

78
app.engine('html', require('../index'))
89

9-
app.set('views', '/')
10+
app.set('views', __dirname)
1011
app.set('view engine', 'html')
11-
app.set('view options', [bem()])
12+
app.set('view options', [ bem() ])
1213

1314
app.get('/', (req, res) => {
14-
res.render('index')
15+
res.render('bem')
1516
})
17+
1618
app.get('/local', (req, res) => {
17-
res.render('index', { plugins: [bem()] })
19+
res.render('bem', { plugins: [
20+
bem({
21+
elemPrefix: '__',
22+
modPrefix: '--',
23+
modDlmtr: '-'
24+
})
25+
]})
26+
})
27+
28+
app.get('/extend', (req, res) => {
29+
res.render('bem', { extend: true, plugins: [ each() ] })
1830
})
1931

2032
app.listen(3000)

0 commit comments

Comments
 (0)