client: optimize fetching rendered markdown
This commit is contained in:
parent
34b1ab979f
commit
eaffebb53c
4 changed files with 11 additions and 6 deletions
|
@ -68,7 +68,8 @@ const Document = ({ remove, editable, title, content, setTitle, setContent, init
|
||||||
|
|
||||||
const onTitleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => setTitle ? setTitle(event.target.value) : null, [setTitle])
|
const onTitleChange = useCallback((event: ChangeEvent<HTMLInputElement>) => setTitle ? setTitle(event.target.value) : null, [setTitle])
|
||||||
|
|
||||||
const removeFile = useCallback(() => (remove?: () => void) => {
|
const removeFile = useCallback((remove?: () => void) => {
|
||||||
|
console.log(remove)
|
||||||
if (remove) {
|
if (remove) {
|
||||||
if (content && content.trim().length > 0) {
|
if (content && content.trim().length > 0) {
|
||||||
const confirmed = window.confirm("Are you sure you want to remove this file?")
|
const confirmed = window.confirm("Are you sure you want to remove this file?")
|
||||||
|
@ -121,7 +122,7 @@ const Document = ({ remove, editable, title, content, setTitle, setContent, init
|
||||||
width={"100%"}
|
width={"100%"}
|
||||||
id={title}
|
id={title}
|
||||||
/>
|
/>
|
||||||
{remove && editable && <Button type="abort" ghost icon={<Trash />} auto height={'36px'} width={'36px'} onClick={removeFile} />}
|
{remove && editable && <Button type="abort" ghost icon={<Trash />} auto height={'36px'} width={'36px'} onClick={() => removeFile(remove)} />}
|
||||||
</div>
|
</div>
|
||||||
<div className={styles.descriptionContainer}>
|
<div className={styles.descriptionContainer}>
|
||||||
{tab === 'edit' && editable && <FormattingIcons setText={setContent} textareaRef={codeEditorRef} />}
|
{tab === 'edit' && editable && <FormattingIcons setText={setContent} textareaRef={codeEditorRef} />}
|
||||||
|
|
|
@ -111,7 +111,7 @@ const Post = () => {
|
||||||
}, [docs, title])
|
}, [docs, title])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div style={{ marginBottom: 150 }}>
|
||||||
<Title title={title} onChange={onChangeTitle} />
|
<Title title={title} onChange={onChangeTitle} />
|
||||||
<FileDropzone setDocs={uploadDocs} />
|
<FileDropzone setDocs={uploadDocs} />
|
||||||
<DocumentList docs={docs} updateDocTitle={updateDocTitle} updateDocContent={updateDocContent} removeDoc={removeDoc} />
|
<DocumentList docs={docs} updateDocTitle={updateDocTitle} updateDocContent={updateDocContent} removeDoc={removeDoc} />
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import useTheme from "@lib/hooks/use-theme"
|
import useTheme from "@lib/hooks/use-theme"
|
||||||
import { memo, useEffect, useState } from "react"
|
import { memo, useEffect, useState } from "react"
|
||||||
|
import styles from './preview.module.css'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
height?: number | string
|
height?: number | string
|
||||||
|
@ -24,7 +25,7 @@ const MarkdownPreview = ({ height = 500, fileId, content, title }: Props) => {
|
||||||
setPreview(res)
|
setPreview(res)
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
} else {
|
} else if (content) {
|
||||||
const resp = await fetch(`/api/render-markdown`, {
|
const resp = await fetch(`/api/render-markdown`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -41,11 +42,12 @@ const MarkdownPreview = ({ height = 500, fileId, content, title }: Props) => {
|
||||||
setIsLoading(false)
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setIsLoading(false)
|
||||||
}
|
}
|
||||||
fetchPost()
|
fetchPost()
|
||||||
}, [content, fileId, title])
|
}, [content, fileId, title])
|
||||||
return (<>
|
return (<>
|
||||||
{isLoading ? <div>Loading...</div> : <div data-theme={theme} dangerouslySetInnerHTML={{ __html: preview }} style={{
|
{isLoading ? <div>Loading...</div> : <div data-theme={theme} className={styles.markdownPreview} dangerouslySetInnerHTML={{ __html: preview }} style={{
|
||||||
height
|
height
|
||||||
}} />}
|
}} />}
|
||||||
</>)
|
</>)
|
||||||
|
|
|
@ -11,12 +11,14 @@ const renderMarkdown: NextApiHandler = async (req, res) => {
|
||||||
return language
|
return language
|
||||||
}
|
}
|
||||||
const type = fileType()
|
const type = fileType()
|
||||||
let contentToRender: string = '\n' + (content || '');
|
let contentToRender: string = (content || '');
|
||||||
|
|
||||||
if (!renderAsMarkdown.includes(type)) {
|
if (!renderAsMarkdown.includes(type)) {
|
||||||
contentToRender = `~~~${type}
|
contentToRender = `~~~${type}
|
||||||
${content}
|
${content}
|
||||||
~~~`
|
~~~`
|
||||||
|
} else {
|
||||||
|
contentToRender = '\n' + content;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof contentToRender !== 'string') {
|
if (typeof contentToRender !== 'string') {
|
||||||
|
|
Loading…
Reference in a new issue