diff --git a/index.coffee b/index.coffee index e2a6ae5..66eb1ba 100644 --- a/index.coffee +++ b/index.coffee @@ -4,6 +4,7 @@ import URL from 'url-parse' # Default settings settings = addBlankToExternal: false + addTrailingSlashToInternal: false internalUrls: [] sameWindowUrls: [] internalHosts: [] @@ -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 diff --git a/index.js b/index.js index 3bad499..00de52e 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -33,6 +33,7 @@ var bind, // Default settings settings = { addBlankToExternal: false, + addTrailingSlashToInternal: false, internalUrls: [], sameWindowUrls: [], internalHosts: [], @@ -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. diff --git a/nuxt/module.js b/nuxt/module.js index 3ecaa24..76f5b6f 100644 --- a/nuxt/module.js +++ b/nuxt/module.js @@ -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({ diff --git a/smart-link.coffee b/smart-link.coffee index ebead60..59db388 100644 --- a/smart-link.coffee +++ b/smart-link.coffee @@ -1,14 +1,14 @@ # 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, { @@ -16,8 +16,8 @@ export default 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 @@ -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 diff --git a/smart-link.js b/smart-link.js index df9c462..2eb3513 100644 --- a/smart-link.js +++ b/smart-link.js @@ -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); } @@ -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 {