From 518e8f7e25e03df7bdc9323cc26ea19c6b5e6d8c Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Tue, 20 Sep 2022 13:13:21 -0400 Subject: [PATCH] Allow passing promises to set:html (#4819) * Allow passing promises to set:html * Adding a changeset --- .changeset/popular-deers-grow.md | 5 +++++ packages/astro/src/runtime/server/escape.ts | 10 ++++++++++ packages/astro/src/runtime/server/index.ts | 2 +- packages/astro/test/astro-slots.test.js | 1 + .../astro-slots/src/components/RenderArgs.astro | 3 +-- .../astro-slots/src/pages/slottedapi-render.astro | 2 +- 6 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 .changeset/popular-deers-grow.md diff --git a/.changeset/popular-deers-grow.md b/.changeset/popular-deers-grow.md new file mode 100644 index 000000000..fa3a40c28 --- /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 2856da0fc..7610a3dee 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 1304aca3e..cd02deaa9 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 b5ab05909..3c0b2be49 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 6936aaa10..d017f2629 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 960ffa629..8cd31fdd3 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}}