Created
July 20, 2020 14:13
-
-
Save johndigital/21b04f00abca2dca35595289fd51e680 to your computer and use it in GitHub Desktop.
Prismic/Nuxt Relative Link Handling. https://github.com/nuxt-community/prismic-module/issues/60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import linkResolver from './linkResolver' | |
import prismicDOM from 'prismic-dom' | |
const Elements = prismicDOM.RichText.Elements | |
// https://prismic.io/docs/vuejs/getting-started/prismic-nuxt | |
export default function (type, element, content, children) { | |
// Generate links to Prismic Documents as <router-link> components | |
// Present by default, it is recommended to keep this | |
if (type === Elements.hyperlink) { | |
let result = '' | |
const url = prismicDOM.Link.url(element.data, linkResolver) | |
if (element.data.link_type === 'Document') { | |
result = `<a href="${url}" data-nuxt-link>${content}</a>` | |
} else { | |
const target = element.data.target | |
? `target="'${element.data.target}'" rel="noopener"` | |
: '' | |
result = `<a href="${url}" ${target}>${content}</a>` | |
} | |
return result | |
} | |
// If the image is also a link to a Prismic Document, it will return a <router-link> component | |
// Present by default, it is recommended to keep this | |
if (type === Elements.image) { | |
let result = `<img src="${element.url}" alt="${ | |
element.alt || '' | |
}" copyright="${element.copyright || ''}">` | |
if (element.linkTo) { | |
const url = prismicDOM.Link.url(element.linkTo, linkResolver) | |
if (element.linkTo.link_type === 'Document') { | |
result = `<a href="${url}" data-nuxt-link>${result}</a>` | |
} else { | |
const target = element.linkTo.target | |
? `target="${element.linkTo.target}" rel="noopener"` | |
: '' | |
result = `<a href="${url}" ${target}>${result}</a>` | |
} | |
} | |
const wrapperClassList = [element.label || '', 'block-img'] | |
result = `<p class="${wrapperClassList.join(' ')}">${result}</p>` | |
return result | |
} | |
// Return null to stick with the default behavior for everything else | |
return null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// ...other configuration | |
plugins: [ | |
// ...other plugins | |
{ src: '~/plugins/prismicLinks', ssr: false } | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default async ({ 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 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment