Skip to content

Commit 998c20f

Browse files
committed
feat(examples): add vitepress example
1 parent ff10a7c commit 998c20f

6 files changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { defineConfig } from 'vitepress'
2+
3+
// https://vitepress.dev/reference/site-config
4+
export default defineConfig({
5+
srcDir: 'docs',
6+
outDir: 'dist',
7+
title: 'VitePress Example',
8+
description: 'An example for @web-analytics/vue with VitePress.',
9+
themeConfig: {
10+
// https://vitepress.dev/reference/default-theme-config
11+
nav: [
12+
{ text: 'Home', link: '/' },
13+
{ text: 'Examples', link: '/markdown-examples' },
14+
],
15+
16+
sidebar: [
17+
{
18+
text: 'Examples',
19+
items: [
20+
{ text: 'Markdown Examples', link: '/markdown-examples' },
21+
{ text: 'Runtime API Examples', link: '/api-examples' },
22+
],
23+
},
24+
],
25+
26+
socialLinks: [
27+
{ icon: 'github', link: 'https://github.com/vuejs/vitepress' },
28+
],
29+
},
30+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { inBrowser } from 'vitepress'
2+
import defaultTheme from 'vitepress/theme'
3+
import { createVitePressBaiduAnalytics } from '@web-analytics/vue'
4+
import type { Theme } from 'vitepress'
5+
6+
const { baiduAnalytics, registerBaiduAnalytics } =
7+
createVitePressBaiduAnalytics()
8+
9+
export { baiduAnalytics }
10+
11+
const theme: Theme = {
12+
...defaultTheme,
13+
enhanceApp({ app, router }) {
14+
if (inBrowser) {
15+
// Complete the registration first
16+
registerBaiduAnalytics(app, {
17+
websiteIds: ['you_website_id_1', 'you_website_id_2'],
18+
debug: true,
19+
})
20+
21+
// Define the behavior of the `onAfterRouteChanged` hook
22+
router.onAfterRouteChanged = (to) => {
23+
baiduAnalytics.trackPageview({
24+
pageUrl: to,
25+
})
26+
}
27+
}
28+
},
29+
}
30+
31+
export default theme
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# Runtime API Examples
6+
7+
This page demonstrates usage of some of the runtime APIs provided by VitePress.
8+
9+
The main `useData()` API can be used to access site, theme, and page data for the current page. It works in both `.md` and `.vue` files:
10+
11+
```md
12+
<script setup>
13+
import { useData } from 'vitepress'
14+
15+
const { theme, page, frontmatter } = useData()
16+
</script>
17+
18+
## Results
19+
20+
### Theme Data
21+
<pre>{{ theme }}</pre>
22+
23+
### Page Data
24+
<pre>{{ page }}</pre>
25+
26+
### Page Frontmatter
27+
<pre>{{ frontmatter }}</pre>
28+
```
29+
30+
<script setup>
31+
import { useData } from 'vitepress'
32+
33+
const { site, theme, page, frontmatter } = useData()
34+
</script>
35+
36+
## Results
37+
38+
### Theme Data
39+
<pre>{{ theme }}</pre>
40+
41+
### Page Data
42+
<pre>{{ page }}</pre>
43+
44+
### Page Frontmatter
45+
<pre>{{ frontmatter }}</pre>
46+
47+
## More
48+
49+
Check out the documentation for the [full list of runtime APIs](https://vitepress.dev/reference/runtime-api#usedata).

examples/vitepress/docs/index.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
layout: home
3+
---
4+
5+
<div class="home">
6+
<a href="https://analytics.js.org/vue/" target="_blank">
7+
<img :src="logo" class="logo" alt="Web Analytics Logo" />
8+
</a>
9+
<h1>Vue3 Example</h1>
10+
<p class="read-the-docs">Press `F12` and see the debug log</p>
11+
<div class="card">
12+
<button id="tp" type="button" @click="trackPageview">
13+
Track Pageview
14+
</button>
15+
<button id="te" type="button" @click="trackEvent">Track Event</button>
16+
</div>
17+
</div>
18+
19+
<script setup lang="ts">
20+
import { capitalize } from '@bassist/utils'
21+
import { baiduAnalytics } from '../.vitepress/theme'
22+
import logo from 'shared/logo.svg'
23+
24+
function trackPageview() {
25+
baiduAnalytics.trackPageview({
26+
pageUrl: window.location.href,
27+
})
28+
}
29+
30+
function trackEvent() {
31+
baiduAnalytics.trackEvent({
32+
category: 'button',
33+
action: 'click',
34+
label: 'Track Event Button',
35+
value: 1,
36+
})
37+
}
38+
</script>
39+
40+
<style scoped>
41+
@import 'shared/style.css';
42+
43+
.home {
44+
display: flex;
45+
flex-direction: column;
46+
gap: 1em;
47+
font-size: 19px;
48+
text-align: center;
49+
}
50+
51+
.logo {
52+
margin: 0 auto;
53+
}
54+
</style>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Markdown Extension Examples
2+
3+
This page demonstrates some of the built-in markdown extensions provided by VitePress.
4+
5+
## Syntax Highlighting
6+
7+
VitePress provides Syntax Highlighting powered by [Shiki](https://github.com/shikijs/shiki), with additional features like line-highlighting:
8+
9+
**Input**
10+
11+
````
12+
```js{4}
13+
export default {
14+
data () {
15+
return {
16+
msg: 'Highlighted!'
17+
}
18+
}
19+
}
20+
```
21+
````
22+
23+
**Output**
24+
25+
```js{4}
26+
export default {
27+
data () {
28+
return {
29+
msg: 'Highlighted!'
30+
}
31+
}
32+
}
33+
```
34+
35+
## Custom Containers
36+
37+
**Input**
38+
39+
```md
40+
::: info
41+
This is an info box.
42+
:::
43+
44+
::: tip
45+
This is a tip.
46+
:::
47+
48+
::: warning
49+
This is a warning.
50+
:::
51+
52+
::: danger
53+
This is a dangerous warning.
54+
:::
55+
56+
::: details
57+
This is a details block.
58+
:::
59+
```
60+
61+
**Output**
62+
63+
::: info
64+
This is an info box.
65+
:::
66+
67+
::: tip
68+
This is a tip.
69+
:::
70+
71+
::: warning
72+
This is a warning.
73+
:::
74+
75+
::: danger
76+
This is a dangerous warning.
77+
:::
78+
79+
::: details
80+
This is a details block.
81+
:::
82+
83+
## More
84+
85+
Check out the documentation for the [full list of markdown extensions](https://vitepress.dev/guide/markdown).

examples/vitepress/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "vitepress-example",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "vitepress dev",
7+
"build": "vitepress build",
8+
"preview": "vitepress preview"
9+
},
10+
"dependencies": {
11+
"@web-analytics/vue": "workspace:^",
12+
"shared": "workspace:^"
13+
},
14+
"devDependencies": {
15+
"vitepress": "1.0.0-alpha.72"
16+
}
17+
}

0 commit comments

Comments
 (0)