|
1 | | -import { defineConfig } from 'vitepress' |
| 1 | +import { defineConfig, type HeadConfig } from 'vitepress' |
2 | 2 | import { tabsMarkdownPlugin } from 'vitepress-plugin-tabs' |
3 | 3 | import { withMermaid } from 'vitepress-plugin-mermaid' |
| 4 | +import { readFileSync } from 'node:fs' |
| 5 | +import { resolve } from 'node:path' |
| 6 | + |
| 7 | +function loadEnvVar(key: string): string | undefined { |
| 8 | + // process.env takes precedence (CI/hosting platforms set vars here) |
| 9 | + if (key in process.env) return process.env[key] || undefined |
| 10 | + // Fall back to .env file for local development |
| 11 | + try { |
| 12 | + const envFile = readFileSync(resolve(process.cwd(), '.env'), 'utf-8') |
| 13 | + const match = envFile.match(new RegExp(`^${key}=(.+)$`, 'm')) |
| 14 | + return match?.[1]?.trim() |
| 15 | + } catch { |
| 16 | + return undefined |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const posthogKey = loadEnvVar('VITE_POSTHOG_KEY') |
| 21 | +const algoliaAppId = loadEnvVar('VITE_ALGOLIA_APP_ID') |
| 22 | +const algoliaApiKey = loadEnvVar('VITE_ALGOLIA_API_KEY') |
| 23 | +const algoliaIndexName = loadEnvVar('VITE_ALGOLIA_INDEX_NAME') |
| 24 | + |
| 25 | +const searchConfig = algoliaAppId && algoliaApiKey && algoliaIndexName |
| 26 | + ? { |
| 27 | + provider: 'algolia' as const, |
| 28 | + options: { |
| 29 | + appId: algoliaAppId, |
| 30 | + apiKey: algoliaApiKey, |
| 31 | + indexName: algoliaIndexName, |
| 32 | + insights: true |
| 33 | + } |
| 34 | + } |
| 35 | + : { provider: 'local' as const } |
| 36 | + |
| 37 | +const posthogHead: HeadConfig[] = posthogKey |
| 38 | + ? [ |
| 39 | + ['script', {}, ` |
| 40 | + !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug getPageViewId captureTraceFeedback captureTraceMetric".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]); |
| 41 | + posthog.init('${posthogKey}',{api_host:'https://us.posthog.com', opt_out_capturing_by_default: true, persistence: 'memory'}); |
| 42 | + `] |
| 43 | + ] |
| 44 | + : [] |
4 | 45 |
|
5 | 46 | export default withMermaid(defineConfig({ |
6 | 47 | markdown: { |
@@ -45,6 +86,22 @@ export default withMermaid(defineConfig({ |
45 | 86 | head: [ |
46 | 87 | ['link', { rel: 'icon', href: '/logo/favicon-32x32.png' }], |
47 | 88 |
|
| 89 | + // Google Analytics with Consent Mode v2 |
| 90 | + ['script', { async: '', src: 'https://www.googletagmanager.com/gtag/js?id=G-JF828SKW90' }], |
| 91 | + ['script', {}, `window.dataLayer = window.dataLayer || []; |
| 92 | + function gtag(){dataLayer.push(arguments);} |
| 93 | + gtag('consent', 'default', { |
| 94 | + 'analytics_storage': 'denied', |
| 95 | + 'ad_storage': 'denied', |
| 96 | + 'ad_user_data': 'denied', |
| 97 | + 'ad_personalization': 'denied' |
| 98 | + }); |
| 99 | + gtag('js', new Date()); |
| 100 | + gtag('config', 'G-JF828SKW90');`], |
| 101 | + |
| 102 | + // PostHog Analytics (loaded only when VITE_POSTHOG_KEY is set) |
| 103 | + ...posthogHead, |
| 104 | + |
48 | 105 | // SEO: Basic meta tags |
49 | 106 | ['meta', { name: 'viewport', content: 'width=device-width, initial-scale=1.0' }], |
50 | 107 | ['meta', { name: 'author', content: 'Plane' }], |
@@ -619,9 +676,7 @@ export default withMermaid(defineConfig({ |
619 | 676 | { icon: 'linkedin', link: 'https://www.linkedin.com/company/planepowers/' } |
620 | 677 | ], |
621 | 678 |
|
622 | | - search: { |
623 | | - provider: 'local' |
624 | | - }, |
| 679 | + search: searchConfig, |
625 | 680 |
|
626 | 681 | editLink: { |
627 | 682 | pattern: 'https://github.com/makeplane/developer-docs/edit/main/:path' |
|
0 commit comments