From 2d3aac4fa5291823e1a4ebb6a41790026036528f Mon Sep 17 00:00:00 2001 From: Benjamin Petetot Date: Mon, 16 Nov 2020 14:48:00 +0100 Subject: [PATCH] Fix links in richtext documents --- components/PixLink.vue | 18 ++++++++++++++++-- nuxt.config.js | 1 + plugins/html-serializer.js | 2 +- plugins/link-resolver.js | 4 +++- plugins/prismicLinks.js | 16 ++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 plugins/prismicLinks.js diff --git a/components/PixLink.vue b/components/PixLink.vue index 39e2059a3..9c47fe49e 100644 --- a/components/PixLink.vue +++ b/components/PixLink.vue @@ -25,15 +25,17 @@ export default { const relativeLinkPrefix = getRelativeLinkPrefix(url) if (this.field.link_type === 'Document') { + const localeURL = getLocaleUrl(url, this.localePath) template = ` - + ` } else if (relativeLinkPrefix) { const relativeUrl = url.replace(relativeLinkPrefix, '/') + const localeURL = getLocaleUrl(relativeUrl, this.localePath) template = ` - + ` @@ -51,6 +53,18 @@ export default { }, }, } + +function getLocaleUrl(url, localePath) { + if ( + url.startsWith('/fr') || + url.startsWith('/en-gb') || + url.startsWith('/fr-fr') + ) { + return url + } + return localePath(url) +} + function getRelativeLinkPrefix(url) { if (!url) { return '' diff --git a/nuxt.config.js b/nuxt.config.js index c7c29c07c..b8c9bfbc3 100755 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -60,6 +60,7 @@ export default { '~/plugins/meta.js', { src: '~plugins/slide-menu', ssr: false }, '~plugins/vue-js-modal', + { src: '~/plugins/prismicLinks', ssr: false }, ], /* diff --git a/plugins/html-serializer.js b/plugins/html-serializer.js index 958e8b973..abee100eb 100755 --- a/plugins/html-serializer.js +++ b/plugins/html-serializer.js @@ -11,7 +11,7 @@ export default function (type, element, content, children) { const url = prismicDOM.Link.url(element.data, linkResolver) if (element.data.link_type === 'Document') { - result = `${content}` + result = `${content}` } else { const target = element.data.target ? `target="'${element.data.target}'" rel="noopener"` diff --git a/plugins/link-resolver.js b/plugins/link-resolver.js index 79d94f396..895393576 100755 --- a/plugins/link-resolver.js +++ b/plugins/link-resolver.js @@ -8,9 +8,11 @@ export default function (doc) { 'cgu_page', 'statistiques', 'legal-notices', + 'simple_page', ] if (staticRoute.includes(doc.type)) { - return `/${doc.uid}` + const locale = doc.lang !== 'fr-fr' ? `/${doc.lang}` : '' + return `${locale}/${doc.uid}` } if (doc.type === 'news_item') { return `/actualites/${doc.uid}` diff --git a/plugins/prismicLinks.js b/plugins/prismicLinks.js new file mode 100644 index 000000000..6e53d9e7e --- /dev/null +++ b/plugins/prismicLinks.js @@ -0,0 +1,16 @@ +export default ({ redirect }) => { + window.addEventListener( + 'click', + (event) => { + // If the clicked element doesn't have the right selector, bail + if (!event.target.matches('a[data-nuxt-link]')) return + + // Don't follow the link + event.preventDefault() + + // Push link destination to router + redirect(event.target.pathname) + }, + false + ) +}