cooking.demo.js 2.9 KB

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