#Helper Functions With Access To Astro Context
3 messages · Page 1 of 1 (latest)
Hi @bright token. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the provided knowledge sources, it seems that the Astro context is not automatically available in externally loaded scripts or helper functions. A contributor in a Github discussion (https://github.com/withastro/astro/issues/506) suggested the idea of passing the Astro global into externally loaded functions, but it's unclear if this is currently supported or not.
Here's the example they provided:
// file:///./fetchAndTransform.mjs
export default (url, parameters, Astro) => Astro.fetchContent(url).then(content => {
// transform content based on parameters
});
---
import fetchAndTransform from "./fetchAndTransform.mjs";
const transformedContent = fetchAndTransform("../pages/post/*.md", { wrap:"li", replaceURL:false }, Astro);
---
<ul>{[...transformedContent]}</ul>