@ablula/egg-yuque-viewer
Advanced tools
Comparing version 0.2.0-beta.1 to 0.2.0-beta.2
@@ -5,2 +5,3 @@ 'use strict'; | ||
const { deepMerge } = require('./utils'); | ||
const cache = require('./cache'); | ||
const BASE_URL = 'https://www.yuque.com/api/v2'; | ||
@@ -11,3 +12,3 @@ | ||
let miniIndex = 0; | ||
if (segments && segments.length) { | ||
if (segments && segments.length && content) { | ||
miniIndex = segments.map(regexp => { | ||
@@ -38,3 +39,3 @@ const idx = content.search(regexp); | ||
title: item.title, | ||
locator: item.target.slug, | ||
locator: item.target && item.target.slug, | ||
searchEngine: 1, | ||
@@ -233,3 +234,3 @@ }; | ||
`${this.baseUrl}/search/?q=${encodeURIComponent(keywords)}&type=doc&scope=${config.namespace}`); | ||
result.data = LarkService.formatSearchResult(result.data, { | ||
const data = LarkService.formatSearchResult(result.data, { | ||
segments: [ keywords ], | ||
@@ -239,5 +240,5 @@ namespace: config.namespace, | ||
return { | ||
total: result.data.length, | ||
total: data.length, | ||
segments: [ keywords ], | ||
data: result.data, | ||
data, | ||
}; | ||
@@ -248,2 +249,7 @@ } | ||
const { app, config } = this; | ||
if (cache.get(url)) { | ||
app.logger.info('[EggYuqeuViewer] get data of %s from cache', url); | ||
return cache.get(url); | ||
} | ||
app.logger.info('[EggYuqeuViewer] get data of %s from yuque', url); | ||
const result = await app.curl(url, { | ||
@@ -259,5 +265,9 @@ timeout: 5000, | ||
}); | ||
if (result && +result.status === 429) { | ||
app.logger.error('当前资源 %s 的访问频率过高,请稍后再试', url); | ||
} | ||
if (result.status !== 200 || !result.data) { | ||
throw new Error(`request document from lark failed, url: ${url}`); | ||
} | ||
cache.set(url, {...result.data}); | ||
return result.data; | ||
@@ -264,0 +274,0 @@ } |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const YuqueClient = require('./YuqueClient'); | ||
const cache = require('./cache'); | ||
@@ -131,2 +132,10 @@ class YuQueViewer { | ||
}); | ||
this.unshiftRoute(`GetCache`, `/LIST-ABLULA-DOCUMENT-CACHE`, async (ctx, next) => { | ||
const data = cache.listAll(); | ||
ctx.body = data; | ||
}); | ||
this.unshiftRoute(`ClearCache`, `/CLEAR-ABLULA-DOCUMENT-CACHE`, async (ctx, next) => { | ||
cache.clear(); | ||
ctx.body = 'ok'; | ||
}); | ||
} | ||
@@ -133,0 +142,0 @@ |
{ | ||
"name": "@ablula/egg-yuque-viewer", | ||
"author": "mark.ck", | ||
"version": "0.2.0-beta.1", | ||
"version": "0.2.0-beta.2", | ||
"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
22011
12
577