From 93095f64a7202fcd0e5dd0b87102928fdb19b14e Mon Sep 17 00:00:00 2001 From: Wilson Lemos Date: Thu, 14 Apr 2022 17:50:11 -0700 Subject: [PATCH 1/5] implement option to add trailing slash to internal links --- index.coffee | 6 ++++++ index.js | 10 +++++++++- smart-link.coffee | 18 +++++++++++++----- smart-link.js | 14 ++++++++++---- 4 files changed, 38 insertions(+), 10 deletions(-) diff --git a/index.coffee b/index.coffee index e2a6ae5..b31b2ad 100644 --- a/index.coffee +++ b/index.coffee @@ -96,6 +96,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.endsWith "/" 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..c52a9d9 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'); @@ -174,6 +174,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.endsWith("/") ? "" : "/"; + 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/smart-link.coffee b/smart-link.coffee index ebead60..cab152c 100644 --- a/smart-link.coffee +++ b/smart-link.coffee @@ -1,22 +1,27 @@ # 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 + internalTrailingSlash: Boolean # Optionally add trailing slash if is internal link # Destructure the props and data we care about render: (create, { - props: { to } + # props: { to, internalTrailingSlash } + props data listeners children }) -> + to = props.to + internalTrailingSlash = props.internalTrailingSlash + console.log 'rendering link, to is', to, 'internaltrailingslash is', internalTrailingSlash, 'props are', props # If no "to", wrap children in a span so that children are nested # consistently @@ -29,7 +34,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 internalTrailingSlash + 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..349c71e 100644 --- a/smart-link.js +++ b/smart-link.js @@ -22,17 +22,23 @@ 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 + internalTrailingSlash: Boolean // Optionally add trailing slash if is internal link }, + // Destructure the props and data we care about + // props: { to, internalTrailingSlash } render: function render(create, _ref) { - var to = _ref.props.to, + var props = _ref.props, data = _ref.data, listeners = _ref.listeners, children = _ref.children; + var internalTrailingSlash, to; + to = props.to; + internalTrailingSlash = props.internalTrailingSlash; + console.log('rendering link, to is', to, 'internaltrailingslash is', internalTrailingSlash, 'props are', props); if (!to) { return create('span', data, children); } @@ -42,7 +48,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: internalTrailingSlash ? (0, _index.makeRouterPath)((0, _index.addTrailingSlash)(to)) : (0, _index.makeRouterPath)(to) } }), children); } else { From 7f8ed25b9be7f1af5a76ff4a66ca38a50faf5f1a Mon Sep 17 00:00:00 2001 From: Wilson Lemos Date: Thu, 14 Apr 2022 17:51:47 -0700 Subject: [PATCH 2/5] remove unused code and logs --- smart-link.coffee | 7 +------ smart-link.js | 9 +++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/smart-link.coffee b/smart-link.coffee index cab152c..c685dab 100644 --- a/smart-link.coffee +++ b/smart-link.coffee @@ -13,16 +13,11 @@ export default # Destructure the props and data we care about render: (create, { - # props: { to, internalTrailingSlash } - props + props: { to, internalTrailingSlash } data listeners children }) -> - to = props.to - internalTrailingSlash = props.internalTrailingSlash - console.log 'rendering link, to is', to, 'internaltrailingslash is', internalTrailingSlash, 'props are', props - # If no "to", wrap children in a span so that children are nested # consistently if !to then return create 'span', data, children diff --git a/smart-link.js b/smart-link.js index 349c71e..a18b8a0 100644 --- a/smart-link.js +++ b/smart-link.js @@ -28,17 +28,14 @@ exports.default = { }, // Destructure the props and data we care about - // props: { to, internalTrailingSlash } render: function render(create, _ref) { - var props = _ref.props, + var _ref$props = _ref.props, + to = _ref$props.to, + internalTrailingSlash = _ref$props.internalTrailingSlash, data = _ref.data, listeners = _ref.listeners, children = _ref.children; - var internalTrailingSlash, to; - to = props.to; - internalTrailingSlash = props.internalTrailingSlash; - console.log('rendering link, to is', to, 'internaltrailingslash is', internalTrailingSlash, 'props are', props); if (!to) { return create('span', data, children); } From 7a8fdc855bd414767171fb19e016ccedc741a9dd Mon Sep 17 00:00:00 2001 From: Wilson Lemos Date: Wed, 20 Apr 2022 08:54:19 -0700 Subject: [PATCH 3/5] pr changes --- index.coffee | 1 + index.js | 3 +++ nuxt/module.js | 1 + smart-link.coffee | 6 +++--- smart-link.js | 13 ++++++------- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/index.coffee b/index.coffee index b31b2ad..af67590 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: [] diff --git a/index.js b/index.js index c52a9d9..6ce14f9 100644 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ var bind, // Default settings settings = { addBlankToExternal: false, + addTrailingSlashToInternal: false, internalUrls: [], sameWindowUrls: [], internalHosts: [], @@ -183,6 +184,8 @@ var addTrailingSlash = exports.addTrailingSlash = function addTrailingSlash(to) }; exports.default = { + // if to.match /\/$/ then to else to + '/' + // Directive definition with settings method for overriding the default settings. // I'm relying on Browser garbage collection to cleanup listeners. bind: bind, diff --git a/nuxt/module.js b/nuxt/module.js index 3ecaa24..806d7ae 100644 --- a/nuxt/module.js +++ b/nuxt/module.js @@ -6,6 +6,7 @@ module.exports = function (options) { // Accept options from module or config options = {...options, ...this.options.anchorParser} + 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 c685dab..59db388 100644 --- a/smart-link.coffee +++ b/smart-link.coffee @@ -9,14 +9,14 @@ export default props: to: String # The URL gets passed here - internalTrailingSlash: Boolean # Optionally add trailing slash if is internal link # Destructure the props and data we care about render: (create, { - props: { to, internalTrailingSlash } + props: { to } data listeners children + parent }) -> # If no "to", wrap children in a span so that children are nested # consistently @@ -30,7 +30,7 @@ export default ...data nativeOn: listeners # nuxt-link doesn't forward events on it's own props: - to: if internalTrailingSlash + to: if parent?.$config?.anchorParser?.addTrailingSlashToInternal then makeRouterPath addTrailingSlash to else makeRouterPath to }, children diff --git a/smart-link.js b/smart-link.js index a18b8a0..2eb3513 100644 --- a/smart-link.js +++ b/smart-link.js @@ -23,19 +23,18 @@ exports.default = { name: 'SmartLink', functional: true, props: { - to: String, // The URL gets passed here - internalTrailingSlash: Boolean // Optionally add trailing slash if is internal link + to: String // The URL gets passed here }, // Destructure the props and data we care about render: function render(create, _ref) { - var _ref$props = _ref.props, - to = _ref$props.to, - internalTrailingSlash = _ref$props.internalTrailingSlash, + 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); } @@ -45,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: internalTrailingSlash ? (0, _index.makeRouterPath)((0, _index.addTrailingSlash)(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 { From 63c6b491cfe4cad2f95c8349eced199d188736a8 Mon Sep 17 00:00:00 2001 From: Wilson Lemos Date: Wed, 20 Apr 2022 08:58:04 -0700 Subject: [PATCH 4/5] replace endsWith with regex match --- index.coffee | 2 +- index.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/index.coffee b/index.coffee index af67590..66eb1ba 100644 --- a/index.coffee +++ b/index.coffee @@ -100,7 +100,7 @@ export shouldOpenInNewWindow = (url) -> # add trailing slash export addTrailingSlash = (to) -> url = new URL to - url.pathname += if url.pathname.endsWith "/" then "" else "/" + url.pathname += if url.pathname.match(/\/$/) then "" else "/" url.toString() # Directive definition with settings method for overriding the default settings. diff --git a/index.js b/index.js index 6ce14f9..00de52e 100644 --- a/index.js +++ b/index.js @@ -179,13 +179,11 @@ var shouldOpenInNewWindow = exports.shouldOpenInNewWindow = function shouldOpenI var addTrailingSlash = exports.addTrailingSlash = function addTrailingSlash(to) { var url; url = new _urlParse2.default(to); - url.pathname += url.pathname.endsWith("/") ? "" : "/"; + url.pathname += url.pathname.match(/\/$/) ? "" : "/"; return url.toString(); }; exports.default = { - // if to.match /\/$/ then to else to + '/' - // Directive definition with settings method for overriding the default settings. // I'm relying on Browser garbage collection to cleanup listeners. bind: bind, From 1dbca123305ae2ada86f50442f247b06d1e9ef6f Mon Sep 17 00:00:00 2001 From: Wilson Lemos Date: Fri, 22 Apr 2022 09:23:54 -0700 Subject: [PATCH 5/5] add comment --- nuxt/module.js | 1 + 1 file changed, 1 insertion(+) diff --git a/nuxt/module.js b/nuxt/module.js index 806d7ae..76f5b6f 100644 --- a/nuxt/module.js +++ b/nuxt/module.js @@ -6,6 +6,7 @@ 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