add optional current route param to toURL and use it to properly compose local anchor links

This commit is contained in:
Bojan Djurkovic 2017-03-31 09:08:05 -03:00
commit a706203879
2 changed files with 5 additions and 4 deletions

View file

@ -82,7 +82,7 @@ renderer.code = function (code, lang = '') {
renderer.link = function (href, title, text) { renderer.link = function (href, title, text) {
let blank = '' let blank = ''
if (!/:|(\/{2})/.test(href)) { if (!/:|(\/{2})/.test(href)) {
href = toURL(href) href = toURL(href, null, currentPath)
} else { } else {
blank = ' target="_blank"' blank = ' target="_blank"'
} }

View file

@ -64,16 +64,17 @@ export function parse (path = window.location.href) {
* to URL * to URL
* @param {string} path * @param {string} path
* @param {object} qs query params * @param {object} qs query params
* @param {string} currentRoute optional current route
*/ */
export function toURL (path, params) { export function toURL (path, params, currentRoute) {
const inline = path[0] === '#' const local = currentRoute && path[0] === '#'
const route = parse(replaceSlug(path)) const route = parse(replaceSlug(path))
route.query = merge({}, route.query, params) route.query = merge({}, route.query, params)
path = route.path + stringifyQuery(route.query) path = route.path + stringifyQuery(route.query)
path = path.replace(/\.md(\?)|\.md$/, '$1') path = path.replace(/\.md(\?)|\.md$/, '$1')
if (inline) path = currentPath + path if (local) path = currentRoute + path
return cleanPath('#/' + path) return cleanPath('#/' + path)
} }