vue-md-loader
Advanced tools
Comparing version 0.1.3 to 0.1.4
{ | ||
"name": "vue-md-loader", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Webpack loader for converting Markdown files to Vue components.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,2 +16,7 @@ # vue-md-loader | ||
## Example | ||
* [Example project](https://github.com/wxsms/vue-md-loader/tree/master/example) | ||
* [https://github.com/wxsms/uiv](https://github.com/wxsms/uiv) | ||
## Install | ||
@@ -33,4 +38,2 @@ | ||
[Example project](https://github.com/wxsms/vue-md-loader/tree/master/example) | ||
### Basic | ||
@@ -146,5 +149,25 @@ | ||
### markdown | ||
Object. | ||
Markdown-It options. Default: | ||
```javascript | ||
{ | ||
html: true, | ||
highlight: function (str, lang) { | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return hljs.highlight(lang, str).value | ||
} catch (__) {} | ||
} | ||
return '' | ||
} | ||
} | ||
``` | ||
### plugins | ||
Array. Default: `[]` | ||
Array. | ||
@@ -169,19 +192,12 @@ Markdown-It plugins list. For example: | ||
### markdown | ||
### rules | ||
Object. | ||
Markdown-It options. Default: | ||
Markdown-It renderer rules. For example: | ||
```javascript | ||
{ | ||
html: true, | ||
highlight: function (str, lang) { | ||
if (lang && hljs.getLanguage(lang)) { | ||
try { | ||
return hljs.highlight(lang, str).value | ||
} catch (__) {} | ||
} | ||
return '' | ||
} | ||
rules: { | ||
'table_open': () => '<div class="table-responsive"><table class="table">', | ||
'table_close': () => '</table></div>' | ||
} | ||
@@ -188,0 +204,0 @@ ``` |
@@ -34,30 +34,40 @@ const MarkdownIt = require('markdown-it') | ||
function Parser (options) { | ||
function Parser (_options) { | ||
// default options | ||
const defaultOptions = { | ||
// live options | ||
live: true, | ||
live: true, // enable live | ||
livePattern: /<!--[\s]*?([-\w]+?).vue[\s]*?-->/i, | ||
liveTemplateProcessor: null, | ||
// markdown options | ||
markdown: Object.assign({}, DEFAULT_MARKDOWN_OPTIONS), | ||
// markdown plugins | ||
plugins: [], | ||
// others | ||
wrapper: 'section' | ||
markdown: Object.assign({}, DEFAULT_MARKDOWN_OPTIONS), // Markdown-It options | ||
rules: {}, // Markdown-It rules | ||
plugins: [], // Markdown-It plugins | ||
wrapper: 'section' // content wrapper | ||
} | ||
// merge user options into defaults | ||
this.options = Object.assign({}, defaultOptions, options) | ||
this.markdown = new MarkdownIt(Object.assign({}, this.options.markdown)) | ||
const options = Object.assign({}, defaultOptions, _options) | ||
options.markdown = Object.assign({}, options.markdown) | ||
this.options = options | ||
// init MarkdownIt instance | ||
this.markdown = new MarkdownIt(this.options.markdown) | ||
// v-pre must be set | ||
ensureVPre(this.markdown) | ||
// apply rules | ||
if (this.options.rules) { | ||
let rendererRules = this.markdown.renderer.rules | ||
let userRules = this.options.rules | ||
for (let key in userRules) { | ||
if (userRules.hasOwnProperty(key) && typeof userRules[key] === 'function') { | ||
rendererRules[key] = userRules[key] | ||
} | ||
} | ||
} | ||
// install plugins | ||
if (this.options.plugins && this.options.plugins.length) { | ||
let markdown = this.markdown | ||
// Apply plugins to markdown instance | ||
this.options.plugins.forEach(function (p) { | ||
this.options.plugins.forEach(p => { | ||
if (Array.isArray(p)) { | ||
if (p[0]) { | ||
markdown.use.apply(markdown, p) | ||
this.markdown.use.apply(this.markdown, p) | ||
} | ||
} else if (p) { | ||
markdown.use(p) | ||
this.markdown.use(p) | ||
} | ||
@@ -99,4 +109,3 @@ }) | ||
Parser.prototype.fetchLiveTemplates = function () { | ||
let self = this | ||
this.lives.forEach(function (live) { | ||
this.lives.forEach(live => { | ||
// greedy | ||
@@ -112,4 +121,4 @@ let template = /<template>([\s\S]*)<\/template>/.exec(live[1]) | ||
// Wrap it by options | ||
if (self.options.liveTemplateProcessor && typeof self.options.liveTemplateProcessor === 'function') { | ||
template = self.options.liveTemplateProcessor(template) | ||
if (this.options.liveTemplateProcessor && typeof this.options.liveTemplateProcessor === 'function') { | ||
template = this.options.liveTemplateProcessor(template) | ||
} | ||
@@ -122,3 +131,3 @@ // mount it to the live obj | ||
Parser.prototype.fetchLiveScripts = function () { | ||
this.lives.forEach(function (live) { | ||
this.lives.forEach(live => { | ||
// mount script inside live block | ||
@@ -130,3 +139,3 @@ live._script = /<script.*?>([\S\s]+?)<\/script>/.exec(live[1]) | ||
Parser.prototype.fetchLiveStyles = function () { | ||
this.lives.forEach(function (live) { | ||
this.lives.forEach(live => { | ||
// mount style inside live block | ||
@@ -168,3 +177,3 @@ live._style = /<style.*?>([\S\s]+?)<\/style>/.exec(live[1]) | ||
let style = '' | ||
this.lives.forEach(function (live) { | ||
this.lives.forEach(live => { | ||
style += live._style ? live._style[0] : '' | ||
@@ -179,4 +188,3 @@ }) | ||
let beforeExports = [] | ||
let self = this | ||
this.lives.forEach(function (live, index) { | ||
this.lives.forEach((live, index) => { | ||
let componentOptions = null | ||
@@ -199,7 +207,7 @@ if (live._script) { | ||
// Add ',' if not the last live component | ||
exports += index === self.lives.length - 1 ? '' : ',' | ||
exports += index === this.lives.length - 1 ? '' : ',' | ||
} | ||
}) | ||
exports = `export default {components:{${exports}}}` | ||
beforeExports.forEach(function (code) { | ||
beforeExports.forEach(code => { | ||
script += code + NEW_LINE | ||
@@ -206,0 +214,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14114
513
213
242