Skip to content

Commit

Permalink
Merge pull request #32 from lemoswilson/add-trailing-slash-to-interna…
Browse files Browse the repository at this point in the history
…l-links-option

Add trailing slash to internal links option
  • Loading branch information
weotch authored Jun 24, 2022
2 parents ac67b2f + 1dbca12 commit 0bea2af
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
7 changes: 7 additions & 0 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import URL from 'url-parse'
# Default settings
settings =
addBlankToExternal: false
addTrailingSlashToInternal: false
internalUrls: []
sameWindowUrls: []
internalHosts: []
Expand Down Expand Up @@ -96,6 +97,12 @@ export shouldOpenInNewWindow = (url) ->
return false if urlObj.href.match urlRegex
return true

# add trailing slash
export addTrailingSlash = (to) ->
url = new URL to
url.pathname += if url.pathname.match(/\/$/) then "" else "/"
url.toString()

# Directive definition with settings method for overriding the default settings.
# I'm relying on Browser garbage collection to cleanup listeners.
export default
Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shouldOpenInNewWindow = exports.makeRouterPath = exports.isInternal = exports.handleAnchor = undefined;
exports.addTrailingSlash = exports.shouldOpenInNewWindow = exports.makeRouterPath = exports.isInternal = exports.handleAnchor = undefined;

var _urlParse = require('url-parse');

Expand Down Expand Up @@ -33,6 +33,7 @@ var bind,
// Default settings
settings = {
addBlankToExternal: false,
addTrailingSlashToInternal: false,
internalUrls: [],
sameWindowUrls: [],
internalHosts: [],
Expand Down Expand Up @@ -174,6 +175,14 @@ var shouldOpenInNewWindow = exports.shouldOpenInNewWindow = function shouldOpenI
return true;
};

// add trailing slash
var addTrailingSlash = exports.addTrailingSlash = function addTrailingSlash(to) {
var url;
url = new _urlParse2.default(to);
url.pathname += url.pathname.match(/\/$/) ? "" : "/";
return url.toString();
};

exports.default = {
// Directive definition with settings method for overriding the default settings.
// I'm relying on Browser garbage collection to cleanup listeners.
Expand Down
2 changes: 2 additions & 0 deletions nuxt/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = function (options) {

// Accept options from module or config
options = {...options, ...this.options.anchorParser}
// Expose module options to publicRuntimeConfig
this.options.publicRuntimeConfig.anchorParser = options

// Register a plugin that applys the settings for SSR and non-SSR
this.addPlugin({
Expand Down
13 changes: 8 additions & 5 deletions smart-link.coffee
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Render a nuxt-link if an internal link or a v-parse-anchor wrapped a if not.
# This is so that link pre-fetching works.

import { isInternal, makeRouterPath, shouldOpenInNewWindow } from './index'
import { isInternal, makeRouterPath, shouldOpenInNewWindow, addTrailingSlash } from './index'

export default
name: 'SmartLink'
functional: true

# The URL gets passed here
props: to: String
props:
to: String # The URL gets passed here

# Destructure the props and data we care about
render: (create, {
props: { to }
data
listeners
children
parent
}) ->

# If no "to", wrap children in a span so that children are nested
# consistently
if !to then return create 'span', data, children
Expand All @@ -29,7 +29,10 @@ export default
then create 'nuxt-link', {
...data
nativeOn: listeners # nuxt-link doesn't forward events on it's own
props: to: makeRouterPath to
props:
to: if parent?.$config?.anchorParser?.addTrailingSlashToInternal
then makeRouterPath addTrailingSlash to
else makeRouterPath to
}, children

# Make a standard link that opens in a new window
Expand Down
10 changes: 6 additions & 4 deletions smart-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ var _extends = Object.assign || function (target) {
exports.default = {
name: 'SmartLink',
functional: true,
// The URL gets passed here
props: {
to: String
to: String // The URL gets passed here
},

// Destructure the props and data we care about
render: function render(create, _ref) {
var to = _ref.props.to,
data = _ref.data,
listeners = _ref.listeners,
children = _ref.children;
children = _ref.children,
parent = _ref.parent;

var ref, ref1;
if (!to) {
return create('span', data, children);
}
Expand All @@ -42,7 +44,7 @@ exports.default = {
return create('nuxt-link', _extends({}, data, {
nativeOn: listeners, // nuxt-link doesn't forward events on it's own
props: {
to: (0, _index.makeRouterPath)(to)
to: (parent != null ? (ref = parent.$config) != null ? (ref1 = ref.anchorParser) != null ? ref1.addTrailingSlashToInternal : void 0 : void 0 : void 0) ? (0, _index.makeRouterPath)((0, _index.addTrailingSlash)(to)) : (0, _index.makeRouterPath)(to)
}
}), children);
} else {
Expand Down

0 comments on commit 0bea2af

Please sign in to comment.