From c619d9cf44c3d15780aff3f9a07391c0219610c3 Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Wed, 15 Aug 2018 19:14:57 -0300 Subject: [PATCH] feat(docz-core): add editBranch option to mount repo link --- packages/docz-core/src/Entries.ts | 2 +- packages/docz-core/src/Entry.ts | 4 +++- packages/docz-core/src/commands/args.ts | 7 +++++++ packages/docz-core/src/utils/repo-info.ts | 22 +++++++++++++++++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/docz-core/src/Entries.ts b/packages/docz-core/src/Entries.ts index a79c1ccda..fead762db 100644 --- a/packages/docz-core/src/Entries.ts +++ b/packages/docz-core/src/Entries.ts @@ -71,7 +71,7 @@ export class Entries { public repoEditUrl: string | null constructor(config: Config) { - this.repoEditUrl = getRepoEditUrl(config.src) + this.repoEditUrl = getRepoEditUrl(config.src, config.editBranch) this.all = new Map() this.get = async () => this.getMap(config) } diff --git a/packages/docz-core/src/Entry.ts b/packages/docz-core/src/Entry.ts index ab5cc03b8..e33c79bcd 100644 --- a/packages/docz-core/src/Entry.ts +++ b/packages/docz-core/src/Entry.ts @@ -111,7 +111,9 @@ export class Entry { } public setLink(url: string): void { - this.link = `${url}/${this.filepath}` + if (url) { + this.link = url.replace('{{filepath}}', this.filepath) + } } private getFilepath(file: string, src: string): string { diff --git a/packages/docz-core/src/commands/args.ts b/packages/docz-core/src/commands/args.ts index dad3183a7..f7bfe6bd5 100644 --- a/packages/docz-core/src/commands/args.ts +++ b/packages/docz-core/src/commands/args.ts @@ -46,6 +46,7 @@ export interface Argv { files: string ignore: string[] dest: string + editBranch: string /* bundler args */ debug: boolean typescript: boolean @@ -104,6 +105,12 @@ export const args = (env: Env) => (yargs: any) => { type: 'string', default: getEnv('docz.dest', '.docz/dist'), }) + yargs.positional('editBranch', { + alias: 'eb', + type: 'string', + default: getEnv('docz.edit.branch', 'master'), + }) + yargs.positional('title', { type: 'string', default: getEnv('docz.title', getInitialTitle(pkg)), diff --git a/packages/docz-core/src/utils/repo-info.ts b/packages/docz-core/src/utils/repo-info.ts index 8bce2f371..1a7c1cd57 100644 --- a/packages/docz-core/src/utils/repo-info.ts +++ b/packages/docz-core/src/utils/repo-info.ts @@ -20,6 +20,7 @@ export const getRepoUrl = () => { return ( repo && + repo.browsetemplate && repo.browsetemplate .replace('{domain}', repo.domain) .replace('{user}', repo.user) @@ -28,16 +29,31 @@ export const getRepoUrl = () => { ) } -export const getRepoEditUrl = (src: string): string | null => { +const getBitBucketPath = (branch: string, relative: string) => { + const querystring = `?mode=edit&spa=0&at=${branch}&fileviewer=file-view-default` + const filepath = path.join(`/src/${branch}`, relative, `{{filepath}}`) + return `${filepath}${querystring}` +} + +const getTree = (repo: any, branch: string, relative: string) => { + const defaultPath = path.join(`/edit/${branch}`, relative, `{{filepath}}`) + const bitBucketPath = getBitBucketPath(branch, relative) + + if (repo && repo.type === 'bitbucket') return bitBucketPath + return defaultPath +} + +export const getRepoEditUrl = (src: string, branch: string): string | null => { try { + const repo = parseRepo() const project = path.parse(findup.sync('.git')).dir const root = path.join(paths.root, src) const relative = path.relative(project, root) - const tree = path.join('/edit/master', relative) - const repo = parseRepo() + const tree = getTree(repo, branch, relative) return ( repo && + repo.browsetemplate && repo.browsetemplate .replace('{domain}', repo.domain) .replace('{user}', repo.user)