Skip to content

Commit

Permalink
Fix link problem caused by subpath.
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed Nov 5, 2023
1 parent e9256fe commit a3a06c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
1 change: 1 addition & 0 deletions components/content/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Components defined in this folder will be available in markdown files.
6 changes: 0 additions & 6 deletions components/content/skip-check-link.vue

This file was deleted.

20 changes: 13 additions & 7 deletions components/person.vue
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
<script setup lang="ts">
defineProps<{ name: string; avatar?: string; website?: string }>();
const config = useRuntimeConfig();
function composeAvatarUrl(baseUrl: string, avatarUrl: string): string {
if (avatarUrl.startsWith("/")) {
/**
* When a subpath is used as base url, relative paths used in this site will break, like links to avatar and markdown files.
* This function will fix it by adding baseUrl if it's a local relative path link.
*/
function composeUrl(baseUrl: string, url: string): string {
if (url.startsWith("/")) {
// image is a local image
if (baseUrl.endsWith("/")) {
return `${baseUrl.substring(0, baseUrl.length - 1)}${avatarUrl}`;
return `${baseUrl.substring(0, baseUrl.length - 1)}${url}`;
} else {
return `${baseUrl}${avatarUrl}`;
return `${baseUrl}${url}`;
}
} else {
// is remote image
return avatarUrl;
return url;
}
}
</script>
<template>
<div class="flex justify-center p-5">
<NuxtLink class="flex flex-col" :href="website" target="_blank">
<NuxtLink class="flex flex-col" :href="composeUrl(config.public.baseURL, website)" target="_blank">
<div class="img-container overflow-hidden relative h-52 w-40">
<img
v-if="avatar"
:src="composeAvatarUrl(config.public.baseURL, avatar)"
:src="composeUrl(config.public.baseURL, avatar)"
alt=""
class="rounded-lg object-cover object-top w-full h-full"
/>
Expand Down
1 change: 0 additions & 1 deletion content/2.documentation/contributor.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ It can be a remote image, or a local image. If it's a local image, you should pu

If you include the `website` field, clicking on your avatar will link to the website you specified.

<!-- Otherwise it by default will link to your profile on this website. For example, for me it will link to :skip-check-link{url=/members/grad-student/huakun-shen} -->
<!-- markdown-link-check-disable-next-line -->
Otherwise it by default will link to your profile on this website. For example, for me it will link to [/members/grad-student/huakun-shen](/members/grad-student/huakun-shen)
If you include the `website` field, clicking on your avatar will link to the website you specified.
Expand Down

0 comments on commit a3a06c0

Please sign in to comment.