-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.ts
825 lines (761 loc) · 30.8 KB
/
doc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import fs from "fs";
import _path from "path";
const siteMap = []
import MarkdownIt from "markdown-it";
import mdContainer from "./markdown-it-container";
import mdAnchor from "./markdown-it-anchor";
import mdToc from "./markdown-it-anchor/toc";
import {resolveHighlightLines, isHighlightLine} from "./highlightLines";
import * as csstree from 'css-tree';
const pathResolve = (pathStr:string) => {
return _path.resolve(__dirname, pathStr);
};
// import from "axios"
// import _G from '../../../global'
// import _routes from "../../../src/router/routers"
import hljs from './highlight/highlight.js';
import { mkdirPath,delPath,isExternalLinks,getImageInfo} from "./tools/files";
function createRedirectHtml(root:string, path:string){
let content = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<script>
window.location.href = "/?redirect=${path}";
</script>
<body>
</body>
</html>
`
return new Promise(function(resolve:Function,reject){
// console.log(path);
mkdirPath(root+'/public'+path)
// mkdirs(root,'./pubilc'+path,()=>{
try{
fs.writeFile(root+'/public'+path+'/index.html',content, "utf-8",function(err: any){
if(err){
reject(err)
}else{
resolve()
}
})
}catch{
resolve()
}
// })
// mkdirPath()
})
}
export function createRedirect(root){
siteMap.forEach((item:any)=>{
if(item.children){
item.children.forEach((ele:any)=>{
createRedirectHtml(root,item.path+'/'+ele.path)
})
}else{
createRedirectHtml(root,item.path)
}
})
}
var docSite:any = {TimeID: new Date().getTime(),dataList:[]};
var docSiteObj:any = {}
const docRule = /^\/@docs\/(.*?).md$/;
var stripImportCheck = {}
function writeFs(path: string,content: { TimeID: number; dataList: never[]; }){
return new Promise(function(resolve:Function,reject){
fs.writeFile(path,JSON.stringify(content, null , 2), "utf-8",function(err: any){
if(err){
reject(err)
}else{
resolve()
}
})
})
}
function writeDemo(path: string,content: string){
return new Promise(function(resolve:Function,reject){
fs.writeFile(path,content, "utf-8",function(err: any){
if(err){
reject(err)
}else{
resolve()
}
})
})
}
function parserJsx(code:string){
let parser = require("babel-core").transform(code, {
plugins: ["transform-react-jsx"]
});
return parser.code
}
const demoContainer = (md: { render: (arg0: any) => any; }, callback: (arg0: any) => any) => ({
validate(params: string) {
return params.trim().match(/^demo\s*(.*)$/);
},
render(tokens:any, idx: string | number) {
if (tokens[idx].nesting === 1) {
const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/);
docSite.dataList.push(m)
const description = m && m.length > 1 ? m[1] : "";
description && callback(md.render(description));
return "";
}
return "";
},
});
const detailsContainer = {
validate: function(params) {
return params.trim().match(/^details\s+(.*)$/);
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^details\s+(.*)$/);
if (tokens[idx].nesting === 1) {
// opening tag
return '<details class="fv-state-details"><summary>' + m[1] + '</summary>\n';
} else {
// closing tag
return '</details>\n';
}
}
}
const tipContainer ={
validate: function(params) {
return params.trim().match(/^tip\s+(.*)$/);
},
render: function (tokens, idx) {
var m = tokens[idx].info.trim().match(/^tip\s+(.*)$/);
if (tokens[idx].nesting === 1) {
// opening tag
return '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n';
} else {
// closing tag
return '</details>\n';
}
}
}
const demoTinymceVue3Container = (md: { render: (arg0: any) => any; }, callback: { (description: any): void; (arg0: any): any; }) => ({
validate(params: string) {
return params.trim().match(/^tinymce-vue3\s*(.*)$/);
},
render(tokens: any, idx: string | number) {
if (tokens[idx].nesting === 1) {
callback('')
return "";
}
return "";
},
});
const demoTinymceVue2Container = (md: { render: (arg0: any) => any; }, callback: (arg0: any) => any) => ({
validate(params: string) {
return params.trim().match(/^tinymce-vue2/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
callback('')
return "";
}
return "";
},
});
const demoTinymceReactContainer = (md: { render: (arg0: any) => any; }, callback: (arg0: any) => any) => ({
validate(params: string) {
return params.trim().match(/^tinymce-react/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
callback('')
return "";
}
return "";
},
});
let currentDemoList={}
let currentBoxType = '';
let currentBoxTypeDemo ={}
const demoTinymceBoxContainer = (md: { render: (arg0: any) => any; }, callback: (arg0: any) => any) => ({
validate(params: string) {
return params.trim().match(/^tinymce-box/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
currentBoxType="tinymce-box"
callback('')
return "<codeBox>";
}
currentBoxType=""
let _styleStr = currentBoxTypeDemo['css']
_styleStr = _styleStr.replace(/<\/?.+?\/?>/g,'')
let _jsStr = currentBoxTypeDemo['js']
_jsStr = _jsStr.replace(/<\/?.+?\/?>/g,'')
let _htmlStr = currentBoxTypeDemo['html']
const tinymceDmeoheight = getHeight(_jsStr);
let _code =`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>${_styleStr}</style>\n
<script src="/tinymce/tinymce.js"></script>
<script src="/tinymce-plugin/tinymce-plugin.js"></script>
</head><body style="padding-bottom: 20px"> ${_htmlStr}<script> ${_jsStr}</script></body></html>`
let demoSrc = '/demo/codebox/demo-'+currentBoxTypeDemo.fileName+ new Date().getTime()+'/'
mkdirPath('./public'+demoSrc)
writeDemo('./public'+demoSrc+'index.html',_code)
currentDemoList[currentBoxTypeDemo.fileName]&¤tDemoList[currentBoxTypeDemo.fileName].length>0? currentDemoList[currentBoxTypeDemo.fileName].push(demoSrc):currentDemoList[currentBoxTypeDemo.fileName] = [demoSrc]
currentBoxTypeDemo={}
return `<codeBoxItem title="TinyMCE"><iframe scrolling="no" style="width:100%;margin:auto 0;border:0px; min-height: ${tinymceDmeoheight.tpHeight||tinymceDmeoheight.min_height||tinymceDmeoheight.min_height||'220'}px" onload="autoIframeHeight(this)" src="${demoSrc}index.html"></iframe></codeBoxItem></codeBox>`;
},
});
const demoTinymceIframeContainer = (md: { render: (arg0: any) => any; }, callback: (arg0: any) => any) => ({
validate(params: string) {
return params.trim().match(/^tinymce$/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
callback('')
return "";
}
return "";
},
});
function stripImport(content: string) {
const result = content.match(/import(.*);/gi);
let code = ''
result&&result.forEach(ele=>{
let importList = ele.trim().replace(/import ([\s\S]+) from ([\s\S]+);/g,'$1').trim().replace(/{([\s\S]+)}/g,'$1').trim().split(',')
let importStr = ele
// console.log(importList);
let delCount = 0
importList.forEach(_i=>{
let _import = _i.trim()
if(!stripImportCheck[_import]){
stripImportCheck[_import] = true
}else{
delCount++
importStr = importStr.replace(new RegExp( _i+','),'').replace(new RegExp( _i),'')
}
})
delCount < importList.length?(code+=(importStr.trim()+'\n')):''
})
return code;
}
function getHeight(content:string) {
let result = {}
let contentList = content.split('tinymce.init({')
contentList&&contentList[0].replace(/<* [^>]*tp-page-height=['"]([^'"]+)[^>]*>/,(macth,val)=>{
result['tpHeight'] = parseInt(val)+21
return ''
})
contentList&&contentList.length>1&&contentList[1].replace(/min_height\:(.*)/g,(macth,val)=>{
result["min_height"] = parseInt(val) + 21
return ''
}).replace(/\nheight\:(.*)/g,(macth,val)=>{
result['height'] = parseInt(val) + 21
return ''
})
return result
}
function stripScript(content: string) {
const result = content.match(/<script.*>([\s\S]+)<\/script>/);
const code = result && result[1] ? result[1].trim() : "";
return code.replace(/\/\/import(.*);\n/gi,'').replace(/import(.*);\n/gi,'');
}
function parseStyle(style,preName){
let _ast = csstree.parse(style);
if(_ast){
csstree.walk(_ast, (node) => {
if(node.type==='Rule'&&node.prelude&&/^SelectorList/.test(node.prelude.type)){
node.prelude.children.forEach((cNode)=>{
// console.log(cNode.children)
if(cNode.type==='Selector'&&cNode.children){
cNode.children.unshift({
"type": "Combinator",
"loc": null,
"name": " "
})
cNode.children.unshift({
"type": "ClassSelector",
"loc": null,
"name": preName
})
}
})
}
})
return csstree.generate(_ast)
}
return ''
}
function stripStyle(content: string) {
const result = content.match(/<style.*>([\s\S]+)<\/style>/);
return result && result[1] ? result[1].trim() : "";
}
function getHighlightCode (code: { match: (arg0: RegExp) => { (): any; new(): any; length: any; }; }, lang: string, langText: string,highlightLines?:string) {
// console.log(highlightLines);
try {
const highlightLinesRanges = !!highlightLines
? resolveHighlightLines(highlightLines)
: null
let numberStr = '';
let numberLen = code.match(/\n/ig).length
for(let i =0; i<numberLen; i++){
numberStr +='<li '+(highlightLinesRanges&&isHighlightLine(i+1,highlightLinesRanges)?'class="line"':'')+'><span>'+(i+1)+'</span></li>'
}
// console.log(code);
return '<pre class="hljs fv-hljs '+(highlightLinesRanges?'highlight-line':'')+'">'+( numberStr? '<ul class="highlight-line-number '+(numberLen<5?' nonumber':'')+'" >'+numberStr+'</ul>':'')+'<code class=" hljs hljs-'+lang+'" @click.stop="()=>{}">' +
hljs.highlight(lang,code,true).value +
'</code><div class="language-text" >'+ langText +'</div></pre>';
} catch (__) {}
return ''
}
function stripTemplate(content: string) {
content = content.trim();
if (!content) {
return content;
}
return content.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "").replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, "").replace(/<template>/, '').replace(/<\/template>/, '').trim();
}
interface createVuedcoPlugin {
docsPath?: (root: string) => string | undefined;
root?: string | undefined;
plugins?: any[];
}
let viteConfig: any | undefined = undefined
//判断是否文件夹
function isFile(path) {
return fs.statSync(path).isFile()
}
// copy 文件
function copyFile(src, dest) {
const folderPath = _path.parse(dest).dir
// 文件夹是否存在
if (!fs.existsSync(folderPath)) {
// 不存在 (递归创建文件夹)
fs.mkdirSync(folderPath, { recursive: true })
}
fs.copyFileSync(src, dest)
}
const copyFolder = (source, dest)=>{
const sourcePath = _path.resolve(source)
const destPath = _path.resolve(dest)
// 原路径是否存在
if (!fs.existsSync(sourcePath)) {
return console.log('source path does not exist')
}
// 源路径存在
// 判断源路径是否时文件
if (isFile(sourcePath)) {
// 是文件
copyFile(sourcePath, destPath)
} else {
// 是文件夹
fs.readdirSync(sourcePath)
.forEach(item => {
const fullPath = _path.join(sourcePath, item)
if (fs.statSync(fullPath).isFile()) {
copyFile(fullPath, _path.join(destPath, item))
} else {
// 文件夹 (进行递归)
copyFolder(fullPath, _path.join(destPath, item))
}
})
}
}
export const initPlugin = async(name)=>{
//判断是否存在tinymce-plugin 没有就通过@npkg/tinymce-plugin 去更新
let isTpDir
try{
isTpDir = fs.readdirSync('./tinymce-plugin')
} catch (error) {
}
if(!isTpDir){
copyFolder('../../../node_modules/@npkg/tinymce-plugin','./tinymce-plugin')
}
let _sourcePath = './tinymce-plugin/plugins/'+name
let _tinymceSourcePath = './public/tinymce/plugins/'+name
let _rmdir = ''
let _rmdirTinymce = ''
try {
_rmdir = fs.readdirSync(_sourcePath)
} catch (error) {
}
//删除tinymce中该插件
try {
_rmdirTinymce = fs.readdirSync(_tinymceSourcePath)
} catch (error) {
}
if(_rmdirTinymce){
await delPath(_tinymceSourcePath)
}
if(_rmdir){
await delPath(_sourcePath)
}
copyFolder('../../../dist',_sourcePath)
copyFolder('../../../dist',_tinymceSourcePath)
try {
copyFolder('../../../dist/langs','./langs')
} catch (error) {
}
}
export function createVuedcoPlugin(options: { docsPath: any; mode?: any; root?: any; }) {
const { docsPath , root, mode} = options;
const docDir = docsPath?docsPath(root) :'' || root;
//
const docLoadRule = new RegExp(`/(.*?).md$`)
return {
name: 'md-to-vue-plugin', // 必须
buildStart(){
},
buildEnd(error: any){
if (error) throw error;
for(let key in docSiteObj){
docSite.dataList.push(docSiteObj[key])
}
fs.stat('./public/demo',(stat:any)=>{
if(stat){
delPath('./public/demo')
}
})
},
configResolved(resolvedConfig: any) {
viteConfig = resolvedConfig
},
transform(src: any, id: any) {
let styleStr=''
if (docLoadRule.test(id)) {
stripImportCheck = {}
const fileName:any = id.split("/").pop().split(".")[0]
const demos: { id: string; component: string; }[] = [];
let docpathPath:any = id.indexOf('site/docs')>-1? id.replace(root.replace(/\\/g,'/'),'').replace(/^\/build\/site\//,'').split("/resources/"): id
const docRepo = docpathPath[0]
const docpath = docpathPath[1]
docSiteObj[fileName] = {title: fileName,repo: docRepo.split('/')[1], path: docpath.replace(/(_t_(.*)_p_\.md)$/,'') }
let currentDescription = "";
let currentType = '';
const md = new MarkdownIt("default", {
html: true,
linkify: true,
// langPrefix: 'language-',
typographer: true,
highlight: function (code: any, lang: string, highlightLine: string) {
const id = `Demo${demos.length}`;
let langText = lang
lang === 'vue' ? lang = 'js': ''
if (langText === "vue"&¤tType) {
const stript = (stripImport(code)+(currentType ==='vue2'?'export default {}':stripScript(code)) || "export default {}").replace(
"export default",
`const ${id} =`,
);
const template = stripTemplate(code);
styleStr+=parseStyle(stripStyle(code),"demo-"+fileName)
// console.log(styleStr);
// console.log(stript);
demos.push({
id: id,
component: [
"",
stript,
`
if (${id}.methods) {
${id}.methods.source = function () {
return ${JSON.stringify(getHighlightCode(code, lang, langText,highlightLine))}
}
${currentType ==='vue2' ? ` ${id}.methods.template= function () {
return ${JSON.stringify(template)}
}
${id}.methods.sourceCode = function(){
return ${stripScript(code).split("export default")[1].replace(/data\(\)/,'data: function()')}
}
`:''}
} else {
${id}.methods = {
${currentType ==='vue2'?`template(){
return ${JSON.stringify(template)}
},sourceCode(){
return ${stripScript(code).split("export default")[1].replace(/data\(\)/,'data: function()')}
},`:''}
source() {
return ${JSON.stringify(getHighlightCode(code, lang, langText,highlightLine))}
},
}
}`,
`${id}.template = ${JSON.stringify(
currentType ==='vue2'?
`<PreviewVue2 class="demo-${fileName}" :template="template()" :source="source()">
${
currentDescription &&
`<template v-slot:description>${currentDescription}</template>`
}
</PreviewVue2>`:
`<Preview class="demo-${fileName}" :source="source()">
<template v-slot:demo>${template}</template>'
${
currentDescription &&
`<template v-slot:description>${currentDescription}</template>`
}
</Preview>`
)}`,
].join("\n"),
});
currentType = ''
return '<'+id+' data-isComponent="vue" class="demo-'+fileName+'" />'
}else if(currentType =='react'){
const stript = (`const ${id} = {}\n`+stripImport(code))
styleStr+=parseStyle(stripStyle(code),"demo-"+fileName)
const template = stripTemplate(code);
demos.push({
id: id,
component: [
"",
stript,
`
${id}.methods = {
source() {
return ${JSON.stringify(getHighlightCode(code, lang, langText,highlightLine))}
},
sourceCode(DomeID){
${parserJsx(stripScript(code)).replace(/document\.getElementById\(([A-Za-z0-9_'"^%&',;=?$]+)\)/,'DomeID').replace(/ReactDOM\.render/,'return ReactDOM.render')}
}
}
`,
`${id}.template = ${JSON.stringify(
`<PreviewReact class="demo-${fileName}" idx="${id}" :source="source()">
${
currentDescription &&
`<template v-slot:description>${currentDescription}</template>`
}
</PreviewReact>`
)}`,
].join("\n"),
});
currentType = ''
return '<'+id+' data-isComponent="react" class="demo-'+fileName+'" />'
}else if(currentType =='iframe'){
const stript = (`const ${id} = {}\n`)
// setTimeout
const tinymceDmeoheight = getHeight(code);
let demoSrc = '/demo/demo-'+fileName+ new Date().getTime()+'/'
mkdirPath('./public'+demoSrc)
writeDemo('./public'+demoSrc+'index.html',code)
currentDemoList[fileName]&¤tDemoList[fileName].length>0? currentDemoList[fileName].push(demoSrc):currentDemoList[fileName] = [demoSrc]
demos.push({
id: id,
component: [
"",
stript,
`
${id}.methods = {
source() {
return ${JSON.stringify(getHighlightCode(code, lang, langText,highlightLine))}
},
sourceCode(){
return '<iframe scrolling="no" width="100%" style="width:100%;margin:auto 0;border:0px; min-height: ${tinymceDmeoheight.tpHeight||tinymceDmeoheight.min_height||tinymceDmeoheight.min_height||'220'}px" onload="autoIframeHeight(this)" src="${demoSrc}index.html"></iframe>'
}
}
`,
`${id}.template = ${JSON.stringify(
`<PreviewIframe class="demo-${fileName}" idx="${id}" :source="source()" :sourceCode="sourceCode()">
${
currentDescription &&
`<template v-slot:description>${currentDescription}</template>`
}
</PreviewIframe>`
)}`,
].join("\n"),
});
currentType = ''
return '<'+id+' data-isComponent="iframe" />'
}else if(currentBoxType =='tinymce-box'&& currentType=='html'){
currentBoxTypeDemo['html']=code
currentBoxTypeDemo['fileName']=fileName
currentType = ''
return `<codeBoxItem title="HTML"><pre>${getHighlightCode(currentBoxTypeDemo['html'], lang, langText,highlightLine)}</pre></codeBoxItem>`
}else if(currentBoxType =='tinymce-box'&& currentType=='js'){
currentBoxTypeDemo['js']=code
currentType = ''
return `<codeBoxItem title="JS"><pre>${getHighlightCode(currentBoxTypeDemo['js'], lang, langText,highlightLine)}</pre></codeBoxItem>`
}else if(currentBoxType =='tinymce-box'&& currentType=='css'){
currentBoxTypeDemo['css']=code
currentType = ''
return `<codeBoxItem title="CSS"><pre>${getHighlightCode(currentBoxTypeDemo['css'], lang, langText,highlightLine)}</pre></codeBoxItem>`
} else if (lang && hljs.getLanguage(lang)) {
// console.log(lang);
currentType = ''
return getHighlightCode(code, lang, langText,highlightLine)
}
currentType = ''
return '';
},
});
const default_code_inline = md.renderer.rules.code_inline;
md.renderer.rules.code_inline = function(tokens: { [x: string]: { attrSet: (arg0: string, arg1: string) => void; }; }, idx: string | number, options: any, env: any, self: any) {
tokens[idx].attrSet('class','fv-code_inline')
return default_code_inline(tokens, idx, options, env, self)
}
var default_image = md.renderer.rules.image;
md.renderer.rules.image = function (tokens, idx, options, env, self) {
let token = tokens[idx],
aIndex = token.attrIndex('src');
let imageInfo:any = getImageInfo(tokens[idx].attrs[aIndex][1])
// console.log(imageInfo);
// console.log(self);
if(imageInfo.width){
token.attrIndex('width')<0 && tokens[idx].attrPush(['width', imageInfo.width]);
}
if(imageInfo.height){
token.attrIndex('height')<0 && tokens[idx].attrPush(['height', imageInfo.height]);
}
// 传递 token 给默认的渲染器。
return default_image(tokens, idx, options, env, self);
};
var default_link_open = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
md.renderer.rules.link_open = function (tokens, idx, options, env, self) {
// 如果你确认其他的插件不能添加 `target`
let ahref = tokens[idx].attrIndex('href');
if(ahref>=0&&isExternalLinks(tokens[idx].attrs[ahref][1])){
let aIndex = tokens[idx].attrIndex('target');
if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank']); // 添加新属性
} else {
tokens[idx].attrs[aIndex][1] = '_blank'; // 替换已经存在的属性值
}
}
// 传递 token 到默认的渲染器。
return default_link_open(tokens, idx, options, env, self);
};
md.use(
mdContainer,
"tinymce-vue3",
demoTinymceVue3Container(md, (description: string) => {
currentType = 'vue'
currentDescription = description;
}),
);
md.use(
mdContainer,
"tinymce-vue2",
demoTinymceVue2Container(md, (description) => {
currentType = 'vue2'
currentDescription = description;
}),
);
md.use(
mdContainer,
"tinymce-react",
demoTinymceReactContainer(md, (description) => {
currentType = 'react'
currentDescription = description;
}),
);
md.use(
mdContainer,
"tinymce-box",
demoTinymceBoxContainer(md, (description) => {
currentDescription = description;
}),
);
md.use(
mdContainer,
"html",
{
validate(params: string) {
return params.trim().match(/^html/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
currentType = 'html'
return "";
}
return "";
},
});
md.use(
mdContainer,
"js",
{
validate(params: string) {
return params.trim().match(/^js/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
currentType = 'js'
return "";
}
return "";
},
});
md.use(
mdContainer,
"css",
{
validate(params: string) {
return params.trim().match(/^css/);
},
render(tokens: { [x: string]: { info: string; nesting:any }; }, idx: string | number) {
if (tokens[idx].nesting === 1) {
currentType = 'css'
return "";
}
return "";
},
});
md.use(
mdContainer,
"tinymce",
demoTinymceIframeContainer(md, (description) => {
currentType = 'iframe'
currentDescription = description;
}),
);
md.use(mdContainer, "tip");
md.use(mdContainer, "danger");
md.use(mdContainer, "info");
md.use(mdContainer, "warning");
md.use(mdContainer, "details",detailsContainer);
// md.use(mdAnchor,{ permalinkSymbol:'#',level: [1,2,3,4,5], permalink: true , permalinkBefore:true, callback: function(e:any, value:any){
// }
// })
md.use(mdToc)
let context = md.render(src, {});
context = context.replace(/\<pre\>\<code[^>]*>/g,'').replace(/\<\/code><\/pre\>/g,'')
const docComponent = docRepo.split('/')[1] ?`
import { createApp, defineComponent } from 'vue';
${demos.map((demo) => demo.component).join("")}
const __script = defineComponent({
components: {
${demos.map((demo) => demo.id).join(",")}
},
template: '<div class="fv-mardown-html">'+${JSON.stringify('<div class="fv-mardown-main tp-doc">'+context+'</div>')}+'</div>'
});
export default __script;`: `
import { createApp, defineComponent } from 'vue';
${demos.map((demo) => demo.component).join("")}
const __script = defineComponent({
components: {
${demos.map((demo) => demo.id).join(",")}
},
template: '<div class="fv-mardown-html">'+${JSON.stringify('<div class="fv-mardown-main">'+context+'</div>')}+'</div>'
});
export default __script;`
;
// console.log();
// console.log(styleStr);
let styleCode = styleStr?`var __vite_style__=document.createElement("style");__vite_style__.innerHTML=\`${styleStr}\`;\n document.head.appendChild(__vite_style__);\n`:''
return {
code: styleCode+docComponent,
map: null, // 如果可行将提供 source map
}
}
},
};
}