ant-design-vue/site/components/demoContainer.vue

81 lines
1.8 KiB
Vue
Raw Normal View History

2018-05-08 11:20:07 +08:00
<template>
2019-02-01 17:23:00 +08:00
<div>
<demo-box :jsfiddle="jsfiddle">
<template slot="component">
<slot />
</template>
<template slot="description">
<div v-html="cnHtml" />
</template>
<template slot="us-description">
<div v-html="usHtml" />
</template>
<template slot="code">
<div v-html="codeHtml" />
</template>
</demo-box>
</div>
2018-05-08 11:20:07 +08:00
</template>
<script>
2019-01-12 11:33:27 +08:00
import marked from 'marked';
const hljs = require('highlight.js');
2019-09-28 20:45:07 +08:00
const replaceDelimiters = function(str) {
2019-01-12 11:33:27 +08:00
return str.replace(/({{|}})/g, '<span>$1</span>');
};
2019-09-28 20:45:07 +08:00
const renderHighlight = function(str, lang) {
2018-05-08 11:20:07 +08:00
if (!(lang && hljs.getLanguage(lang))) {
2019-01-12 11:33:27 +08:00
return '';
2018-05-08 11:20:07 +08:00
}
try {
2019-01-12 11:33:27 +08:00
return replaceDelimiters(hljs.highlight(lang, str, true).value);
2018-05-08 11:20:07 +08:00
} catch (err) {}
2019-01-12 11:33:27 +08:00
};
const renderer = new marked.Renderer();
2019-09-28 20:45:07 +08:00
renderer.heading = function(text, level) {
return (
'<h' + level + ' id="' + text.replace(/[^\w]+/g, '-') + '">' + text + '</h' + level + '>\n'
);
2019-01-12 11:33:27 +08:00
};
2018-05-08 11:20:07 +08:00
marked.setOptions({
renderer,
gfm: true,
tables: true,
breaks: true,
pedantic: true,
sanitize: true,
smartLists: true,
smartypants: true,
html: true,
highlight: renderHighlight,
2019-01-12 11:33:27 +08:00
});
const cnReg = /<cn>([\S\s\t]*?)<\/cn>/;
const usReg = /<us>([\S\s\t]*?)<\/us>/;
2018-05-08 11:20:07 +08:00
export default {
2019-02-01 17:23:00 +08:00
name: 'DemoContainer',
2018-05-08 11:20:07 +08:00
props: ['code'],
2019-09-28 20:45:07 +08:00
data() {
2019-01-12 11:33:27 +08:00
const cn = this.code.match(cnReg) || [];
const us = this.code.match(usReg) || [];
const cnHtml = marked(cn[1].trim());
const usHtml = marked(us[1].trim());
2019-09-28 20:45:07 +08:00
const sourceCode = this.code
.replace(cn[0], '')
.replace(us[0], '')
.trim();
2019-01-12 11:33:27 +08:00
const codeHtml = marked('````html\n' + sourceCode + '````');
2018-05-08 11:20:07 +08:00
return {
codeHtml,
cnHtml,
usHtml,
jsfiddle: {
sourceCode,
cn: cn[1].trim(),
us: us[1].trim(),
},
2019-01-12 11:33:27 +08:00
};
2018-05-08 11:20:07 +08:00
},
2019-01-12 11:33:27 +08:00
};
2018-05-08 11:20:07 +08:00
</script>