Coder Social home page Coder Social logo

wxsms / vue-md-loader Goto Github PK

View Code? Open in Web Editor NEW
118.0 3.0 56.0 2.79 MB

:sparkles: Markdown files to ALIVE Vue components.

Home Page: https://vue-md-loader.wxsm.space

License: MIT License

JavaScript 49.81% HTML 2.26% Vue 3.83% CSS 44.10%
vue markdown webpack webpack-loader document

vue-md-loader's Introduction

vue-md-loader

CI NPM Downloads NPM Version License

loader version webpack version
2.x 5
1.x 4 and lower

Introduction

Webpack loader for converting Markdown files to ALIVE Vue components.

  • Configurable Markdown-It parser.
  • Built-in syntax highlighter with highlightjs.
  • Live demo✨ support. Extremely useful for document examples.
  • Vue 3 & vue-cli usage support.
  • Hot reload.

Example

This page (vue-md-loader.wxsm.space) is generated by a markdown file. Source: /example.

There is also a Vue 3 & Vue-cli example: /example-vue3.

Install

NPM:

npm install vue-md-loader --save-dev

Yarn:

yarn add vue-md-loader --dev

Usage

Basic

Simply use vue-md-loader to load .md files and chain it with your vue-loader.

module.exports = {
  // ...
  module: {
    rules: [
      // ...
      {
        test: /\.md$/,
        loader: 'vue-loader!vue-md-loader'
      }
    ]
  }
}

Note that to get code highlighting to work, you need to:

  • include one of the highlight.js css files into your project, for example: highlight.js/styles/github-gist.css.
  • specify a lang in code block. ref: creating and highlighting code blocks.

With Options

module.exports = {
  // ...
  module: {
    rules: [
      // ...
      {
        test: /\.md$/,
        loaders: [
          'vue-loader',
          {
            loader: 'vue-md-loader',
            options: {
              // your preferred options
            }
          }
        ]
      }
    ]
  }
}

✨ Markdown Alive!

A live demo is:

<template>
  <div class="cls">{{msg}}</div>
</template>
<script>
  export default {
    data () {
      return {
        msg: 'Hello world!'
      }
    }
  }
</script>
<style>
  .cls {
    color: red;
    background: green;
  }
</style>
<!-- some-live-demo.vue -->

becomes something like:

<some-live-demo/>
<pre><code>...</code></pre>

A Vue component with all it's <template>, <script> and <style> settled will be inserted before it's source code block.

Multiple lives inside a single markdown file is supported by:

  • All <script> from different code blocks:
    • code inside export default will be extract into it's own Vue component with no conflicts.
    • code before export default will be extract into the same top-level component.
  • All <style> from different code blocks will be extract into the same top-level component.

Note:

  • Loader will treat the entire block as template if no <template> found in live block.
  • You will need runtime + compiler build of Vue.js for this feature. For example:
module.exports = {
  // ...
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js' // 'vue/dist/vue.common.js' for webpack 1
    }
  }
}

Options

wrapper

String. Default: section

The wrapper of entire markdown content, can be HTML tag name or Vue component name.

markdown

Object.

Markdown-It options. Default:

{
  html: true,
  highlight: function (str, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(str, { language: lang }).value
      } catch (__) {}
    }
    return ''
  }
}

plugins

Array.

Markdown-It plugins list. For example:

// ...
plugins: [
  // Without option
  require('markdown-it-plugin-1'),
  // With options
  [
    require('markdown-it-plugin-2'),
    {
      // ...
    }
  ]
]
// ...

rules

Object.

Markdown-It renderer rules. For example:

rules: {
  'table_open': () => '<div class="table-responsive"><table class="table">',
  'table_close': () => '</table></div>'
}

preProcess

Function. For example:

preProcess: function(source) {
  // do anything
  return source
}

process

Function. For example:

// This is useful when used with front-matter-loader to set the page title in nuxt projects
process: function(source){
  let attrs = (source && source.attributes) || {}
  attrs.title = attrs.title || ""
  return {
    template: source.body,
    style: "",
    script: `export default {
      head(){
        return {
          title: '${attrs.title}'
        }
      }
    }`
  }
}

afterProcess

Function. For example:

afterProcess: function(result) {
  // do anything
  return result
}

live

Boolean. Default: true

Enable / Disable live detecting and assembling.

livePattern

Regex. Default: /<!--[\s]*?([-\w]+?).vue[\s]*?-->/i

A code block with livePattern inside itself becomes a live block. The matched body will become the live Vue component's name and reference (note that they must be unique to each other within the same page).

afterProcessLiveTemplate

Function. Default: null

Use this if you wish to change the live template manually after process (e.g. add wrappers). For example:

afterProcessLiveTemplate: function(template) {
  return `<div class="live-wrapper">${template}</div>`
}

Build Setup

