From 502554928222e42abb5505fbd5f9efcafc80e371 Mon Sep 17 00:00:00 2001 From: ZhangKaitlyn <1873070425@qq.com> Date: Fri, 19 Apr 2019 09:54:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=88=AC=E8=99=AB=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/viewer.js | 27 +++++++++++++++++++ .../parser/mdSearchedParser/searchByUrl.js | 20 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js diff --git a/app/controller/viewer.js b/app/controller/viewer.js index 7bc9c1114..38e296af3 100644 --- a/app/controller/viewer.js +++ b/app/controller/viewer.js @@ -1,9 +1,36 @@ 'use strict' +const _ = require('lodash') +const { + getSearchableContentByPath +} = require('../web/lib/mod/parser/mdSearchedParser/searchByUrl') +// const { getSearchableContent } = require('../web/lib/mod/parser/mdSearchedParser/index') +const SPIDERS = [ + 'PostmanRuntime', + 'Baiduspider', + 'Googlebot', + '360Spider', + 'Sosospider', + 'sogou spider' +] module.exports = app => { return class PageViewerController extends app.Controller { async index() { const { ctx } = this + let userAgent = _.get(ctx, ['request', 'header', 'user-agent'], '') + let spiderIndex = _.findIndex(SPIDERS, spider => { + return new RegExp('^' + spider, 'i').test(userAgent) + }) + console.log('----------') + console.log('USER-AGENT: ', userAgent, ctx.request.url) + if (spiderIndex !== -1) { + console.log('SPIDER: ', SPIDERS[spiderIndex]) + let filePath = _.get(ctx, 'request.url') + // ctx.body = getSearchableContent(filePath) + ctx.body = getSearchableContentByPath(filePath) + return + } + console.log('----------') await ctx.renderClient('viewer/index.js') } } diff --git a/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js new file mode 100644 index 000000000..b35dc4a8e --- /dev/null +++ b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js @@ -0,0 +1,20 @@ +const axios = require('axios') +const { getSearchableContent } = require('./index') + +const gitlabAxiosInstance = axios.create({ + baseURL: process.env.GITLAB_API_PREFIX, + timeout: 30 * 1000 +}) + +const getContentFromGitlab = async path => { + let content = await gitlabAxiosInstance.get(path).catch() + return content +} + +const getSearchableContentByPath = async path => { + let content = await getContentFromGitlab(path) + console.log(content) + return getSearchableContent(content) +} + +module.exports = { getSearchableContentByPath } From 6a3ca873037cd86edd0f8ec13efcc2836cdc3e8c Mon Sep 17 00:00:00 2001 From: ZhangKaitlyn <1873070425@qq.com> Date: Wed, 8 May 2019 17:45:01 +0800 Subject: [PATCH 2/4] =?UTF-8?q?remove=20console.log();=20=E7=88=AC?= =?UTF-8?q?=E8=99=AB=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/viewer.js | 9 +++------ .../lib/mod/parser/mdSearchedParser/searchByUrl.js | 11 ++++++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/controller/viewer.js b/app/controller/viewer.js index 38e296af3..505db8134 100644 --- a/app/controller/viewer.js +++ b/app/controller/viewer.js @@ -21,16 +21,13 @@ module.exports = app => { let spiderIndex = _.findIndex(SPIDERS, spider => { return new RegExp('^' + spider, 'i').test(userAgent) }) - console.log('----------') - console.log('USER-AGENT: ', userAgent, ctx.request.url) if (spiderIndex !== -1) { - console.log('SPIDER: ', SPIDERS[spiderIndex]) let filePath = _.get(ctx, 'request.url') - // ctx.body = getSearchableContent(filePath) - ctx.body = getSearchableContentByPath(filePath) + ctx.body = { + content: await getSearchableContentByPath(filePath) + } return } - console.log('----------') await ctx.renderClient('viewer/index.js') } } diff --git a/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js index b35dc4a8e..80de19ef3 100644 --- a/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js +++ b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js @@ -2,18 +2,23 @@ const axios = require('axios') const { getSearchableContent } = require('./index') const gitlabAxiosInstance = axios.create({ - baseURL: process.env.GITLAB_API_PREFIX, + // baseURL: process.env.GITLAB_API_PREFIX, + baseURL: 'https://api.keepwork.com/git/v0', timeout: 30 * 1000 }) const getContentFromGitlab = async path => { - let content = await gitlabAxiosInstance.get(path).catch() + let pathArr = path.split('/') + let projectPath = pathArr[1] + '/' + pathArr[2] + let encodedProjectPath = encodeURIComponent(projectPath) + let encodedPath = encodeURIComponent(path) + let result = await gitlabAxiosInstance.get(`projects/${encodedProjectPath}/files/${encodedPath}.md`).catch() + let content = result.data.content return content } const getSearchableContentByPath = async path => { let content = await getContentFromGitlab(path) - console.log(content) return getSearchableContent(content) } From 805c31bda64f3ffd7544f3e231006342c4500407 Mon Sep 17 00:00:00 2001 From: ZhangKaitlyn <1873070425@qq.com> Date: Thu, 9 May 2019 11:21:33 +0800 Subject: [PATCH 3/4] remove comment --- app/controller/viewer.js | 1 - app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controller/viewer.js b/app/controller/viewer.js index 505db8134..36647c9ee 100644 --- a/app/controller/viewer.js +++ b/app/controller/viewer.js @@ -3,7 +3,6 @@ const _ = require('lodash') const { getSearchableContentByPath } = require('../web/lib/mod/parser/mdSearchedParser/searchByUrl') -// const { getSearchableContent } = require('../web/lib/mod/parser/mdSearchedParser/index') const SPIDERS = [ 'PostmanRuntime', 'Baiduspider', diff --git a/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js index 80de19ef3..7c39b0a80 100644 --- a/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js +++ b/app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js @@ -2,7 +2,6 @@ const axios = require('axios') const { getSearchableContent } = require('./index') const gitlabAxiosInstance = axios.create({ - // baseURL: process.env.GITLAB_API_PREFIX, baseURL: 'https://api.keepwork.com/git/v0', timeout: 30 * 1000 }) @@ -12,7 +11,9 @@ const getContentFromGitlab = async path => { let projectPath = pathArr[1] + '/' + pathArr[2] let encodedProjectPath = encodeURIComponent(projectPath) let encodedPath = encodeURIComponent(path) - let result = await gitlabAxiosInstance.get(`projects/${encodedProjectPath}/files/${encodedPath}.md`).catch() + let result = await gitlabAxiosInstance + .get(`projects/${encodedProjectPath}/files/${encodedPath}.md`) + .catch() let content = result.data.content return content } From f6a0f1a01e26ba231060ec814f8fe8d7db571a13 Mon Sep 17 00:00:00 2001 From: ZhangKaitlyn <1873070425@qq.com> Date: Thu, 9 May 2019 14:32:02 +0800 Subject: [PATCH 4/4] return content directly --- app/controller/viewer.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/controller/viewer.js b/app/controller/viewer.js index 36647c9ee..cfffa3399 100644 --- a/app/controller/viewer.js +++ b/app/controller/viewer.js @@ -22,9 +22,7 @@ module.exports = app => { }) if (spiderIndex !== -1) { let filePath = _.get(ctx, 'request.url') - ctx.body = { - content: await getSearchableContentByPath(filePath) - } + ctx.body = await getSearchableContentByPath(filePath) return } await ctx.renderClient('viewer/index.js')