🌈 An enterprise-class UI components based on Ant Design and Vue. 🐜
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.4 KiB

7 years ago
const path = require('path')
const hljs = require('highlight.js')
7 years ago
const Token = require('markdown-it/lib/token')
7 years ago
const cheerio = require('cheerio')
7 years ago
const getBabelCommonConfig = require('./antd-tools/getBabelCommonConfig')
const babelConfig = getBabelCommonConfig(false)
babelConfig.plugins.push(require.resolve('babel-plugin-syntax-dynamic-import'))
7 years ago
const fetch = (str, tag, scoped) => {
7 years ago
const $ = cheerio.load(str, { decodeEntities: false, xmlMode: true })
7 years ago
if (!tag) return str
if (tag === 'style') {
return scoped ? $(`${tag}[scoped]`).html() : $(`${tag}`).not(`${tag}[scoped]`).html()
}
7 years ago
return $(tag).html()
}
/**
* `{{ }}` => `<span>{{</span> <span>}}</span>`
* @param {string} str
* @return {string}
*/
const replaceDelimiters = function (str) {
return str.replace(/({{|}})/g, '<span>$1</span>')
}
/**
* renderHighlight
* @param {string} str
* @param {string} lang
*/
const renderHighlight = function (str, lang) {
if (!(lang && hljs.getLanguage(lang))) {
return ''
}
try {
return replaceDelimiters(hljs.highlight(lang, str, true).value)
} catch (err) {}
}
const md = require('markdown-it')('default', {
html: true,
breaks: true,
highlight: renderHighlight,
7 years ago
}).use(require('markdown-it-anchor'), {
level: 2,
7 years ago
slugify: string => string.trim().split(' ').join('-'),
7 years ago
permalink: true,
// renderPermalink: (slug, opts, state, permalink) => {},
permalinkClass: 'anchor',
permalinkSymbol: '#',
permalinkBefore: false,
7 years ago
})
7 years ago
// md.renderer.rules.fence = wrap(md.renderer.rules.fence)
7 years ago
const cnReg = new RegExp('<(cn)(?:[^<]|<)+</\\1>', 'g')
const usReg = new RegExp('<(us)(?:[^<]|<)+</\\1>', 'g')
7 years ago
md.core.ruler.push('update_template', function replace ({ tokens }) {
let cn = ''
let us = ''
let template = ''
let script = ''
let style = ''
let scopedStyle = ''
7 years ago
let code = ''
let sourceCode = ''
7 years ago
tokens.forEach(token => {
if (token.type === 'html_block') {
if (token.content.match(cnReg)) {
cn = fetch(token.content, 'cn')
token.content = ''
}
if (token.content.match(usReg)) {
us = fetch(token.content, 'us')
token.content = ''
}
}
if (token.type === 'fence' && token.info === 'html' && token.markup === '```') {
sourceCode = token.content
7 years ago
code = '````html\n' + token.content + '````'
template = fetch(token.content, 'template')
script = fetch(token.content, 'script')
style = fetch(token.content, 'style')
scopedStyle = fetch(token.content, 'style', true)
7 years ago
token.content = ''
token.type = 'html_block'
}
})
if (template) {
let jsfiddle = {
html: template,
script,
style,
7 years ago
us,
cn,
sourceCode,
7 years ago
}
jsfiddle = md.utils.escapeHtml(JSON.stringify(jsfiddle))
const codeHtml = code ? md.render(code) : ''
const cnHtml = cn ? md.render(cn) : ''
7 years ago
let newContent = `
7 years ago
<template>
<demo-box :jsfiddle="${jsfiddle}">
7 years ago
<template slot="component">${template}</template>
7 years ago
<template slot="description">${cnHtml}</template>
<template slot="us-description">${us ? md.render(us) : ''}</template>
7 years ago
<template slot="code">${codeHtml}</template>
7 years ago
</demo-box>
7 years ago
</template>`
newContent += script ? `
7 years ago
<script>
${script || ''}
</script>
7 years ago
` : ''
newContent += style ? `
<style>
7 years ago
${style || ''}
7 years ago
</style>
` : ''
newContent += scopedStyle ? `
<style scoped>
${scopedStyle || ''}
</style>
` : ''
7 years ago
const t = new Token('html_block', '', 0)
t.content = newContent
tokens.push(t)
}
})
7 years ago
module.exports = {
entry: {
index: [
7 years ago
'./site/index.js',
7 years ago
],
},
module: {
rules: [
{
test: /\.md/,
use: [
{
7 years ago
loader: 'vue-antd-md-loader',
options: Object.assign(md, { wrapper: 'div',
vueLoaderOptions: {
loaders: {
js: [
{ loader: 'babel-loader',
options: {
presets: ['env'],
plugins: [
'transform-vue-jsx',
'transform-object-rest-spread',
],
}},
],
},
},
}),
7 years ago
},
],
},
{
test: /\.vue$/,
loader: 'vue-loader',
7 years ago
options: {
loaders: {
js: [
{ loader: 'babel-loader',
options: {
presets: ['env'],
plugins: [
'transform-vue-jsx',
'transform-object-rest-spread',
'syntax-dynamic-import',
],
}},
],
},
},
7 years ago
},
{
test: /\.(js|jsx)$/,
7 years ago
loader: 'babel-loader', exclude: /node_modules/,
7 years ago
options: babelConfig,
7 years ago
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]',
},
},
],
},
resolve: {
modules: ['node_modules', path.join(__dirname, '../node_modules')],
extensions: ['.js', '.jsx', '.vue', '.md'],
7 years ago
alias: {
'vue$': 'vue/dist/vue.esm.js',
'antd': path.join(__dirname, 'components'),
'vue-antd-ui': path.join(__dirname, 'components'),
7 years ago
'@': path.join(__dirname, ''),
7 years ago
},
},
}