vue组件文档(.md)中自动导入示例(.vue)

症结(懒癌患者)

在写组件库文档的时候,会把示例代码粘贴到文档里,这样作有一个很恶心的地方:每次组件迭代或修改示例都须要从新修改文档中的代码片断。终年累月,苦不堪言。javascript

猜测(狂想曲)

因此我想,可不能够把.vue文件里的template块和script块取出来,放入对应的.md文件中css

好比在.md文件中 {{:xx.vue?type=(template|script)}} 便替换示例中对应的template|scripthtml

# xx

## 示例代码

// {{:}}  定义变量规则模版(加个冒号防冲突)
{{:image.vue?type=template}} // 对应.vue 的template

{{:image.vue?type=script}}  // 对应.vue 的script

{{:index.js}}  // 对应index.js

## 参数说明

xxx...

outputvue

# xx

## 示例代码

 
// image.vue template
<template>
    <div>xx</div>
</template>

// image.vue script
<script>
    ...
</script>

// index.js

var x = 1

## 参数说明

xxx...

动手(能动手毫不**)

要实现以上功能,须要探索如下几点:java

  • .vue里取出template&script
  • 塞进对应的.md的变量位置
  • .md文件转为Vue Componet / html

若是按照咱们写js的习惯,如下嵌套排列可能更易读webpack

  • .md文件转为Vue Componet / htmlgit

    • 找到变量位置,塞进对应的.md的指定位置github

      • .vue里取出template&script

一步一步来吧:web

一、将.md文件转为Vue Componet / html

要想在vue中使用.md文件为组件,只须要用loadermd转成Vue Componet便可。npm

这个过程很简单,如下为loader伪代码

const wrapper = content => `
<template>
  <section v-html="content" v-once />
</template>
<script>
export default {
  created() {
    this.content = content
  }
};
</script>
`
module.exports = function(source) { 
  // markdown 编译用的 markdown-it
  return  wrapper(new MarkdownIt().render(source)) 
}

二、找到变量位置,塞进对应的.md的指定位置

1)找到变量位置

使用正则匹配定义的规则,找到被{{:}} 包围的字符串,如上例所示则为‘image.vue?type=template’

2)读取文件

若是是其余.js.html等普通文件,直接使用fs.readFileSync读取替换便可,因是.vue,咱们但愿传入type来获取不一样的块(template、script等)

const replaceResults = (template, baseDir) => { 
  const regexp = new RegExp("\\{\\{:([^\\}]+)\\}\\}", "g")
  return template.replace(regexp, function(match) {
    // 获取文件变量
    match = match.substr(3, match.length - 5)
    let [loadFile, query=''] = match.split('?')
    // 读取文件内容
    const source = fs.readFileSync(path.join(baseDir, loadFile), "utf-8").replace(/[\r\n]*$/, "") 
    if (path.extname(loadFile) === ".vue") { 
      let { type } = loaderUtils.parseQuery(`?${query}`)  
      return replaceVue(source, type) // 根据type提取.vue里的不一样块
    }
    return source // 非.vue直接返回文件内容
  })
};

三、从.vue里取出template&script

const replaceVue = (source, type) => {
  const descriptor = templateCompiler.parseComponent(source)
  const lang = {
    template: 'html',
    script: 'javascript' //,
    // style: 'css'
  }
  return lang[type] && `
  \`\`\`${lang[type]}
  ${descriptor[type].content}
  \`\`\` 
  ` 
}

如若要取一个文件里的多个块,则需屡次调用,考虑到咱们的组件库场景,默认返回template和script(未使用type参数时),
对上面代码进行优化,一次性从.vue中取出多个块

// replaceVue(source, [type])
const replaceVue = (source, types = ['template', 'script']) => {
  const descriptor = templateCompiler.parseComponent(source)
  const lang = {
    template: 'html',
    script: 'javascript' //,
    // style: 'css'
  }
  return types.map(type => lang[type] && `
  \`\`\`${lang[type]}
  ${descriptor[type].content}
  \`\`\` 
  `).join('')
}

大功告成🎉🎉! 那么,如何使用呢?

使用(给我小星星🌟)

安装

npm i vue-markd-loader -D

配置

rules: [{
    test: /\.md$/,
    use: [
      'vue-loader',
      {
        loader: 'vue-markd-loader',
        options: {
          replaceFiles: true , // 默认true, 是否将文件填充进md
          wrapper:true // 默认true,默认输出Vue Component ,false 时输出html片断 
        }
      }
    ]
  }]
// main.js 
import 'highlight.js/styles/github.css' // 可使用任意喜欢的主题哟
import 'github-markdown-css'

其余

第一次撸webpack loader,若有不正确/不足的地方,欢迎指正。

相关文章
相关标签/搜索