diff --git a/.changeset/popular-deers-grow.md b/.changeset/popular-deers-grow.md new file mode 100644 index 000000000000..fa3a40c280cf --- /dev/null +++ b/.changeset/popular-deers-grow.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Allow passing promises to set:html diff --git a/packages/astro/src/runtime/server/escape.ts b/packages/astro/src/runtime/server/escape.ts index 2856da0fc4c9..7610a3deef98 100644 --- a/packages/astro/src/runtime/server/escape.ts +++ b/packages/astro/src/runtime/server/escape.ts @@ -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); +} diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 1304aca3e38e..cd02deaa9eab 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -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'; diff --git a/packages/astro/test/astro-slots.test.js b/packages/astro/test/astro-slots.test.js index b5ab059090cd..3c0b2be49174 100644 --- a/packages/astro/test/astro-slots.test.js +++ b/packages/astro/test/astro-slots.test.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'); } }); diff --git a/packages/astro/test/fixtures/astro-slots/src/components/RenderArgs.astro b/packages/astro/test/fixtures/astro-slots/src/components/RenderArgs.astro index 6936aaa10333..d017f2629395 100644 --- a/packages/astro/test/fixtures/astro-slots/src/components/RenderArgs.astro +++ b/packages/astro/test/fixtures/astro-slots/src/components/RenderArgs.astro @@ -1,6 +1,5 @@ --- const { id, text } = Astro.props; -const content = await Astro.slots.render('default', [text]); --- -
+
diff --git a/packages/astro/test/fixtures/astro-slots/src/pages/slottedapi-render.astro b/packages/astro/test/fixtures/astro-slots/src/pages/slottedapi-render.astro index 960ffa629ee2..8cd31fdd3bed 100644 --- a/packages/astro/test/fixtures/astro-slots/src/pages/slottedapi-render.astro +++ b/packages/astro/test/fixtures/astro-slots/src/pages/slottedapi-render.astro @@ -15,6 +15,6 @@ import RenderArgs from '../components/RenderArgs.astro'; render {() => "render-fn"} - {(text: string) => text} + {(text: string) => {text}}