Skip to content
Permalink
Browse files

feat: support "repoUrl" option

  • Loading branch information...
ulivz committed Feb 6, 2019
1 parent d22011d commit 81d07dd832113f4f14db913f83d31b32c98393af
Showing with 88 additions and 19 deletions.
  1. +4 −3 example/.vuepress/config.js
  2. +25 −10 lib/index.js
  3. +59 −6 lib/yuque.js
@@ -1,11 +1,12 @@
module.exports = {
title: 'Ant Design 实战教程',
description: '基于 umi 的 Ant Design 实战教程',
// title: 'Ant Design 实战教程',
// description: '基于 umi 的 Ant Design 实战教程',
plugins: [
[
require('../../lib/index.js'),
{
repoId: '146550',
repoUrl: 'https://www.yuque.com/yuque/developer',
// repoId: '146550',
home: {
// actionText: 'Getting Started →',
// actionLink: '/intro/',
@@ -16,16 +16,22 @@ const debug = require('debug')(pluginName)
*/

module.exports = (opts, ctx) => {
const { repoId } = opts

assert(
typeof repoId === 'string' ||
typeof repoId === 'number',
`[${pluginName}] repoId should be string or number, but got ${repoId}`
)

Yuque.setRepoId(repoId)
const yuque = Yuque.getInstance()
let { repoId, repoUrl } = opts

if (repoId) {
assert(
typeof repoId === 'string' ||
typeof repoId === 'number',
`[${pluginName}] repoId should be string or number, but got ${repoId}`
)
} else if (repoUrl) {
assert(
typeof repoUrl === 'string',
`[${pluginName}] repoUrl should be string, but got ${repoId}`
)
} else {
throw new Error(`Expected repoId or repoUrl`)
}

return {
name: pluginName,
@@ -40,6 +46,15 @@ module.exports = (opts, ctx) => {
},

async ready() {
if (repoUrl) {
const targetRepo = await Yuque.inferRepoByUrl(repoUrl)
console.log(targetRepo)
repoId = targetRepo.id
}

Yuque.setRepoId(repoId)
const yuque = Yuque.getInstance()

spinner.start(`Fetching repo detail ...`)
const repoDetail = await yuque.getRepoDetail()
if (!(repoDetail && repoDetail.data && repoDetail.data.name)) {
@@ -1,3 +1,5 @@
const assert = require('assert')
const url = require('url')
const wfetch = require('./fetch')
const store = require('./store')
const { hash, logger, chalk } = require('@vuepress/shared-utils')
@@ -40,26 +42,77 @@ module.exports = class Yuque {
}
}

static async get(base, path) {
path = `${base}${path}`
return Yuque.fetch(path)
}

static async getRepos(base, repoId) {
return Yuque.get(base, `groups/${repoId}/repos`)
}

static async getRepoDetail(base, repoId) {
return Yuque.get(base, `repos/${repoId}`)
}

static async getToc(base, repoId) {
return this.get(base, `repos/${repoId}/toc`)
}

static async getPage(base, repoId, slug) {
return this.get(base, `repos/${repoId}/docs/${slug}?raw=1`)
}

static async inferRepoByUrl(repoUrl) {
const { protocol, host, pathname } = url.parse(repoUrl)
assert(
typeof pathname === 'string',
`[CANNOT_RESOLVE_PATHNAME] Cannot infer repoId from repoUrl: ${repoUrl}`
)

const normalizedPathname = pathname.replace(/(^\/|\/$)/g, '')
const [groupId, repoId] = normalizedPathname.split('/')
assert(
typeof groupId === 'string' &&
typeof repoId === 'string',
`[CANNOT_PARSE_PATHNAME] Cannot infer repoId from repoUrl: ${repoUrl}`
)

const base = `${protocol}//${host}/api/v2/`
const { data: repos } = await Yuque.getRepos(base, groupId)
assert(
Array.isArray(repos),
`[CANNOT_FIND_GROUP] Cannot infer repoId from repoUrl: ${repoUrl}`
)

const targetRepo = repos.find(repo => repo.namespace === normalizedPathname)
assert(
typeof targetRepo === 'object',
`[CANNOT_FIND_REPO] Cannot infer repoId from repoUrl: ${repoUrl}`
)

return targetRepo
}

constructor(repoId) {
this.base = 'https://www.yuque.com/api/v2/'
this.repoId = repoId
this.store = store.get(`yuque_repo_${repoId}`)
}

get(path) {
path = `${this.base}${path}`
return Yuque.fetch(path)
async getRepos() {
return Yuque.getRepos(this.base, this.repoId)
}

async getRepoDetail() {
return this.get(`repos/${this.repoId}`)
return Yuque.getRepoDetail(this.base, this.repoId)
}

async getToc() {
return this.get(`repos/${this.repoId}/toc`)
return Yuque.getToc(this.base, this.repoId)
}

async getPage(slug) {
return this.get(`repos/${this.repoId}/docs/${slug}?raw=1`)
return Yuque.getPage(this.base, this.repoId, slug)
}
}

0 comments on commit 81d07dd

Please sign in to comment.
You can’t perform that action at this time.