cooking.demo.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var cooking = require('cooking');
  2. var config = require('./config');
  3. var md = require('markdown-it')();
  4. var striptags = require('./strip-tags');
  5. var slugify = require('transliteration').slugify;
  6. var isProd = process.env.NODE_ENV === 'production';
  7. var isPlay = !!process.env.PLAY_ENV;
  8. function convert(str) {
  9. str = str.replace(/(&#x)(\w{4});/gi, function($0) {
  10. return String.fromCharCode(parseInt(encodeURIComponent($0).replace(/(%26%23x)(\w{4})(%3B)/g, '$2'), 16));
  11. });
  12. return str;
  13. }
  14. cooking.set({
  15. entry: isProd ? {
  16. docs: './examples/entry.js',
  17. 'element-ui': './src/index.js'
  18. } : (isPlay ? './examples/play.js' : './examples/entry.js'),
  19. dist: './examples/element-ui/',
  20. template: [
  21. {
  22. template: './examples/index.tpl',
  23. filename: './index.html',
  24. favicon: './examples/favicon.ico'
  25. }
  26. ],
  27. publicPath: process.env.CI_ENV || '/',
  28. hash: true,
  29. devServer: {
  30. port: 8085,
  31. log: false,
  32. publicPath: '/'
  33. },
  34. minimize: true,
  35. chunk: isProd ? {
  36. 'common': { name: ['element-ui', 'manifest'] }
  37. } : false,
  38. extractCSS: true,
  39. alias: config.alias,
  40. extends: ['vue2', 'lint'],
  41. postcss: config.postcss
  42. });
  43. cooking.add('loader.md', {
  44. test: /\.md$/,
  45. loader: 'vue-markdown-loader'
  46. });
  47. cooking.add('vueMarkdown', {
  48. use: [
  49. [require('markdown-it-anchor'), {
  50. level: 2,
  51. slugify: slugify,
  52. permalink: true,
  53. permalinkBefore: true
  54. }],
  55. [require('markdown-it-container'), 'demo', {
  56. validate: function(params) {
  57. return params.trim().match(/^demo\s*(.*)$/);
  58. },
  59. render: function(tokens, idx) {
  60. var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
  61. if (tokens[idx].nesting === 1) {
  62. var description = (m && m.length > 1) ? m[1] : '';
  63. var content = tokens[idx + 1].content;
  64. var html = convert(striptags.strip(content, ['script', 'style'])).replace(/(<[^>]*)=""(?=.*>)/g, '$1');
  65. var script = striptags.fetch(content, 'script');
  66. var style = striptags.fetch(content, 'style');
  67. var jsfiddle = { html: html, script: script, style: style };
  68. var descriptionHTML = description
  69. ? md.render(description)
  70. : '';
  71. jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
  72. return `<demo-block class="demo-box" :jsfiddle="${jsfiddle}">
  73. <div class="source" slot="source">${html}</div>
  74. ${descriptionHTML}
  75. <div class="highlight" slot="highlight">`;
  76. }
  77. return '</div></demo-block>\n';
  78. }
  79. }]
  80. ],
  81. preprocess: function(MarkdownIt, source) {
  82. MarkdownIt.renderer.rules.table_open = function() {
  83. return '<table class="table">';
  84. };
  85. MarkdownIt.renderer.rules.fence = wrap(MarkdownIt.renderer.rules.fence);
  86. return source;
  87. }
  88. });
  89. var wrap = function(render) {
  90. return function() {
  91. return render.apply(this, arguments)
  92. .replace('<code class="', '<code class="hljs ')
  93. .replace('<code>', '<code class="hljs">');
  94. };
  95. };
  96. if (isProd) {
  97. cooking.add('externals.vue', 'Vue');
  98. cooking.add('externals.vue-router', 'VueRouter');
  99. }
  100. cooking.add('vue.preserveWhitespace', false);
  101. module.exports = cooking.resolve();