Skip to content

Commit

Permalink
Strip the router.base from the path
Browse files Browse the repository at this point in the history
  • Loading branch information
weotch committed Jun 25, 2022
1 parent 6ff4fa8 commit 5b5baa0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 10 additions & 3 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,23 @@ makeUrlObj = (url) ->
# know about a route, it will not be handled by vue-router. Though it won't
# open in a new window.
handleInternal = (anchor, url, router) ->
path = makeRouterPath url
path = makeRouterPath url, { router }
if router.resolve({ path }).route.matched.length
anchor.addEventListener 'click', (e) ->
e.preventDefault()
router.push { path }

# Make routeable path
export makeRouterPath = (url) ->
export makeRouterPath = (url, { router } = {}) ->
urlObj = makeUrlObj url
"#{urlObj.pathname}#{urlObj.query}#{urlObj.hash}"

# Remove the router.base from the path, if it exists
path = urlObj.pathname
if (base = router?.options?.base) and path.indexOf(base) == 0
then path = '/' + path.slice base.length

# Create path with query and hash
"#{path}#{urlObj.query}#{urlObj.hash}"

# Add target blank to external links
handleExternal = (anchor, url) ->
Expand Down
9 changes: 5 additions & 4 deletions smart-link.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ export default
# consistently
if !to then return create 'span', data, children

# Add trailing slashes if configured to
if parent?.$config?.anchorParser?.addTrailingSlashToInternal
then to = addTrailingSlash to

# Test if an internal link
if isInternal to

# Render a nuxt-link
then create 'nuxt-link', {
...data
nativeOn: listeners # nuxt-link doesn't forward events on it's own
props:
to: if parent?.$config?.anchorParser?.addTrailingSlashToInternal
then makeRouterPath addTrailingSlash to
else makeRouterPath to
props: makeRouterPath to, { router: parent?.$router }
}, children

# Make a standard link that opens in a new window
Expand Down

0 comments on commit 5b5baa0

Please sign in to comment.