Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/controller/viewer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
'use strict'
const _ = require('lodash')
const {
getSearchableContentByPath
} = require('../web/lib/mod/parser/mdSearchedParser/searchByUrl')
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)
})
if (spiderIndex !== -1) {
let filePath = _.get(ctx, 'request.url')
ctx.body = await getSearchableContentByPath(filePath)
return
}
await ctx.renderClient('viewer/index.js')
}
}
Expand Down
26 changes: 26 additions & 0 deletions app/web/lib/mod/parser/mdSearchedParser/searchByUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const axios = require('axios')
const { getSearchableContent } = require('./index')

const gitlabAxiosInstance = axios.create({
baseURL: 'https://api.keepwork.com/git/v0',
timeout: 30 * 1000
})

const getContentFromGitlab = async path => {
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)
return getSearchableContent(content)
}

module.exports = { getSearchableContentByPath }