npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@vusion/md-vue-loader

v1.4.1

Published

Webpack loader for converting Markdown files to alive Vue components.

Downloads

255

Readme

@vusion/md-vue-loader

NPM Version Dependencies NPM Download

Webpack loader for converting Markdown files to alive Vue components.

Features

  • Live vue/html code blocks
  • Cache markdown component and code block components
  • Hot reload
  • Built-in syntax highlighter with highlightjs
  • Code block style modifier
  • Configurable markdown-it parser

Example

Live Code Blocks

Support two kinds of code blocks to live:

  1. html code
<u-button>Button</u-button>
<u-input></u-input>
  1. vue code
<template>
<div :class="$style.root">
    <u-button>Button</u-button>
    <u-input v-model="value"></u-input>
</div>
</template>
<script>
export default {
    data() {
        return {
            value: 'Hello world!',
        };
    },
};
</script>
<style module>
.root {
    width: 200px;
    background: #eee;
}
</style>

Style Modifier

You can attach more styles enclosed in braces after code block lang. For example: html {width: 30%}.

<u-button>Button</u-button>
<u-textarea></u-textarea>

Install

npm i -D @vusion/md-vue-loader

Usage

Basic

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

module.exports = {
    module: {
        rules: [{
            test: /\.md$/,
            loader: 'vue-loader!@vusion/md-vue-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: (https://highlightjs.org/static/demo/styles/atom-one-dark.css).
  • Specify a lang in code block. Ref: creating and highlighting code blocks).

With Options

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

Resource Query

Remember that you can override options in markdown files query.

const routes = [
    { path: 'article', component: import('./article.md?live=false') },
]

Vue CLI 3

Just chain @vusion/md-vue-loader with vue-loader in your vue.config.js file:

module.exports = {
    chainWebpack(config) {
        config.module.rule('md')
            .test(/\.md$/)
            .use('vue-loader')
            .loader('vue-loader')
            .end()
            .use('@vusion/md-vue-loader')
            .loader('@vusion/md-vue-loader')
            .end();
    },
};

Options

live

Enable/Disable live detecting and assembling vue/html code blocks.

  • Type: boolean
  • Default: true

codeProcess

Process after fetching live components from code blocks

  • Type: Function
  • Default: null
  • @param {string} live - code of live components
  • @param {string} code - highlighted code of raw content
  • @param {string} content - raw content
  • @param {string} lang - code block lang
  • @param {string} modifier - string enclosed in braces after lang. Used to modify style by defaults. Actually, You can do whatever you want, but take care about XSS.

For example:

codeProcess(live, code, content, lang, modifier) {
    // do anything
    return `<div${modifier ? ' style="' + modifier + '"' : ''}>${live}</div>` + '\n\n' + code;
}

For another example, suppose you have a complex container component called <code-example>, with some useful slots.

codeProcess(live, code, content, lang, modifier) {
    // do anything
    return `<code-example lang="${lang}">
    <div${modifier ? ' style="' + modifier + '"' : ''}>${live}</div>
    <div slot="code">${code}</div>
</code-example>\n\n`;
}

wrapper

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

  • Type: string
  • Default: 'section'

markdown

markdown-it options.

  • Type: Object
  • Default:
{
    html: true,
    langPrefix: 'lang-',
    highlight: (content, lang, modifier) => {
        content = content.trim();
        lang = lang.trim();

        let hlLang = lang;
        if (lang === 'vue')
            hlLang = 'html';

        let code = '';
        if (hlLang && hljs.getLanguage(hlLang)) {
            try {
                const result = hljs.highlight(hlLang, content).value;
                code = `<pre class="hljs ${markdown.options.langPrefix}${lang}"><code>${result}</code></pre>\n`;
            } catch (e) {}
        } else {
            const result = markdown.utils.escapeHtml(content);
            code = `<pre class="hljs"><code>${result}</code></pre>\n`;
        }

        const live = this.options.live ? this.liveComponent(lang, content) : '';
        return this.options.codeProcess.call(this, live, code, content, lang, modifier);
    },
};

plugins

markdown-it plugins list.

  • Type: Array
  • Default: []

For example:

plugins: [
    require('markdown-it-task-lists'),
],

rules

markdown-it renderer rules.

  • Type: Object
  • Default: {}

For example:

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

preprocess

Process before converting.

  • Type: Function
  • Default: null
  • @param {string} source - Markdown source content

For example:

preprocess(source) {
  // do anything
  return source
}

postprocess

Process after converting.

  • Type: Function
  • Default: null
  • @param {string} result - Final converted result

For example:

postprocess(result) {
  // do anything
  return result
}
  • Type: Function
  • Default: null

Developing

test

npm run test
open test/index.html

test:options

npm run test:options
open test/index.html

test:plugins

npm run test:plugins
open test/index.html

test:dev

npm run test:dev

Changelog

See Releases

Contributing

See Contributing Guide

Reference

License

MIT