Skip to content

Commit 0ba3eeb

Browse files
committed
⚡ Energize.
0 parents  commit 0ba3eeb

28 files changed

+9345
-0
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_size = 2
6+
indent_style = space
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.md]
11+
trim_trailing_whitespace = false

.github/FUNDING.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: ['https://paypal.me/cossssmin', 'https://www.amazon.co.uk/hz/wishlist/ls/3I8KTU4FLQIZ1']

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
.nyc_output
3+
*.log

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- stable

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Cosmin Popovici
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<div align="center">
2+
<img width="150" height="150" title="PostHTML" src="https://posthtml.github.io/posthtml/logo.svg">
3+
<h1>Fetch Remote Content</h1>
4+
<p>A plugin for fetching and working with remote content</p>
5+
6+
[![Version][npm-version-shield]][npm]
7+
[![License][license-shield]][license]
8+
[![Build][travis-ci-shield]][travis-ci]
9+
[![Downloads][npm-stats-shield]][npm-stats]
10+
</div>
11+
12+
## About
13+
14+
This plugin allows you to fetch remote content and display it in your HTML.
15+
16+
Input:
17+
18+
```html
19+
<fetch url="https://jsonplaceholder.typicode.com/users/1">
20+
{{ response.name }}'s username is {{ response.username }}
21+
</fetch>
22+
```
23+
24+
Output:
25+
26+
```html
27+
Leanne Graham's username is Bret
28+
```
29+
30+
## Install
31+
32+
```
33+
$ npm i posthtml posthtml-fetch
34+
```
35+
36+
## Usage
37+
38+
```js
39+
const posthtml = require('posthtml')
40+
const pf = require('posthtml-fetch')
41+
42+
posthtml()
43+
.use(pf())
44+
.process('<fetch url="https://example.test">{{ response }}</fetch>')
45+
.then(result => console.log(result.html))
46+
47+
// response body
48+
```
49+
50+
The response body will be available under the `response` local variable.
51+
52+
## Response types
53+
54+
The plugin supports `json` and `text` responses.
55+
56+
Only the response body is returned.
57+
58+
## Expressions
59+
60+
The plugin uses [`posthtml-expressions`](https://github.com/posthtml/posthtml-expressions), so you can use any of its tags to work with the `response`.
61+
62+
For example, you can iterate over items in a JSON response:
63+
64+
```html
65+
<fetch url="https://jsonplaceholder.typicode.com/users">
66+
<each loop="user in response">
67+
{{ user.name }}
68+
</each>
69+
</fetch>
70+
```
71+
72+
## Options
73+
74+
You can configure the plugin with the following options.
75+
76+
### `tags`
77+
78+
Default: `['fetch', 'remote']`
79+
80+
Array of supported tag names.
81+
82+
Only tags from this array will be processed by the plugin.
83+
84+
Example:
85+
86+
```js
87+
const posthtml = require('posthtml')
88+
const pf = require('posthtml-fetch')
89+
90+
posthtml()
91+
.use(pf({
92+
tags: ['get']
93+
}))
94+
.process('<get url="https://example.test">{{ response }}</get>')
95+
.then(result => console.log(result.html))
96+
```
97+
98+
### `attribute`
99+
100+
Default: `'url'`
101+
102+
String representing attribute name containing the URL to fetch.
103+
104+
Example:
105+
106+
```js
107+
const posthtml = require('posthtml')
108+
const pf = require('posthtml-fetch')
109+
110+
posthtml()
111+
.use(pf({
112+
attribute: 'from'
113+
}))
114+
.process('<fetch from="https://example.test">{{ response }}</fetch>')
115+
.then(result => console.log(result.html))
116+
```
117+
118+
### `got`
119+
120+
The plugin uses [`got`](https://github.com/sindresorhus/got) to fetch data. You can pass options directly to it, inside the `got` object.
121+
122+
Example:
123+
124+
```js
125+
const posthtml = require('posthtml')
126+
const pf = require('posthtml-fetch')
127+
128+
posthtml()
129+
.use(pf({
130+
got: {
131+
// pass options to got...
132+
}
133+
}))
134+
.process('<fetch url="https://example.test">{{ response }}</fetch>')
135+
.then(result => console.log(result.html))
136+
```
137+
138+
139+
140+
[npm]: https://www.npmjs.com/package/posthtml-fetch
141+
[npm-version-shield]: https://img.shields.io/npm/v/posthtml-fetch.svg
142+
[npm-stats]: http://npm-stat.com/charts.html?package=posthtml-fetch
143+
[npm-stats-shield]: https://img.shields.io/npm/dt/posthtml-fetch.svg
144+
[travis-ci]: https://travis-ci.org/posthtml/posthtml-fetch/
145+
[travis-ci-shield]: https://img.shields.io/travis/posthtml/posthtml-fetch/master.svg
146+
[license]: ./LICENSE
147+
[license-shield]: https://img.shields.io/npm/l/posthtml-fetch.svg

lib/index.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict'
2+
3+
const got = require('got')
4+
const posthtml = require('posthtml')
5+
const matcher = require("posthtml-match-helper")
6+
const expressions = require("posthtml-expressions")
7+
8+
module.exports = (options = {}) => tree => {
9+
options.tags = options.tags || ['fetch', 'remote']
10+
options.attribute = options.attribute || 'url'
11+
options.got = options.got || {}
12+
13+
return new Promise((resolve, reject) => {
14+
const all = []
15+
16+
tree.match(matcher(options.tags.join()), node => {
17+
if (node.attrs && node.attrs[options.attribute]) {
18+
options.got.url = options.got.url || node.attrs[options.attribute]
19+
20+
all.push(new Promise((resolve, reject) => {
21+
got(options.got)
22+
.then(({body}) => {
23+
const plugins = []
24+
let content = body;
25+
node.tag = false
26+
27+
try {
28+
plugins.push(expressions({locals: {response: JSON.parse(body)}}))
29+
content = tree.render(node.content)
30+
} catch {}
31+
32+
return posthtml(plugins).process(content)
33+
})
34+
.then(result => {
35+
node.content = result.html
36+
resolve(node)
37+
})
38+
.catch(error => {
39+
reject(error)
40+
})
41+
})
42+
)
43+
}
44+
45+
return node
46+
})
47+
48+
Promise.all(all).then(resolve.bind(null, tree)).catch(error => reject(error))
49+
})
50+
}

0 commit comments

Comments
 (0)