Skip to content

Commit

Permalink
feat(docz-core): add editBranch option to mount repo link
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Aug 16, 2018
1 parent b15457c commit c619d9c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/docz-core/src/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface Argv {
files: string
ignore: string[]
dest: string
editBranch: string
/* bundler args */
debug: boolean
typescript: boolean
Expand Down Expand Up @@ -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)),
Expand Down
22 changes: 19 additions & 3 deletions packages/docz-core/src/utils/repo-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const getRepoUrl = () => {

return (
repo &&
repo.browsetemplate &&
repo.browsetemplate
.replace('{domain}', repo.domain)
.replace('{user}', repo.user)
Expand All @@ -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)
Expand Down

0 comments on commit c619d9c

Please sign in to comment.