Allow passing promises to set:html (#4819)
* Allow passing promises to set:html * Adding a changeset
This commit is contained in:
parent
666d98e3ec
commit
518e8f7e25
6 changed files with 19 additions and 4 deletions
5
.changeset/popular-deers-grow.md
Normal file
5
.changeset/popular-deers-grow.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allow passing promises to set:html
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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])} />
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in a new issue