mirror of https://gitee.com/answerdev/answer.git
fix(Question): Set markdown render by server api
This commit is contained in:
parent
9030dc5bf5
commit
c9d4f5d7fa
|
@ -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;
|
||||
|
|
|
@ -23,7 +23,6 @@ const usePageUsers = () => {
|
|||
setUsers(uniqBy([...users, data], 'userName'));
|
||||
globalUsers = uniqBy([...globalUsers, data], 'userName');
|
||||
}
|
||||
console.log('users', globalUsers);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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 });
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue