fix(Question): Set markdown render by server api

This commit is contained in:
haitao(lj) 2023-01-11 17:42:07 +08:00
parent 9030dc5bf5
commit c9d4f5d7fa
3 changed files with 19 additions and 13 deletions

View File

@ -7,29 +7,31 @@ import {
useImperativeHandle,
} from 'react';
import { marked } from 'marked';
import { markdownToHtml } from '@/services';
import { htmlRender } from './utils';
let scrollTop = 0;
marked.setOptions({
breaks: true,
sanitize: true,
});
let renderTimer;
const Index = ({ value }, ref) => {
const [html, setHtml] = useState('');
const previewRef = useRef<HTMLDivElement>(null);
const renderMarkdown = (markdown) => {
clearTimeout(renderTimer);
const timeout = renderTimer ? 1000 : 0;
renderTimer = setTimeout(() => {
markdownToHtml(markdown).then((resp) => {
scrollTop = previewRef.current?.scrollTop || 0;
setHtml(resp);
});
}, timeout);
};
useEffect(() => {
const previewHtml = marked(value).replace(
/<img/gi,
'<img referrerpolicy="no-referrer"',
);
scrollTop = previewRef.current?.scrollTop || 0;
setHtml(previewHtml);
renderMarkdown(value);
}, [value]);
useEffect(() => {
if (!html) {
return;

View File

@ -23,7 +23,6 @@ const usePageUsers = () => {
setUsers(uniqBy([...users, data], 'userName'));
globalUsers = uniqBy([...globalUsers, data], 'userName');
}
console.log('users', globalUsers);
},
};
};

View File

@ -268,3 +268,8 @@ export const unsubscribe = (code: string) => {
const apiUrl = '/answer/api/v1/user/email/notification';
return request.put(apiUrl, { code });
};
export const markdownToHtml = (content: string) => {
const apiUrl = '/answer/api/v1/post/render';
return request.post(apiUrl, { content });
};