imzbf / md-editor-v3

Markdown editor for vue3, developed in jsx and typescript, dark theme、beautify content by prettier、render articles directly、paste or clip the picture and upload it...
https://imzbf.github.io/md-editor-v3
MIT License
1.51k stars 145 forks source link

图片懒加载未生效 #628

Closed 11003 closed 1 month ago

11003 commented 1 month ago

描述这个Bug

版本号:

"markdown-it-image-figures": "^2.1.1",
"md-editor-v3": "^4.17.4",
<script setup>
    import { MdPreview, MdCatalog, config } from 'md-editor-v3'
    import 'md-editor-v3/lib/preview.css'
    import ImageFiguresPlugin from 'markdown-it-image-figures'

    config({
        markdownItConfig: (mdit) => {
            mdit.use(ImageFiguresPlugin, { lazy: true, async: true, figcaption: true })
        }
    })

    const props = defineProps({
        /**
         * 服务器渲染必需
         */
        contentId: {
            type: [String, Number],
            required: true,
            default: () => ''
        },
        /**
         * 内容
         */
        markdownContent: {
            type: String,
            required: true,
            default: () => ''
        }
    })

    /**
     * 是否存在文章目录
     */
    const showToc = ref(false)
    function onGetCatalog(catalog) {
        showToc.value = !!catalog.length
    }

    /**
     * toc item 点击滚动
     */
    const scrollElement = ref()
    if (import.meta.client) {
        scrollElement.value = document.documentElement
    }
</script>
<template>
    <div v-show="props.markdownContent" class="renderBox" :class="{ showTocA: showToc }">
        <MdPreview @onGetCatalog="onGetCatalog" noMermaid noKatex class="custom-markdown" v-model="props.markdownContent" :editor-id="`pv-${props.contentId}`" />
        <div v-show="showToc" class="toc">
            <MdCatalog class="renderToc" :editorId="`pv-${props.contentId}`" :scrollElement="scrollElement" :scrollElementOffsetTop="80" />
        </div>
    </div>
</template>

image

image

版本号

20.16.0

问题重现链接

https://liuhai.work/post/43

imzbf commented 1 month ago
{
  lazy: true,
+  removeSrc: true,
  async: true
}

另外,内部使用了markdown-it-image-figures,你应该通过config去修改option

config({
  markdownItPlugins(plugins, { editorId }) {
    return plugins.map((item) => {
      if (item.type === 'image') {
        return {
          ...item,
          options: {
            ...item.options,
+         ...your options
          }
        };
      }

      return item;
    });
  },
})
11003 commented 1 month ago
    config({
        markdownItPlugins(plugins) {
            return plugins.map((item) => {
                if (item.type === 'image') {
                    return {
                        ...item,
                        options: {
                            ...item.options,
                            lazy: true,
                            async: true,
                            classes: 'lazy'
                        }
                    }
                }
                return item
            })
        }
    })