Skip to content
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

feat: move to esm #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions .github/ISSUE_TEMPLATE/1_Bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: "🐛 Bug Report"
about: 'Report a general issue.'
---

## Problem

_Describe your problem._

## Environment

- `posthtml-fetch` plugin version: #.#.#
- PostHTML version: #.#.#
- Node.js version: #.#.#
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
.nyc_output
node_modules
coverage
*.log
53 changes: 28 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require('path')
const isUrl = require('is-url')
const got = require('got')
const posthtml = require('posthtml')
const matcher = require('posthtml-match-helper')
const expressions = require('posthtml-expressions')

module.exports = (options = {}) => tree => {
import {createRequire} from 'node:module'
import isUrl from 'is-url'
import got from 'got'
import posthtml from 'posthtml'
import matcher from 'posthtml-match-helper'
import expressions from 'posthtml-expressions'

const plugin = (options = {}) => tree => {
options.tags = options.tags || ['fetch', 'remote']
options.attribute = options.attribute || 'url'
options.got = options.got || {}
Expand All @@ -16,13 +16,13 @@ module.exports = (options = {}) => tree => {

tree.match(matcher(options.tags.join(',')), node => {
const responseAssign = response => {
let [item] = response.tree;
let [item] = response.tree

if (typeof item === 'string') {
item = {content: [item]};
item = {content: [item]}
}

Object.assign(node, item);
Object.assign(node, item)
}

if (node.attrs && node.attrs[options.attribute]) {
Expand All @@ -31,10 +31,10 @@ module.exports = (options = {}) => tree => {
all.push(new Promise((resolve, reject) => {
Promise.resolve((() => {
let plugins = []
const content = tree.render(node);
const content = tree.render(node)

if (options.plugins && options.plugins.before) {
plugins = plugins.concat(options.plugins.before);
plugins = plugins.concat(options.plugins.before)
}

return posthtml(plugins).process(content)
Expand All @@ -43,21 +43,22 @@ module.exports = (options = {}) => tree => {
.then(() => {
if (isUrl(options.got.url)) {
return got(options.got)
} else {
const response = {
body: undefined
}

try {
response.body = JSON.stringify(require(path.resolve(options.got.url)))
} catch {}
}

return response
const response = {
body: undefined
}

try {
const require = createRequire(import.meta.url)
response.body = JSON.stringify(require.resolve(options.got.url))
} catch {}

return response
})
.then(({body}) => {
const plugins = []
let content = body;
let content = body

try {
plugins.push(expressions({locals: {response: JSON.parse(body)}}))
Expand All @@ -75,10 +76,10 @@ module.exports = (options = {}) => tree => {
})
.then(() => {
let plugins = []
const content = tree.render(node);
const content = tree.render(node)

if (options.plugins && options.plugins.after) {
plugins = plugins.concat(options.plugins.after);
plugins = plugins.concat(options.plugins.after)
}

return posthtml(plugins).process(content)
Expand All @@ -101,3 +102,5 @@ module.exports = (options = {}) => tree => {
Promise.all(all).then(resolve.bind(null, tree)).catch(error => reject(error))
})
}

export default plugin
Loading