yapi-plugin-api-doc
Advanced tools
| /* | ||
| * @Author: qiucong | ||
| * @Date: 2020-05-19 10:02:27 | ||
| * @LastEditors: qiucong | ||
| * @LastEditTime: 2020-05-20 17:28:09 | ||
| */ | ||
| const baseController = require('controllers/base.js'); | ||
| const interfaceModel = require('models/interface.js'); | ||
| const interfaceCatModel = require('models/interfaceCat.js'); | ||
| const settingModel = require('../models/project'); | ||
| const yapi = require('yapi.js'); | ||
| const markdownIt = require('markdown-it'); | ||
| const markdownItAnchor = require('markdown-it-anchor'); | ||
| const markdownItTableOfContents = require('markdown-it-table-of-contents'); | ||
| const defaultTheme = require('../theme/default/defaultTheme.js'); | ||
| const md = require('../utils/markdown'); | ||
| class interfaceDocController extends baseController { | ||
| constructor(ctx) { | ||
| super(ctx); | ||
| this.catModel = yapi.getInst(interfaceCatModel); | ||
| this.interModel = yapi.getInst(interfaceModel); | ||
| this.settingModel = yapi.getInst(settingModel); | ||
| } | ||
| /** | ||
| * 搜索接口 | ||
| * @param {*} ctx | ||
| */ | ||
| async search(ctx) { | ||
| let params = ctx.request.body; | ||
| let tp = '', keyword = params.keyword; | ||
| if (keyword.trim() === '') { | ||
| ctx.set('Content-Type', 'text/html'); | ||
| return (ctx.body = createHtml5("", "", false)); | ||
| } | ||
| try { | ||
| let projects = await this.settingModel.listByStatus(true); | ||
| let projectIds = projects.map(v => v.project_id); | ||
| let list = await this.interModel.search(keyword); | ||
| list = list.filter((v) => { | ||
| return v.api_opened === true && projectIds.includes(v.project_id); | ||
| }) | ||
| ctx.set('Content-Type', 'text/html'); | ||
| tp = await createHtml.bind(this)(list); | ||
| return (ctx.body = tp); | ||
| } catch (e) { | ||
| ctx.body = yapi.commons.resReturn(null, 401, e.message); | ||
| } | ||
| async function createHtml(list) { | ||
| let md = await createMarkdown.bind(this)(list, true); | ||
| let markdown = markdownIt({ html: true, breaks: true }); | ||
| markdown.use(markdownItAnchor); // Optional, but makes sense as you really want to link to something | ||
| markdown.use(markdownItTableOfContents, { | ||
| markerPattern: /^\[toc\]/im, | ||
| includeLevel: [2, 3] | ||
| }); | ||
| let tp = unescape(markdown.render(md)); | ||
| let left; | ||
| let content = tp.replace( | ||
| /<div\s+?class="table-of-contents"\s*>[\s\S]*?<\/ul>\s*<\/div>/gi, | ||
| function(match) { | ||
| left = match; | ||
| return ''; | ||
| } | ||
| ); | ||
| return createHtml5(left || '', content, list.length > 0); | ||
| } | ||
| function createHtml5(left, tp, hasInter) { | ||
| let info = ''; | ||
| if (!hasInter) { | ||
| info = ` | ||
| <script src="/prd/assets.js?v=${Math.random()}"></script> | ||
| <script> | ||
| document.write('<link rel="stylesheet" href="/prd/' + window.WEBPACK_ASSETS['index.js'].css + '" />'); | ||
| </script> | ||
| ` | ||
| } | ||
| //html5模板 | ||
| let html = `<!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>接口搜索</title> | ||
| <meta charset="utf-8" /> | ||
| ${info} | ||
| ${defaultTheme} | ||
| </head> | ||
| <body class="fine-api-doc"> | ||
| <div class="header-box"> | ||
| <div class="breadcrumb"> | ||
| <span><a href="/api/public/plugin/documents">首页</a>/</span><span>关键字:${keyword}</span> | ||
| </div> | ||
| </div> | ||
| <div class="g-doc"> | ||
| ${left} | ||
| <div id="right" class="content-right"> | ||
| ${hasInter ? tp : getEmpty()} | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| `; | ||
| return html; | ||
| } | ||
| function createMarkdown(list, isToc) { | ||
| //拼接markdown | ||
| //模板 | ||
| let mdTemplate = ``; | ||
| try { | ||
| // 分类信息 | ||
| mdTemplate += md.createListMarkdown(list, isToc); | ||
| return mdTemplate; | ||
| } catch (e) { | ||
| yapi.commons.log(e, 'error'); | ||
| ctx.body = yapi.commons.resReturn(null, 502, '获取文档出错'); | ||
| } | ||
| } | ||
| function getEmpty() { | ||
| return ` | ||
| <div class="ant-empty"> | ||
| <div class="err-msg"> | ||
| <i class="anticon anticon-frown-o icon"></i> | ||
| <p class="title">没有找到相关接口</p> | ||
| <p><a href="/api/public/plugin/documents">返回首页</a></p> | ||
| </div> | ||
| </div> | ||
| ` | ||
| } | ||
| } | ||
| } | ||
| module.exports = interfaceDocController; |
| const shell = require ('shelljs'); | ||
| const packageJson = require ('./package.json'); | ||
| const version = "v" + packageJson.version; | ||
| if(!version){ | ||
| console.error('version 不能为空') | ||
| process.exit(1) | ||
| } | ||
| shell.exec ('git pull origin master'); | ||
| let a = shell.exec (`git tag |grep ${version} |wc -l`); | ||
| if (a && parseInt (a) > 0) { | ||
| shell.exec ('git tag -d ' + version); | ||
| shell.exec ('git push origin :' + version); | ||
| } | ||
| shell.exec ('git tag ' + version); | ||
| shell.exec ('git push origin ' + version); | ||
| console.log('git push success', version) | ||
| console.log('正在执行npm发布') | ||
| shell.exec('npm publish') |
+35
-0
@@ -62,2 +62,3 @@ const baseController = require('controllers/base.js'); | ||
| } | ||
| let html = `<!DOCTYPE html> | ||
@@ -75,3 +76,37 @@ <html> | ||
| <body class="fine-api-doc"> | ||
| <div class="header-box m-header ant-layout-header"> | ||
| <div class="content g-row"> | ||
| <div class="breadcrumb-container"> | ||
| <span class="ant-breadcrumb">在线接口文档</span> | ||
| </div> | ||
| <div class="user-toolbar"> | ||
| <div class="toolbar-li item-search"> | ||
| <form id="search-form" class="ant-form ant-form-horizontal" method="post" action="/api/public/plugin/fine/document/search"> | ||
| <span class="ant-input-search ant-input-affix-wrapper" style="width: 200px;"> | ||
| <input name="keyword" id="fine-api-search-input" placeholder="输入接口名称搜索" class="ant-input" type="text" value="" required> | ||
| <span class="ant-input-suffix"> | ||
| <span role="img" id="fine-api-search-icon" aria-label="search" class="anticon anticon-search srch-icon"> | ||
| </span> | ||
| </span> | ||
| </span> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ${htmlBody || "<h3 style='text-align:center; margin: 50px 0;'>你走错地方啦,这里什么都没有</h3>"} | ||
| <script> | ||
| document.getElementById("fine-api-search-icon").addEventListener('click', function(){ | ||
| if (document.getElementById("fine-api-search-input").value.trim() === "") { | ||
| return false; | ||
| } | ||
| document.getElementById("search-form").submit(); | ||
| }); | ||
| document.getElementById("search-form").addEventListener('submit', function(event){ | ||
| if (document.getElementById("fine-api-search-input").value.trim() === "") { | ||
| event.preventDefault(); | ||
| return false; | ||
| } | ||
| }); | ||
| </script> | ||
| </body> | ||
@@ -78,0 +113,0 @@ </html> |
@@ -192,13 +192,21 @@ const baseController = require('controllers/base.js'); | ||
| try { | ||
| if ((await this.checkAuth(params.project_id, 'project', 'edit')) !== true) { | ||
| let projectId = params.project_id, isPublic = params.is_public; | ||
| if ((await this.checkAuth(projectId, 'project', 'edit')) !== true) { | ||
| return (ctx.body = yapi.commons.resReturn(null, 406, '没有权限')); | ||
| } | ||
| let username = this.getUsername(), uid = this.getUid(); | ||
| let data = { | ||
| project_id: params.project_id, | ||
| is_public: params.is_public, | ||
| uid: this.getUid() | ||
| project_id: projectId, | ||
| is_public: isPublic, | ||
| uid: uid | ||
| }; | ||
| let res = await this.settingModel.findOneAndUpdate(params.project_id, data); | ||
| yapi.commons.saveLog({ | ||
| content: `<a href="/user/profile/${uid}">${username}</a> 将接口文档设置为${isPublic ? "公开" : "不公开"}`, | ||
| type: 'project', | ||
| uid: uid, | ||
| username: username, | ||
| typeid: projectId | ||
| }); | ||
| let res = await this.settingModel.findOneAndUpdate(projectId, data); | ||
| ctx.body = yapi.commons.resReturn(res); | ||
@@ -205,0 +213,0 @@ } catch (e) { |
@@ -48,2 +48,11 @@ const yapi = require('yapi.js'); | ||
| listByStatus(open) { | ||
| return this.model | ||
| .find({ | ||
| is_public: open | ||
| }) | ||
| .sort({ _id: -1 }) | ||
| .exec(); | ||
| } | ||
| find(id) { | ||
@@ -50,0 +59,0 @@ return this.model.findOne({ _id: id }); |
+11
-4
| { | ||
| "name": "yapi-plugin-api-doc", | ||
| "version": "0.1.0", | ||
| "description": "在标签页中加入查看接口文档", | ||
| "version": "0.2.0", | ||
| "description": "在标签页中加入查看接口文档,在线查看接口文档", | ||
| "main": "index.js", | ||
| "keywords": ["yapi", "doc", "api"], | ||
| "keywords": [ | ||
| "yapi", | ||
| "doc", | ||
| "api" | ||
| ], | ||
| "scripts": { | ||
@@ -20,3 +24,6 @@ "test": "echo \"Error: no test specified\" && exit 1", | ||
| }, | ||
| "homepage": "https://github.com/congqiu/yapi-plugin-api-doc#readme" | ||
| "homepage": "https://github.com/congqiu/yapi-plugin-api-doc#readme", | ||
| "publishConfig": { | ||
| "registry": " https://registry.npmjs.org/" | ||
| } | ||
| } |
+10
-0
| const projectDocController = require('./controllers/project'); | ||
| const groupDocController = require('./controllers/group'); | ||
| const interfaceDocController = require('./controllers/interface'); | ||
@@ -87,3 +88,12 @@ module.exports = function() { | ||
| }); | ||
| // 搜索接口名称或path | ||
| addRouter({ | ||
| controller: interfaceDocController, | ||
| prefix: "/public", | ||
| method: 'post', | ||
| path: 'fine/document/search', | ||
| action: 'search' | ||
| }); | ||
| }); | ||
| }; |
@@ -394,2 +394,3 @@ @charset "UTF-8"; | ||
| right: 0; | ||
| height: 40px; | ||
| background: #32363a; | ||
@@ -421,2 +422,5 @@ } | ||
| color: #2395f1; | ||
| } | ||
| .ant-empty { | ||
| margin-top: 50px; | ||
| } |
+15
-2
@@ -236,3 +236,3 @@ const schema = require('../../../common/shema-transformTo-table.js'); | ||
| template += `<tbody className="ant-table-tbody">${tableBody(dataSource, columns, 0)} | ||
| </tbody> | ||
| </tbody> | ||
| </table> | ||
@@ -330,2 +330,14 @@ `; | ||
| function createListMarkdown(list, isToc) { | ||
| let mdTemplate = `\n## \n`; | ||
| const toc = `[TOC]\n\n`; | ||
| isToc && (mdTemplate += toc); | ||
| for (let i = 0; i < list.length; i++) { | ||
| //循环拼接 接口 | ||
| // 接口内容 | ||
| mdTemplate += createInterMarkdown("", list[i], isToc); | ||
| } | ||
| return mdTemplate; | ||
| } | ||
| let r = { | ||
@@ -335,5 +347,6 @@ createInterMarkdown, | ||
| createProjectMarkdown, | ||
| createClassMarkdown | ||
| createClassMarkdown, | ||
| createListMarkdown | ||
| }; | ||
| module.exports = r; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
54979
16.6%20
11.11%1847
13.73%0
-100%