Skip to content

Commit

Permalink
fix: content page layout shift
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdayo committed May 6, 2024
1 parent 2e9d388 commit 32705ec
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions .vitepress/theme/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ onMounted(() => {
)
})
// Post related
// Current post
const currentPost = ref<Post | undefined>(undefined)
watch(
() => page.value.relativePath,
(newPath) => {
// Find current post
const postId = newPath.match(/posts\/(.*)\//)?.[1]
currentPost.value = postId ? posts.find((post) => post.id === postId) : undefined
},
{ immediate: true }
)
// Process markdown image
const mdImgSelector = '.vp-doc img'
onMounted(() => {
watch(
() => page.value.relativePath,
async (newPath) => {
() => currentPost.value,
async (post) => {
if (!post) return
await nextTick() // Wait for the DOM to update
// Find current post
const postId = newPath.match(/posts\/(.*)\//)?.[1]
if (!postId) return
currentPost.value = posts.find((post) => post.id === postId)
// Append alt text to images
document.querySelectorAll(mdImgSelector).forEach((img) => {
const alt = img.attributes.getNamedItem('alt')
Expand Down

0 comments on commit 32705ec

Please sign in to comment.