0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00

Fixed theme preview on theme without styles (#18375)

refs https://ghost.slack.com/archives/C0568LN2CGJ/p1695741379821479

- Fixes a bug in the preview renderer where a theme without styles
wouldn't be handled properly as it cannot inject new styles and cause an
empty page to be returned.

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
copilot:summary
This commit is contained in:
Ronald Langeveld 2023-09-27 12:32:11 +07:00 committed by GitHub
parent 29f98104df
commit 7bebe7daf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -81,8 +81,12 @@ const ThemePreview: React.FC<ThemePreviewProps> = ({settings,url}) => {
const htmlDoc = domParser.parseFromString(data, 'text/html');
const stylesheet = htmlDoc.querySelector('style') as HTMLStyleElement;
const originalCSS = stylesheet.innerHTML;
stylesheet.innerHTML = `${originalCSS}\n\n${injectedCss}`;
const originalCSS = stylesheet?.innerHTML;
if (originalCSS) {
stylesheet.innerHTML = `${originalCSS}\n\n${injectedCss}`;
} else {
htmlDoc.head.innerHTML += `<style>${injectedCss}</style>`;
}
// replace the iframe contents with the doctored preview html
const doctype = htmlDoc.doctype ? new XMLSerializer().serializeToString(htmlDoc.doctype) : '';