cooking.demo.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // fix publicPath
  44. if (!process.env.CI_ENV) {
  45. cooking.add('output.publicPath', '');
  46. }
  47. cooking.add('loader.md', {
  48. test: /\.md$/,
  49. loader: 'vue-markdown-loader'
  50. });
  51. cooking.add('vueMarkdown', {
  52. use: [
  53. [require('markdown-it-anchor'), {
  54. level: 2,
  55. slugify: slugify,
  56. permalink: true,
  57. permalinkBefore: true
  58. }],
  59. [require('markdown-it-container'), 'demo', {
  60. validate: function(params) {
  61. return params.trim().match(/^demo\s*(.*)$/);
  62. },
  63. render: function(tokens, idx) {
  64. var m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
  65. if (tokens[idx].nesting === 1) {
  66. var description = (m && m.length > 1) ? m[1] : '';
  67. var content = tokens[idx + 1].content;
  68. var html = convert(striptags.strip(content, ['script', 'style'])).replace(/(<[^>]*)=""(?=.*>)/g, '$1');
  69. var script = striptags.fetch(content, 'script');
  70. var style = striptags.fetch(content, 'style');
  71. var jsfiddle = { html: html, script: script, style: style };
  72. var descriptionHTML = description
  73. ? md.render(description)
  74. : '';
  75. jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle));
  76. return `<demo-block class="demo-box" :jsfiddle="${jsfiddle}">
  77. <div class="source" slot="source">${html}</div>
  78. ${descriptionHTML}
  79. <div class="highlight" slot="highlight">`;
  80. }
  81. return '</div></demo-block>\n';
  82. }
  83. }]
  84. ],
  85. preprocess: function(MarkdownIt, source) {
  86. MarkdownIt.renderer.rules.table_open = function() {
  87. return '<table class="table">';
  88. };
  89. MarkdownIt.renderer.rules.fence = wrap(MarkdownIt.renderer.rules.fence);
  90. return source;
  91. }
  92. });
  93. var wrap = function(render) {
  94. return function() {
  95. return render.apply(this, arguments)
  96. .replace('<code class="', '<code class="hljs ')
  97. .replace('<code>', '<code class="hljs">');
  98. };
  99. };
  100. if (isProd) {
  101. cooking.add('externals.vue', 'Vue');
  102. cooking.add('externals.vue-router', 'VueRouter');
  103. }
  104. cooking.add('vue.preserveWhitespace', false);
  105. module.exports = cooking.resolve();