Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 615783d

Browse files
authoredApr 26, 2022
Merge pull request #55 from posthtml/expressions-options
2 parents 1316324 + 3e2a6c9 commit 615783d

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed
 

‎lib/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
const got = require('got')
12
const path = require('path')
23
const isUrl = require('is-url')
3-
const got = require('got')
44
const posthtml = require('posthtml')
5+
const merge = require('lodash.merge')
56
const matcher = require('posthtml-match-helper')
67
const expressions = require('posthtml-expressions')
78

89
module.exports = (options = {}) => tree => {
9-
options.tags = options.tags || ['fetch', 'remote']
10-
options.attribute = options.attribute || 'url'
1110
options.got = options.got || {}
11+
options.attribute = options.attribute || 'url'
12+
options.expressions = options.expressions || {}
1213
options.preserveTag = options.preserveTag || false
14+
options.tags = options.tags || ['fetch', 'remote']
1315

1416
return new Promise((resolve, reject) => {
1517
const all = []
@@ -60,7 +62,16 @@ module.exports = (options = {}) => tree => {
6062
let content = body;
6163

6264
try {
63-
plugins.push(expressions({locals: {response: JSON.parse(body)}}))
65+
plugins.push(
66+
expressions(
67+
merge(
68+
options.expressions,
69+
{
70+
locals: {response: JSON.parse(body)}
71+
}
72+
))
73+
)
74+
6475
content = tree.render(node.content)
6576
} catch {}
6677

‎package-lock.json

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"dependencies": {
3434
"got": "^11.8.0",
3535
"is-url": "^1.2.4",
36+
"lodash.merge": "^4.6.2",
3637
"posthtml": "^0.16.4",
3738
"posthtml-expressions": "^1.6.2",
3839
"posthtml-match-helper": "^1.0.1"

‎readme.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ posthtml()
4444
.process('<fetch url="https://example.test">{{ response }}</fetch>')
4545
.then(result => console.log(result.html))
4646

47-
// response body
47+
// => interpolated response body
4848
```
4949

5050
The response body will be available under the `response` local variable.
@@ -114,7 +114,7 @@ posthtml()
114114
.process('<fetch from="https://example.test">{{ response }}</fetch>')
115115
.then(result => {
116116
console.log(result.html)
117-
// => ...interpolated response from https://example.test
117+
// => interpolated response body
118118
})
119119
```
120120

@@ -137,13 +137,15 @@ posthtml()
137137
.process('<fetch url="https://example.test">{{ response }}</fetch>')
138138
.then(result => {
139139
console.log(result.html)
140-
// => ...interpolated response from https://example.test
140+
// => interpolated response body
141141
})
142142
```
143143

144144
### `preserveTag`
145145

146-
Allows you to leave an item. Default value `false`.
146+
Default: `false`
147+
148+
When set to `true`, this option will preserve the `tag` around the response body.
147149

148150
Example:
149151

@@ -158,7 +160,32 @@ posthtml()
158160
.process('<fetch url="https://example.test">{{ response }}</fetch>')
159161
.then(result => {
160162
console.log(result.html)
161-
// => <fetch url="https://example.test">...interpolated response from https://example.test</fetch>
163+
// => <fetch url="https://example.test">interpolated response body</fetch>
164+
})
165+
```
166+
167+
### `expressions`
168+
169+
Default: `{}`
170+
171+
You can pass options to `posthtml-expressions`.
172+
173+
Example:
174+
175+
```js
176+
const posthtml = require('posthtml')
177+
const pf = require('posthtml-fetch')
178+
179+
posthtml()
180+
.use(pf({
181+
expressions: {
182+
delimiters: ['[[', ']]'],
183+
}
184+
}))
185+
.process('<fetch url="https://example.test">[[ response ]]</fetch>')
186+
.then(result => {
187+
console.log(result.html)
188+
// => interpolated response body
162189
})
163190
```
164191

@@ -193,7 +220,7 @@ posthtml()
193220
.process('<fetch url="https://example.test">{{ response }}</fetch>')
194221
.then(result => {
195222
console.log(result.html)
196-
// => ...interpolated response from https://example.test
223+
// => interpolated response body
197224
})
198225
```
199226

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
User name: Leanne Graham
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<fetch url="https://jsonplaceholder.typicode.com/users/1">
2+
User name: [[ response.name ]]
3+
</fetch>

‎test/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,7 @@ test('It works with custom attribute', async t => {
9494
test('It works with multiple call of fetch', async t => {
9595
await process(t, 'multiple-src')
9696
})
97+
98+
test('It works with options passed to posthtml-expressions', async t => {
99+
await process(t, 'expressions-options', {expressions: {delimiters: ['[[', ']]']}})
100+
})

0 commit comments

Comments
 (0)
Please sign in to comment.