Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Commit

Permalink
feat: support "repoUrl" option
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Feb 6, 2019
1 parent d22011d commit 81d07dd
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 19 deletions.
7 changes: 4 additions & 3 deletions example/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -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/',
Expand Down
35 changes: 25 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)) {
Expand Down
65 changes: 59 additions & 6 deletions lib/yuque.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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.