@ablula/egg-yuque-viewer
Advanced tools
Comparing version 0.1.9 to 0.2.0-beta.0
'use strict'; | ||
// yuqueViewer.npm: '@ablula/document-client', | ||
// yuqueViewer.namespace: 'ant-design/course', | ||
// yuqueViewer.search: '/documents/search', | ||
// yuqueViewer.title: 'Ant Design 实战教程', | ||
// yuqueViewer.prefix: '/documents', | ||
// yuqueViewer.onlyDoc: false, | ||
// yuqueViewer.noHeader: false, | ||
// yuqueViewer.showSearch: true, | ||
// yuqueViewer.showEditor: true, | ||
// yuqueViewer.view: '/help', | ||
// yuqueViewer.token: '', | ||
// yuqueViewer.darkMode: true, | ||
// yuqueViewer.lightColor: '#ffffff00', | ||
// yuqueViewer.blackColor: 'black', | ||
// yuqueViewer.logo: 'https://i.ablula.tech/ablula.png', | ||
exports.yuqueViewer = { | ||
npm: '@ablula/document-client', | ||
namespace: 'ant-design/course', | ||
search: '/documents/search', | ||
title: 'Ant Design 实战教程', | ||
prefix: '/documents', | ||
onlyDoc: false, | ||
noHeader: false, | ||
showSearch: true, | ||
showEditor: true, | ||
view: '/help', | ||
token: '', | ||
darkMode: true, | ||
lightColor: '#ffffff00', | ||
blackColor: 'black', | ||
logo: 'https://i.ablula.tech/ablula.png', | ||
client: {} | ||
}; |
118
lib/index.js
'use strict'; | ||
const YuqueClient = require('./YuqueClient'); | ||
const YuqueViewer = require('./YuqueViewer'); | ||
function bindRoute(app, router, config) { | ||
const yuqueClient = new YuqueClient(app, config); | ||
module.exports = app => { | ||
let view = config.view || ''; | ||
if (view === '/') { | ||
view = ''; | ||
const { config: appConfig } = app; | ||
const { yuqueViewer } = appConfig; | ||
if (!yuqueViewer) { | ||
app.logger.error('[EggYuqueViewer: init]: yuqueViewer is required in app config, but got null.'); | ||
return; | ||
} | ||
const { prefix, showSearch, lazyLoad, search } = config; | ||
if (showSearch && !search) { | ||
console.warn('show search without search api, search won\'t be shown'.yellow); | ||
// 本插件不使用 egg 插件中推荐的 clients 方式 | ||
if (!yuqueViewer.client) { | ||
appConfig.yuqueViewer.client = {}; | ||
} | ||
if (lazyLoad && !prefix) { | ||
throw new Error('prefix required in lazyMode.'); | ||
if (yuqueViewer.clients) { | ||
delete yuqueViewer.clients; | ||
} | ||
if (search) { | ||
router.get(`GetDocumentView-${search}`, `${search}`, async ctx => { | ||
const keywords = ctx.query.keywords; | ||
if (!keywords) { | ||
throw new Error('keywords param required.'); | ||
} | ||
const result = await yuqueClient.search(keywords); | ||
ctx.status = 200; | ||
ctx.body = result; | ||
}); | ||
} | ||
router.get('GetDocument', `${prefix}/*`, async (ctx, next) => { | ||
const locator = ctx.request.path.replace(`${prefix}/`, ''); | ||
if (!locator) { | ||
return; | ||
} | ||
if (locator.startsWith('http')) { | ||
try { | ||
ctx.redirect(locator); | ||
} catch (e) { | ||
ctx.logger.error('redirect failed: %s-%s', e && e.message, e && e.stack); | ||
ctx.body = `can not redirect to ${locator}, please check domainWhiteList in your config.`; | ||
} | ||
return; | ||
} | ||
const result = await yuqueClient.getDocument(locator); | ||
ctx.status = 200; | ||
ctx.body = result; | ||
next(); | ||
app.addSingleton('yuqueViewer', (config, app) => { | ||
return new YuqueViewer(app); | ||
}); | ||
router.get(`Static Resources-${view}/js`, `${view}/index.js`, async ctx => { | ||
return ctx.redirect(`https://cdn.jsdelivr.net/npm/${config.npm}/build/index.js`); | ||
}); | ||
router.get(`Static Resources-${view}/css`, `${view}/index.css`, async ctx => { | ||
return ctx.redirect(`https://cdn.jsdelivr.net/npm/${config.npm}/build/index.css`); | ||
}); | ||
router.get(`GetDocumentView-${view}`, `${view}`, async (ctx, next) => { | ||
ctx.body = await yuqueClient.getDocumentView(); | ||
next(); | ||
}); | ||
router.get(`GetDocumentView-${view}`, `${view}/*`, async (ctx, next) => { | ||
const subPath = ctx.request.path.replace(`${view}/`, ''); | ||
if (subPath.startsWith('http')) { | ||
try { | ||
ctx.redirect(subPath); | ||
} catch (e) { | ||
ctx.logger.error('redirect failed: %s-%s', e && e.message, e && e.stack); | ||
ctx.body = `can not redirect to ${subPath}, please check domainWhiteList in your config.`; | ||
} | ||
return; | ||
} | ||
ctx.body = await yuqueClient.getDocumentView(); | ||
next(); | ||
}); | ||
} | ||
module.exports = app => { | ||
app.addSingleton('yuqueViewer', async (config, app) => { | ||
const { yuqueViewer: yuqueViewerConfig = {} } = config || {}; | ||
const { documents, otherConfig } = yuqueViewerConfig; | ||
if (!documents) { | ||
return new YuqueClient(app, yuqueViewerConfig); | ||
} | ||
const yuqueClientMap = {}; | ||
Object.keys(documents).forEach(item => { | ||
yuqueClientMap[item] = new YuqueClient(app, { | ||
...otherConfig, | ||
...item, | ||
}); | ||
}); | ||
}); | ||
const { router, config: appConfig } = app; | ||
const config = appConfig.yuqueViewer; | ||
const { documents, ...otherConfig } = config; | ||
if (typeof documents === 'object' && Object.values(documents).length) { | ||
Object.values(documents).forEach(item => { | ||
bindRoute(app, router, { | ||
...otherConfig, | ||
...item, | ||
}); | ||
}); | ||
return; | ||
} | ||
bindRoute(app, router, config); | ||
}; |
@@ -66,2 +66,7 @@ 'use strict'; | ||
} | ||
mergeConfig(config) { | ||
this.config = { ...this.config, ...(config || {}) }; | ||
} | ||
async getUserInfo() { | ||
@@ -81,8 +86,9 @@ const { app, config } = this; | ||
} catch (e) { | ||
console.log(e); | ||
app.logger.error('[EggYuqueViewer] parse user info failed: ', e); | ||
} | ||
} | ||
app.logger.info('getDirectories SUCCESSFULLY'); | ||
app.logger.info('[EggYuqeuViewer] getDirectories SUCCESSFULLY'); | ||
return docs.data; | ||
} | ||
async getDocuments() { | ||
@@ -95,3 +101,3 @@ const { app, config } = this; | ||
} | ||
app.logger.info('getDirectories SUCCESSFULLY'); | ||
app.logger.info('[EggYuqeuViewer] getDirectories SUCCESSFULLY'); | ||
const documents = docs.data; | ||
@@ -113,3 +119,7 @@ if (!documents || !documents.length) { | ||
} | ||
async getDocumentView() { | ||
async getDocumentView(hotConfig) { | ||
if (hotConfig) { | ||
this.mergeConfig(hotConfig); | ||
} | ||
const { config } = this; | ||
@@ -173,3 +183,3 @@ if (config.view === '/') { | ||
} | ||
app.logger.info('getDirectories SUCCESSFULLY'); | ||
app.logger.info('[EggYuqeuViewer: getDirectories] SUCCESSFULLY'); | ||
return LarkService.formatDirs(dirs.data); | ||
@@ -185,3 +195,3 @@ } | ||
if (!result || !result.data) { | ||
app.logger.error(`Get document from lark failed, groupId: ${config.namespace}, locator: ${locator}`); | ||
app.logger.error(`[EggYuqeuViewer: getDocument] Get document from lark failed, groupId: ${config.namespace}, locator: ${locator}`); | ||
return { | ||
@@ -194,3 +204,3 @@ locator, | ||
} | ||
app.logger.info('getDocument SUCCESSFULLY'); | ||
app.logger.info('[EggYuqeuViewer: getDocument] SUCCESSFULLY'); | ||
const data = result && result.data || {}; | ||
@@ -212,3 +222,3 @@ const content = (data.body_html || '').replace(/<img/g, '<img referrerpolicy="no-referrer"'); | ||
} catch (e) { | ||
app.logger.error('getDocument failed: ' + e.message); | ||
app.logger.error('[EggYuqeuViewer: getDocument] getDocument failed: ' + e.message); | ||
return { | ||
@@ -215,0 +225,0 @@ locator, |
{ | ||
"name": "@ablula/egg-yuque-viewer", | ||
"author": "mark.ck", | ||
"version": "0.1.9", | ||
"version": "0.2.0-beta.0", | ||
"description": "egg plugin for yuque viewer", | ||
@@ -6,0 +6,0 @@ "eggPlugin": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17006
10
417