@@ -35,6 +35,7 @@ import {
3535import Vditor from " vditor" ;
3636import " vditor/dist/index.css" ;
3737import " github-markdown-css/github-markdown-light.css" ;
38+ import normalizeMarkdownForVditor from " ./assets/js/normalizeMarkdownForVditor" ;
3839
3940const { __cms } = JX3BOX ;
4041const UPLOAD_API = ` ${ __cms} api/cms/upload` ;
@@ -130,6 +131,7 @@ export default {
130131 previewVisible: false ,
131132 isRebuildingEditor: false ,
132133 isUploadingImage: false ,
134+ previewRenderVersion: 0 ,
133135 counterElement: null ,
134136 counterClickListener: null ,
135137 counterClickTimestamps: [],
@@ -235,7 +237,9 @@ export default {
235237 style: " github" ,
236238 },
237239 markdown: {
240+ mark: true ,
238241 sanitize: true ,
242+ toc: true ,
239243 },
240244 mode: this .getPreviewMode (),
241245 },
@@ -300,7 +304,7 @@ export default {
300304 if (this .editor .getValue () === nextValue) return ;
301305 this .editor .setValue (nextValue);
302306 if (this .previewVisible ) {
303- this .editor . renderPreview ();
307+ this .renderPreviewContent ();
304308 }
305309 },
306310 getToolbarButton (name ) {
@@ -356,9 +360,38 @@ export default {
356360
357361 this .syncEditorPanels ();
358362 if (this .previewVisible ) {
359- this .editor . renderPreview ();
363+ this .renderPreviewContent ();
360364 }
361365 },
366+ renderPreviewContent: async function () {
367+ if (! this .editorReady || ! this .editor ) return ;
368+
369+ const previewRoot = this .$refs .editorHost ? .querySelector ? .(" .vditor-preview" );
370+ const previewBody = previewRoot? .querySelector ? .(" .vditor-reset" ) || previewRoot;
371+ if (! previewBody) return ;
372+
373+ const version = ++ this .previewRenderVersion ;
374+ const normalizedMarkdown = normalizeMarkdownForVditor (this .editor .getValue ());
375+
376+ await Vditor .preview (previewBody, normalizedMarkdown, {
377+ mode: " light" ,
378+ lang: " zh_CN" ,
379+ hljs: {
380+ enable: true ,
381+ lineNumber: false ,
382+ style: " github" ,
383+ },
384+ markdown: {
385+ mark: true ,
386+ sanitize: true ,
387+ toc: true ,
388+ },
389+ icon: " ant" ,
390+ });
391+
392+ if (version !== this .previewRenderVersion ) return ;
393+ previewBody .classList .add (" markdown-body" );
394+ },
362395 togglePreview () {
363396 this .previewVisible = ! this .previewVisible ;
364397
@@ -528,8 +561,8 @@ export default {
528561 }
529562
530563 .vditor - toolbar {
531- display: flex;
532- align- items: center;
564+ display: flex;
565+ align- items: center;
533566 padding: 10px 12px ;
534567 background: #f6f8fa;
535568 border- bottom: 1px solid #d8dee4;
0 commit comments