Skip to content

Commit

Permalink
feat: allows relative path, fixed #590
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Brocard committed Mar 25, 2019
1 parent d80aa21 commit 31654f1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
40 changes: 40 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,46 @@ window.$docsify = {
};
```

## relativePath

- Type: `Boolean`
- Default: `false`

If **true** links are relative to the current context.

For example, the directory structure is as follows:

```text
.
└── docs
├── README.md
├── guide.md
└── zh-cn
├── README.md
├── guide.md
└── config
└── example.md
```

With relative path **enabled** and current URL `http://domain.com/zh-cn/README`, given links will resolve to:

```text
guide.md => http://domain.com/zh-cn/guide
config/example.md => http://domain.com/zh-cn/config/example
../README.md => http://domain.com/README
/README.md => http://domain.com/README
```

```js
window.$docsify = {
// Relative path enabled
relativePath: true,

// Relative path disabled (default value)
relativePath: false
};
```

## coverpage

- Type: `Boolean|String|String[]|Object`
Expand Down
3 changes: 2 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default function () {
formatUpdated: '',
externalLinkTarget: '_blank',
routerMode: 'hash',
noCompileLinks: []
noCompileLinks: [],
relativePath: false
},
window.$docsify
)
Expand Down
7 changes: 6 additions & 1 deletion src/core/router/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
isAbsolutePath,
stringifyQuery,
cleanPath,
replaceSlug
replaceSlug,
resolvePath
} from '../util'
import {noop, merge} from '../../util/core'

Expand Down Expand Up @@ -76,6 +77,10 @@ export class History {
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
}

if (this.config.relativePath && !path.startsWith('/')) {
const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1)
return cleanPath(resolvePath(currentDir + path))
}
return cleanPath('/' + path)
}
}
14 changes: 14 additions & 0 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ export const cleanPath = cached(path => {
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
})

export const resolvePath = cached(path => {
const segments = path.replace(/^\//, '').split('/')
let resolved = []
for (let i = 0, len = segments.length; i < len; i++) {
const segment = segments[i]
if (segment === '..') {
resolved.pop()
} else if (segment !== '.') {
resolved.push(segment)
}
}
return '/' + resolved.join('/')
})

export function getPath(...args) {
return cleanPath(args.join('/'))
}
Expand Down

0 comments on commit 31654f1

Please sign in to comment.