Allow passing promises to set:html (#4819)

* Allow passing promises to set:html

* Adding a changeset
This commit is contained in:
Matthew Phillips 2022-09-20 13:13:21 -04:00 committed by GitHub
parent 666d98e3ec
commit 518e8f7e25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allow passing promises to set:html

View file

@ -29,3 +29,13 @@ export const markHTMLString = (value: any) => {
// The compiler will recursively stringify these correctly at a later stage.
return value;
};
export function unescapeHTML(str: any) {
// If a promise, await the result and mark that.
if(!!str && typeof str === 'object' && typeof str.then === 'function') {
return Promise.resolve(str).then(value => {
return markHTMLString(value);
});
}
return markHTMLString(str);
}

View file

@ -4,7 +4,7 @@ export {
escapeHTML,
HTMLString,
markHTMLString,
markHTMLString as unescapeHTML,
unescapeHTML,
} from './escape.js';
export type { Metadata } from './metadata';
export { createMetadata } from './metadata.js';

View file

@ -148,6 +148,7 @@ describe('Slots', () => {
const $ = cheerio.load(html);
expect($('#render-args')).to.have.lengthOf(1);
expect($('#render-args span')).to.have.lengthOf(1);
expect($('#render-args').text()).to.equal('render-args');
}
});

View file

@ -1,6 +1,5 @@
---
const { id, text } = Astro.props;
const content = await Astro.slots.render('default', [text]);
---
<div id={id} set:html={content} />
<div id={id} set:html={Astro.slots.render('default', [text])} />

View file

@ -15,6 +15,6 @@ import RenderArgs from '../components/RenderArgs.astro';
<body>
<Render id="render">render</Render>
<RenderFn id="render-fn">{() => "render-fn"}</RenderFn>
<RenderArgs id="render-args" text="render-args">{(text: string) => text}</RenderArgs>
<RenderArgs id="render-args" text="render-args">{(text: string) => <span>{text}</span>}</RenderArgs>
</body>
</html>