MCPcopy
hub / github.com/YMFE/yapi / exportData

Method exportData

exts/yapi-plugin-export-data/controller.js:71–186  ·  view source on GitHub ↗
(ctx)

Source from the content-addressed store, hash-verified

69 }
70
71 async exportData(ctx) {
72 let pid = ctx.request.query.pid;
73 let type = ctx.request.query.type;
74 let status = ctx.request.query.status;
75 let isWiki = ctx.request.query.isWiki;
76
77 if (!pid) {
78 ctx.body = yapi.commons.resReturn(null, 200, 'pid 不为空');
79 }
80 let curProject, wikiData;
81 let tp = '';
82 try {
83 curProject = await this.projectModel.get(pid);
84 if (isWiki === 'true') {
85 const wikiModel = require('../yapi-plugin-wiki/wikiModel.js');
86 wikiData = await yapi.getInst(wikiModel).get(pid);
87 }
88 ctx.set('Content-Type', 'application/octet-stream');
89 const list = await this.handleListClass(pid, status);
90
91 switch (type) {
92 case 'markdown': {
93 tp = await createMarkdown.bind(this)(list, false);
94 ctx.set('Content-Disposition', `attachment; filename=api.md`);
95 return (ctx.body = tp);
96 }
97 case 'json': {
98 let data = this.handleExistId(list);
99 tp = JSON.stringify(data, null, 2);
100 ctx.set('Content-Disposition', `attachment; filename=api.json`);
101 return (ctx.body = tp);
102 }
103 default: {
104 //默认为html
105 tp = await createHtml.bind(this)(list);
106 ctx.set('Content-Disposition', `attachment; filename=api.html`);
107 return (ctx.body = tp);
108 }
109 }
110 } catch (error) {
111 yapi.commons.log(error, 'error');
112 ctx.body = yapi.commons.resReturn(null, 502, '下载出错');
113 }
114
115 async function createHtml(list) {
116 let md = await createMarkdown.bind(this)(list, true);
117 let markdown = markdownIt({ html: true, breaks: true });
118 markdown.use(markdownItAnchor); // Optional, but makes sense as you really want to link to something
119 markdown.use(markdownItTableOfContents, {
120 markerPattern: /^\[toc\]/im
121 });
122
123 // require('fs').writeFileSync('./a.markdown', md);
124 let tp = unescape(markdown.render(md));
125 // require('fs').writeFileSync('./a.html', tp);
126 let left;
127 // console.log('tp',tp);
128 let content = tp.replace(

Callers

nothing calls this directly

Calls 3

handleListClassMethod · 0.95
handleExistIdMethod · 0.95
getMethod · 0.45

Tested by

no test coverage detected