Skip to content

Commit 0bc8098

Browse files
committed
add carbon ads in posts
1 parent 3006fe3 commit 0bc8098

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

docs/.vitepress/theme/components/VPDocAside.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
<script setup lang="ts">
2+
import { computed } from 'vue'
3+
import { useData } from 'vitepress'
24
import VPDocAsideOutline from 'vitepress/dist/client/theme-default/components/VPDocAsideOutline.vue'
5+
import VPCarbonAds from './VPCarbonAds.vue'
6+
7+
const { theme, page } = useData()
8+
9+
// Check if current page is a blog post article
10+
const isPost = computed(() => {
11+
const path = page.value.relativePath || page.value.filePath || ''
12+
const routePath = page.value.route || ''
13+
14+
// Post pages are in /posts/ directory, but not the list pages
15+
const isPostPath = (path.includes('/posts/') || path.startsWith('posts/')) &&
16+
path.endsWith('.md') &&
17+
path !== 'posts.md' &&
18+
path !== 'posts/index.md' &&
19+
path !== 'blog.md' &&
20+
path !== 'blog/index.md'
21+
22+
const isPostRoute = routePath.includes('/posts/') &&
23+
routePath !== '/posts' &&
24+
routePath !== '/posts/' &&
25+
routePath !== '/blog' &&
26+
routePath !== '/blog/'
27+
28+
return isPostPath || isPostRoute
29+
})
330
431
</script>
532

633
<template>
734
<div class="VPDocAside">
835
<slot name="aside-top" />
936

37+
<!-- Show carbonAds at the top of right sidebar, only for post pages -->
38+
<VPCarbonAds
39+
v-if="isPost && theme.carbonAds"
40+
:carbon-ads="theme.carbonAds"
41+
/>
42+
1043
<slot name="aside-outline-before" />
1144
<VPDocAsideOutline />
1245
<slot name="aside-outline-after" />

docs/.vitepress/theme/components/VPSidebar.vue

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
<script lang="ts" setup>
22
import { useScrollLock } from '@vueuse/core'
33
import { inBrowser } from 'vitepress'
4-
import { ref, watch } from 'vue'
4+
import { ref, watch, computed } from 'vue'
55
import { useLayout } from 'vitepress/dist/client/theme-default/composables/layout'
66
import VPSidebarGroup from 'vitepress/dist/client/theme-default/components/VPSidebarGroup.vue'
77
88
import { useData } from 'vitepress/dist/client/theme-default/composables/data'
99
import VPDocAsideCarbonAds from 'vitepress/dist/client/theme-default/components/VPDocAsideCarbonAds.vue'
10-
const { theme } = useData()
10+
const { theme, page } = useData()
1111
1212
const { sidebarGroups, hasSidebar } = useLayout()
1313
14+
// Check if current page is a blog post article
15+
const isPost = computed(() => {
16+
const path = page.value.relativePath || page.value.filePath || ''
17+
const routePath = page.value.route || ''
18+
19+
// Post pages are in /posts/ directory, but not the list pages
20+
const isPostPath = (path.includes('/posts/') || path.startsWith('posts/')) &&
21+
path.endsWith('.md') &&
22+
path !== 'posts.md' &&
23+
path !== 'posts/index.md' &&
24+
path !== 'blog.md' &&
25+
path !== 'blog/index.md'
26+
27+
const isPostRoute = routePath.includes('/posts/') &&
28+
routePath !== '/posts' &&
29+
routePath !== '/posts/' &&
30+
routePath !== '/blog' &&
31+
routePath !== '/blog/'
32+
33+
return isPostPath || isPostRoute
34+
})
35+
1436
const props = defineProps<{
1537
open: boolean
1638
}>()
@@ -58,7 +80,11 @@ watch(
5880
tabindex="-1"
5981
>
6082

61-
<VPDocAsideCarbonAds v-if="theme.carbonAds" :carbon-ads="theme.carbonAds" />
83+
<!-- Show carbonAds at the top of left sidebar, only for non-post pages (API docs, etc.) -->
84+
<VPDocAsideCarbonAds
85+
v-if="!isPost && theme.carbonAds"
86+
:carbon-ads="theme.carbonAds"
87+
/>
6288

6389
<span class="visually-hidden" id="sidebar-aria-label">
6490
Sidebar Navigation

0 commit comments

Comments
 (0)