You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So looking at plugins.before and plugins.after, I understand you can pass an array of PostHTML plugins to run before and after the remote content is fetched.
I'm trying to pass in posthtml-expressions, because I already have some variables in context, that I need to use inside the <fetch> tag:
constfetch=require('posthtml-fetch')module.exports=(html,config)=>{// config is an object I already haveconstfetchPlugin=fetch({plugins: {before: [expressions({locals: {page: config}})]}})returnposthtml([fetchPlugin]).process(html).then(result=>result.html)}
At this point, I would expect both the response variable from posthtml-fetch and the page variable that I'm passing in plugins.before with expressions to be available.
I'll use a real example so you can reproduce it - imagine I already had page.title in context:
Imagine I already have an object with data that I want to make available to the response after it is fetched. So I may already have a variable, let's say language which equals en.
With that, I should be able to do:
<fetchurl="...">
{{ language }}
<!-- now render response -->
{{ response.title }}
</fetch>
... and get:
en
<!-- now render response -->
response title text
If I do this now, I get an error because language is not defined when used inside the <fetch> tag, even though 'globally' it's available (i.e. outside the fetch tag).
So looking at
plugins.before
andplugins.after
, I understand you can pass an array of PostHTML plugins to run before and after the remote content is fetched.I'm trying to pass in
posthtml-expressions
, because I already have some variables in context, that I need to use inside the<fetch>
tag:At this point, I would expect both the
response
variable fromposthtml-fetch
and thepage
variable that I'm passing inplugins.before
withexpressions
to be available.I'll use a real example so you can reproduce it - imagine I already had
page.title
in context:This results in a
ReferenceError: page is not defined
One way around it is if
posthtml-fetch
would accept alocals
option.However, I think passing in
expressions
with its own locals, before the content is fetched, should work.Am I missing something here?
The text was updated successfully, but these errors were encountered: