element/build/cooking.demo.js

112 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-07-27 14:15:02 +08:00
var cooking = require('cooking');
var config = require('./config');
2016-08-20 00:38:59 +08:00
var md = require('markdown-it')();
var striptags = require('./strip-tags');
2016-11-04 16:59:59 +08:00
var slugify = require('transliteration').slugify;
2016-11-05 19:56:34 +08:00
var isProd = process.env.NODE_ENV === 'production';
2016-11-14 14:10:58 +08:00
var isPlay = !!process.env.PLAY_ENV;
2016-07-27 14:15:02 +08:00
function convert(str) {
str = str.replace(/(&#x)(\w{4});/gi, function($0) {
return String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16));
2016-08-23 16:57:58 +08:00
});
return str;
}
2016-07-27 14:15:02 +08:00
cooking.set({
2016-11-05 19:56:34 +08:00
entry: isProd ? {
docs: './examples/entry.js',
'element-ui': './src/index.js'
2016-11-14 14:10:58 +08:00
} : (isPlay ? './examples/play.js' : './examples/entry.js'),
2016-07-27 14:15:02 +08:00
dist: './examples/element-ui/',
2016-11-09 17:57:00 +08:00
template: [
{
template: './examples/index.tpl',
filename: './index.html',
favicon: './examples/favicon.ico'
}
],
2016-11-17 12:09:43 +08:00
publicPath: process.env.CI_ENV || '',
2016-07-27 14:15:02 +08:00
hash: true,
devServer: {
port: 8085,
log: false,
publicPath: '/'
},
2016-09-06 11:51:08 +08:00
minimize: true,
2016-11-05 19:56:34 +08:00
chunk: isProd ? {
'common': { name: ['element-ui', 'manifest'] }
} : false,
2016-07-27 14:15:02 +08:00
extractCSS: true,
alias: config.alias,
2016-09-06 11:51:08 +08:00
extends: ['vue2', 'lint'],
2016-10-11 19:00:37 +08:00
postcss: config.postcss
2016-07-27 14:15:02 +08:00
});
cooking.add('loader.md', {
test: /\.md$/,
loader: 'vue-markdown-loader'
});
cooking.add('vueMarkdown', {
use: [
2016-11-04 16:59:59 +08:00
[require('markdown-it-anchor'), {
level: 2,
slugify: slugify,
permalink: true,
permalinkBefore: true
}],
2016-08-22 14:04:03 +08:00
[require('markdown-it-container'), 'demo', {
2016-08-20 00:38:59 +08:00
validate: function(params) {
2016-08-23 16:57:58 +08:00
return params.trim().match(/^demo\s*(.*)$/);
2016-08-20 00:38:59 +08:00
},
render: function(tokens, idx) {
2016-08-23 16:57:58 +08:00
var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
2016-08-20 00:38:59 +08:00
if (tokens[idx].nesting === 1) {
2016-08-23 13:46:51 +08:00
var description = (m && m.length > 1) ? m[1] : '';
2016-11-02 18:47:19 +08:00
var content = tokens[idx + 1].content;
2016-11-16 14:09:25 +08:00
var html = convert(striptags.strip(content, ['script', 'style'])).replace(/(<[^>]*)=""(?=.*>)/g, '$1');
2016-11-02 18:47:19 +08:00
var script = striptags.fetch(content, 'script');
var style = striptags.fetch(content, 'style');
var jsfiddle = { html: html, script: script, style: style };
2016-08-23 13:46:51 +08:00
var descriptionHTML = description
2016-11-08 15:59:02 +08:00
? md.render(description)
2016-08-23 13:46:51 +08:00
: '';
2016-11-02 18:47:19 +08:00
jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
return `<demo-block class="demo-box" :jsfiddle="${jsfiddle}">
2016-11-08 15:59:02 +08:00
<div class="source" slot="source">${html}</div>
${descriptionHTML}
<div class="highlight" slot="highlight">`;
2016-08-20 00:38:59 +08:00
}
2016-11-08 15:59:02 +08:00
return '</div></demo-block>\n';
2016-08-20 00:38:59 +08:00
}
2016-07-27 14:15:02 +08:00
}]
],
preprocess: function(MarkdownIt, source) {
MarkdownIt.renderer.rules.table_open = function() {
2016-07-27 14:15:02 +08:00
return '<table class="table">';
};
MarkdownIt.renderer.rules.fence = wrap(MarkdownIt.renderer.rules.fence);
return source;
}
});
var wrap = function(render) {
return function() {
2016-07-27 14:15:02 +08:00
return render.apply(this, arguments)
.replace('<code class="', '<code class="hljs ')
.replace('<code>', '<code class="hljs">');
2016-07-27 14:15:02 +08:00
};
};
2016-11-05 19:56:34 +08:00
if (isProd) {
2016-09-21 14:38:07 +08:00
cooking.add('externals.vue', 'Vue');
cooking.add('externals.vue-router', 'VueRouter');
}
2016-10-22 00:45:18 +08:00
cooking.add('vue.preserveWhitespace', false);
2016-07-27 14:15:02 +08:00
module.exports = cooking.resolve();