Skip to content

Commit 3dabe47

Browse files
committed
fixed: html codeblock include 2 newline
1 parent 07c6fe5 commit 3dabe47

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

app/components/artifacts.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const HTMLPreview = forwardRef<HTMLPreviewHander, HTMLPreviewProps>(
8080
}, [props.autoHeight, props.height, iframeHeight]);
8181

8282
const srcDoc = useMemo(() => {
83-
const script = `<script>new ResizeObserver((entries) => parent.postMessage({id: '${frameId}', height: entries[0].target.clientHeight}, '*')).observe(document.body)</script>`;
83+
const script = `<script>window.addEventListener("DOMContentLoaded", () => new ResizeObserver((entries) => parent.postMessage({id: '${frameId}', height: entries[0].target.clientHeight}, '*')).observe(document.body))</script>`;
8484
if (props.code.includes("<!DOCTYPE html>")) {
8585
props.code.replace("<!DOCTYPE html>", "<!DOCTYPE html>" + script);
8686
}

app/components/markdown.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,26 @@ function escapeBrackets(text: string) {
237237
);
238238
}
239239

240+
function tryWrapHtmlCode(text: string) {
241+
// try add wrap html code (fixed: html codeblock include 2 newline)
242+
return text
243+
.replace(
244+
/([`]*?)(\w*?)([\n\r]*?)(<!DOCTYPE html>)/g,
245+
(match, quoteStart, lang, newLine, doctype) => {
246+
return !quoteStart ? "\n```html\n" + doctype : match;
247+
},
248+
)
249+
.replace(
250+
/(<\/body>)([\r\n\s]*?)(<\/html>)([\n\r]*?)([`]*?)([\n\r]*?)/g,
251+
(match, bodyEnd, space, htmlEnd, newLine, quoteEnd) => {
252+
return !quoteEnd ? bodyEnd + space + htmlEnd + "\n```\n" : match;
253+
},
254+
);
255+
}
256+
240257
function _MarkDownContent(props: { content: string }) {
241258
const escapedContent = useMemo(() => {
242-
return escapeBrackets(escapeDollarNumber(props.content));
259+
return tryWrapHtmlCode(escapeBrackets(escapeDollarNumber(props.content)));
243260
}, [props.content]);
244261

245262
return (

0 commit comments

Comments
 (0)