Skip to content

Commit

Permalink
Rework link logic to directive
Browse files Browse the repository at this point in the history
  • Loading branch information
rijkvanzanten committed Sep 18, 2023
1 parent 8c69ec3 commit 171d36d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion components/Base/Heading.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const iconSize = computed(() => {
<BaseIcon v-if="icon && size !== 'title'" :name="icon" :size="iconSize" :weight="700" />

<!-- eslint-disable-next-line vue/no-v-html -->
<span class="content" v-html="content" />
<span v-links class="content" v-html="content" />
</component>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion components/Base/Quote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defineProps<BaseQuoteProps>();
<template>
<div class="base-quote">
<!-- eslint-disable-next-line vue/no-v-html -->
<blockquote class="quote" v-html="quote" />
<blockquote v-links class="quote" v-html="quote" />

<BaseByline :name="personName" :title="personTitle" :image="personImage" />
</div>
Expand Down
31 changes: 1 addition & 30 deletions components/Base/Text.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,13 @@ withDefaults(defineProps<BaseTextProps>(), {
align: 'start',
size: 'medium',
});
const config = useRuntimeConfig();
const contentEl = ref<HTMLElement | null>(null);
onMounted(() => {
if (!contentEl.value) return;
// Intercept all the local links
const anchors = contentEl.value.getElementsByTagName('a');
if (!anchors) return;
Array.from(anchors).forEach((anchor) => {
const url = anchor.getAttribute('href');
if (!url) return;
// Add target blank to external links
if (!url.startsWith(config.public.site.url) && !url.startsWith('/')) {
anchor.setAttribute('target', '_blank');
anchor.setAttribute('rel', 'noopener noreferrer');
return;
} else {
// Add on click event to local links
anchor.addEventListener('click', (e) => {
e.preventDefault();
navigateTo(url);
});
}
});
});
</script>

<template>
<div class="base-text-container">
<!-- eslint-disable vue/no-v-html -->
<div
ref="contentEl"
v-links
class="base-text"
:class="[`align-${align}`, `size-${size}`, `type-${type}`, `color-${color}`]"
v-html="content"
Expand Down
1 change: 1 addition & 0 deletions components/Block/Code.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const activeSnippet = ref(0);
v-for="(snippet, index) in snippets"
v-show="activeSnippet === index"
:key="snippet.name"
v-links
class="snippet"
v-html="snippet.html"
/>
Expand Down
2 changes: 1 addition & 1 deletion components/Nav/Banner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const dismiss = (id: string) => {
<NuxtLink class="link" :href="banner.link ?? undefined">
<BaseIcon v-if="banner.icon" class="icon" :name="banner.icon" size="small" />
<!-- eslint-disable-next-line vue/no-v-html -->
<span class="content" v-html="banner.content" />
<span v-links class="content" v-html="banner.content" />
<BaseIcon class="arrow" name="arrow_forward" size="small" />
</NuxtLink>

Expand Down
2 changes: 1 addition & 1 deletion components/Nav/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const socials = {
</NuxtLink>

<!-- eslint-disable-next-line vue/no-v-html -->
<div v-if="globals" class="description" v-html="globals.description" />
<div v-if="globals" v-links class="description" v-html="globals.description" />
</li>

<li v-for="group of navPrimary.items" :key="group.id">
Expand Down
33 changes: 33 additions & 0 deletions plugins/links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.directive('links', {
mounted(el: HTMLElement) {
const anchors = Array.from(el.getElementsByTagName('a'));

for (const anchor of anchors) {
const href = anchor.getAttribute('href');

if (!href) return;

const url = new URL(href, window.location.origin);

const isLocal = url.hostname === 'directus.io';

if (isLocal) {
anchor.addEventListener('click', (e) => {
e.preventDefault();
const { pathname, searchParams, hash } = new URL(anchor.href);

navigateTo({
path: pathname,
hash: hash,
query: Object.fromEntries(searchParams.entries()),
});
});
} else {
anchor.setAttribute('target', '_blank');
anchor.setAttribute('rel', 'noopener noreferrer');
}
}
},
});
});

0 comments on commit 171d36d

Please sign in to comment.