# install dependencies
npm install

# serve example with hot reload at localhost:8888
npm run dev

# run all tests
npm test

License

MIT

vue-md-loader's People

Contributors

dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar shiling avatar wxsms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue-md-loader's Issues

Handle loading state

Hi. I'm looking for a little guidance here.

I'm trying to handle the loading state of my markdown component because it takes a moment to process, but the compiler can't find my markdown files when I use the advanced async component factory syntax (Vue 2).

https://vuejs.org/v2/guide/components-dynamic-async.html#Handling-Loading-State

This WORKS:

<script>
const ArticleContent = () =>
  import("@/assets/articles/test-article/article.md");

export default {
  name: "Article",
  components: {
    ArticleContent,
  ...

This DOES NOT work:

<script>
const ArticleContent = () => ({
  component: import("@/assets/articles/test-article/article.md"),
  loading: LoaderComponent,
  error: ErrorComponent,
  delay: 200,  
  timeout: 3000
});

export default {
  name: "Article",
  components: {
    ArticleContent,
  ...

The 2nd method works fine with .vue files, but when I try to import a .md file with vue-md-loader, I get the error:

This dependency was not found:

* @/assets/articles/test-article/article.md in ./node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/babel-loader/lib!./node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./src/components/Article.vue?vue&type=script&lang=js&

I have of course tried both @/path/to/article.md and ../path/to/article.md.

Any idea why this isn't working, or if I should use some other technique?

Thank you

请问webpack对应如何配置,才能解析 markdown 加上样式

请问webpack对应如何配置,才能解析 markdown 加上样式

t.md

image

解析后

image

我的webpack是这么配置的,和你的demo配置的一样

  {
        test: /\.md$/,
        loaders: [
          'vue-loader',
          {
            loader: 'vue-md-loader',
            options: {
              preProcess (source) {
                console.log('pre', source)
                return source
              },
              afterProcess (result) {
                console.log('after', result)
                return result
              },
              afterProcessLiveTemplate (template) {
                return `<div class="live-wrapper">${template}</div>`
              },
              rules: {
                'table_open': () => '<div class="table-responsive"><table class="table">',
                'table_close': () => '</table></div>'
              },
              plugins: [
                [
                  require('markdown-it-anchor'),
                  {
                    permalink: true,
                    permalinkSymbol: '&#128279;'
                  }
                ]
              ]
            }
          }
        ]
      }

Usage

<template>
  <section>
    <tmd></tmd>
  </section>
</template>
<script>

  import tmd from '../../doc/t.md'
  export default {
    components: {
      tmd
    }
  }

这样是能将 markdown 解析成一个 components ,但没加上对应样式,都是默认的 html 标签,怎么加上样式呢,看你的源码有用到 highlight.js ,但没效果,还需如何配置呢

启动不起来

这是 基于 webpack 的包吗?
vite 创建 ts + vue3 的项目不支持的吗?
配置各种报错。。。。

我现在很需要这个库,之前找了好久,今天在 element3 的项目中发现了。

Module build failed after update to 2.0.1

Hi,
I am getting the below error on 2.0.1, but not on 2.0.0.

Error: Module build failed (from ./node_modules/vue-md-loader/index.js):
TypeError: this.getOptions is not a function
    at Object.module.exports (E:\git\project\node_modules\vue-md-loader\index.js:7:24)
    at eval (webpack-internal:///./src/assets/markdown.md:1:7)
    at Object../src/assets/markdown.md (https://localhost/js/app.js:5881:1)
    at __webpack_require__ (https://localhost/js/app.js:854:30)
    at fn (https://localhost/js/app.js:151:20)
    at VueComponent.created 
    ...

I could be totally wrong, but looking at the source and what changed, I would guess that loader-utils should not have been removed like they were in 59ae702. The v3 way of calling getOptions() makes it seem like we don't need the dependency but I guess we do.

Downgrading fixes it for me, but I thought it was worth pointing out anyway.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Pending Branch Automerge

These updates await pending status checks before automerging. Click on a checkbox to abort the branch automerge, and create a PR instead.

  • chore(deps): update dependency @babel/core to v7.17.2
  • chore(deps): update dependency jest to v27.5.1
  • chore(deps): update vue monorepo to v3.2.30 (@vue/compiler-sfc, vue)
  • chore(deps): update pnpm/action-setup action to v2.1.0

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.


  • Check this box to trigger a request for Renovate to run again on this repository

Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.

当我在md文件上写vue代码的时候,发现他会报错
我的写法:

index.md

## test
<template></template>
<script></script>

错误:
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.

请问怎么解决,我看vue-markdown-loader这个是可以渲染的

Vue 3 Support

Hello, is it possible to use this plugin with Vue 3?

I have tried several things, but always get errors.

e.g. when using this webpack.config.js:

image

or this vue.config.js:

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.