DesignMarkdown.vue 1.9 KB
Newer Older
yma16's avatar
yma16 已提交
1
<template>
yma16's avatar
yma16 已提交
2 3
  <div style="width: 100%">
    <MarkDirTree :dirContent="dirContent"/>
yma16's avatar
yma16 已提交
4 5
    <VueMarkdown
      :source="content"
yma16's avatar
yma16 已提交
6
      :toc="true"
yma16's avatar
yma16 已提交
7 8 9 10 11 12 13
      v-highlight
      style="width: 100%; text-align: left"
    ></VueMarkdown>
  </div>
</template>
<script>
import VueMarkdown from 'vue-markdown'
yma16's avatar
yma16 已提交
14
import MarkDirTree from './MarkDirTree'
yma16's avatar
yma16 已提交
15
export default {
yma16's avatar
yma16 已提交
16
    components: {VueMarkdown, MarkDirTree},
yma16's avatar
yma16 已提交
17 18 19 20 21 22
    name: 'DesignMarkdown',
    props: {
        contentSource: undefined
    },
    data () {
        return {
yma16's avatar
yma16 已提交
23 24
            content: '',
            dirContent: []
yma16's avatar
yma16 已提交
25 26 27 28
        }
    },
    watch: {
        contentSource: {
yma16's avatar
yma16 已提交
29
            handler (newVal) {
yma16's avatar
yma16 已提交
30
                this.content = newVal || ''
yma16's avatar
yma16 已提交
31
                this.getDirContent(newVal)
yma16's avatar
yma16 已提交
32 33 34 35 36 37 38 39 40
            },
            deep: true,
            immediate: true
        }
    },
    mounted () {
        console.log('design vuemarkdown')
    },
    methods: {
yma16's avatar
yma16 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        // 树状目录
        getDirContent (mdContent) {
            if (!mdContent) {
                return []
            }
            const lineT = mdContent.split('\n')
            const titleArray = lineT.filter(item => item && item[0] === '#')
            console.log('titleArray', titleArray)
            const dirArray = titleArray.map(item => {
                return item.replace(/\r/g, '')
            })
            console.log('dirArray', dirArray)
            const dirLevelArray = dirArray.map(item => {
                let level = 0
                for (let loc in item) {
                    if (item[loc] === '#') {
                        ++level
                    }
                }
                const itemBack = item
                const value = itemBack.replace(/#/g, '').trim()
                return {
                    level,
                    value
                }
            })
            console.log('dirLevelArray', dirLevelArray)
            this.dirContent = dirLevelArray
        }
yma16's avatar
yma16 已提交
70 71 72
    }
}
</script>