Skip to content

Commit

Permalink
Allow passing promises to set:html (#4819)
Browse files Browse the repository at this point in the history
* Allow passing promises to set:html

* Adding a changeset
  • Loading branch information
matthewp authored Sep 20, 2022
1 parent 666d98e commit 518e8f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-deers-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Allow passing promises to set:html
10 changes: 10 additions & 0 deletions packages/astro/src/runtime/server/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/astro/test/astro-slots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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])} />
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit 518e8f7

Please sign in to comment.