Skip to content

Commit aa9de96

Browse files
committed
feat: refactor VitePress plugin with top-level repo config
- Add repoURL as top-level plugin option - Add contentPath option for docs directory - Auto-configure GitChangelog from repoURL - Enhance useSourceCode with plugin config - Add useNimiqConfig composable with SSR safety - Update docs with new configuration examples
1 parent 6de2ba9 commit aa9de96

File tree

5 files changed

+132
-12
lines changed

5 files changed

+132
-12
lines changed

docs/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default defineConfig(async () => ({
2323
Inspect(),
2424
UnoCSS(),
2525
NimiqVitepressVitePlugin({
26-
gitChangelog: { repoURL: 'https://github.com/onmax/nimiq-ui' },
26+
repoURL: 'https://github.com/onmax/nimiq-ui',
27+
contentPath: 'docs',
2728
}),
2829
],
2930
}))

docs/vitepress-theme/index.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,55 @@ export default defineNimiqThemeConfig({
119119

120120
This will install the layout and will register the Nimiq Components globally.
121121

122+
### Configure the Vite plugin
123+
124+
The theme provides a Vite plugin that adds Git changelog functionality and source code features:
125+
126+
::: code-group
127+
128+
```ts [vite.config.ts]
129+
import { NimiqVitepressVitePlugin } from 'nimiq-vitepress-theme/vite'
130+
import { defineConfig } from 'vite'
131+
132+
export default defineConfig(() => {
133+
return {
134+
plugins: [
135+
NimiqVitepressVitePlugin({
136+
// GitHub repository URL (required for source code features)
137+
repoURL: 'https://github.com/your-org/your-repo',
138+
139+
// Content directory path relative to repository root
140+
// Use this if your docs are not in the repository root
141+
contentPath: 'docs', // Optional, defaults to ''
142+
143+
// Git changelog configuration (optional)
144+
// If not provided, will use repoURL as default
145+
// Set to false to disable changelog
146+
gitChangelog: {
147+
repoURL: 'https://github.com/your-org/your-repo' // Can be different from main repoURL
148+
}
149+
})
150+
]
151+
}
152+
})
153+
```
154+
155+
:::
156+
157+
#### Plugin Options
158+
159+
| Option | Type | Default | Description |
160+
| -------------- | ----------------- | -------------- | -------------------------------------------------------------------------------- |
161+
| `repoURL` | `string` | - | GitHub repository URL used for source code links and as default for GitChangelog |
162+
| `contentPath` | `string` | `''` | Directory path where documentation files are located relative to repository root |
163+
| `gitChangelog` | `object \| false` | Uses `repoURL` | Git changelog configuration or `false` to disable |
164+
165+
The plugin automatically:
166+
167+
- Configures Git changelog functionality using the provided repository URL
168+
- Enables source code viewing and copying features
169+
- Constructs proper URLs for "View Source" and "Edit Page" links
170+
122171
### Register the theme as internal dependency
123172

124173
This step is optional and only needed if you want to use the Vue components from `nimiq-vitepress-theme` in your project.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
declare const __NIMIQ_VITEPRESS_CONFIG__: {
2+
repoURL?: string
3+
contentPath?: string
4+
} | undefined
5+
6+
export interface NimiqVitepressConfig {
7+
repoURL?: string
8+
contentPath?: string
9+
}
10+
11+
export function useNimiqConfig(): NimiqVitepressConfig {
12+
// Check if running in browser environment and config is available
13+
if (typeof window !== 'undefined' && typeof __NIMIQ_VITEPRESS_CONFIG__ !== 'undefined') {
14+
return __NIMIQ_VITEPRESS_CONFIG__
15+
}
16+
17+
// For SSR or when config is not available, return fallback
18+
return {
19+
repoURL: undefined,
20+
contentPath: '',
21+
}
22+
}

packages/nimiq-vitepress-theme/src/composables/useSourceCode.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { useData } from 'vitepress'
44
import { computed, ref } from 'vue'
55
import { toast } from 'vue-sonner'
66
import { useChangelog } from './useChangelog'
7+
import { useNimiqConfig } from './useNimiqConfig'
78

89
export function useSourceCode() {
910
const { page, frontmatter } = useData()
1011
const { repoURL } = useChangelog()
12+
const nimiqConfig = useNimiqConfig()
1113

1214
// Use a safer approach for SSR compatibility
1315
const clipboardResult = typeof window !== 'undefined'
@@ -56,17 +58,28 @@ export function useSourceCode() {
5658
return config.markdownLink || config.viewMarkdown || config.chatgpt || config.claude
5759
})
5860

61+
// Get the effective repo URL, prioritizing plugin config over changelog
62+
const effectiveRepoURL = computed(() => {
63+
return nimiqConfig.repoURL || repoURL.value
64+
})
65+
5966
const getRepoFilePath = computed(() => {
6067
const pathPrefix = frontmatter.value.sourceCodePathPrefix
6168

69+
// If explicit path prefix is set in frontmatter, use it
6270
if (pathPrefix !== undefined) {
6371
return pathPrefix ? join(pathPrefix, page.value.filePath) : page.value.filePath
6472
}
6573

66-
// Detect monorepo patterns to add docs/ prefix
67-
if (repoURL.value && (
68-
repoURL.value.includes('/nimiq-ui')
69-
|| repoURL.value.includes('/ui')
74+
// Use contentPath from plugin config if available
75+
if (nimiqConfig.contentPath) {
76+
return join(nimiqConfig.contentPath, page.value.filePath)
77+
}
78+
79+
// Fallback to legacy monorepo detection logic
80+
if (effectiveRepoURL.value && (
81+
effectiveRepoURL.value.includes('/nimiq-ui')
82+
|| effectiveRepoURL.value.includes('/ui')
7083
|| page.value.filePath.includes('docs/') === false
7184
)) {
7285
return join('docs', page.value.filePath)
@@ -76,16 +89,16 @@ export function useSourceCode() {
7689
})
7790

7891
const editUrl = computed(() => {
79-
if (!repoURL.value)
92+
if (!effectiveRepoURL.value)
8093
return ''
81-
return `${repoURL.value.replace(/\/$/, '')}/${getRepoFilePath.value}`
94+
return `${effectiveRepoURL.value.replace(/\/$/, '')}/${getRepoFilePath.value}`
8295
})
8396

8497
const sourceCodeUrl = computed(() => {
85-
if (!repoURL.value)
98+
if (!effectiveRepoURL.value)
8699
return ''
87100

88-
return `${repoURL.value.replace(/\/$/, '')}/blob/main/${getRepoFilePath.value}`
101+
return `${effectiveRepoURL.value.replace(/\/$/, '')}/blob/main/${getRepoFilePath.value}`
89102
})
90103

91104
const sourceCodeLabel = computed(() => {

packages/nimiq-vitepress-theme/src/vite.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,48 @@ import { groupIconVitePlugin } from './code-groups/vite'
55
type GitChangelogOptions = Parameters<typeof GitChangelog>[0]
66

77
export interface NimiqVitepressVitePluginOptions {
8-
gitChangelog: GitChangelogOptions | false
8+
/**
9+
* GitHub repository URL (e.g., 'https://github.com/owner/repo')
10+
* Used as default for GitChangelog and for constructing source code URLs
11+
*/
12+
repoURL?: string
13+
14+
/**
15+
* Content directory path relative to repository root
16+
* Specifies where documentation files are located
17+
* @default '' (repository root)
18+
*/
19+
contentPath?: string
20+
21+
/**
22+
* Git changelog configuration
23+
* If not provided, will use repoURL as default
24+
* Set to false to disable changelog
25+
*/
26+
gitChangelog?: GitChangelogOptions | false
927
}
1028

11-
export async function NimiqVitepressVitePlugin({ gitChangelog }: NimiqVitepressVitePluginOptions): Promise<Plugin[]> {
29+
export async function NimiqVitepressVitePlugin({
30+
repoURL,
31+
contentPath = '',
32+
gitChangelog,
33+
}: NimiqVitepressVitePluginOptions): Promise<Plugin[]> {
1234
const { resolveId, configureServer, load, transform } = groupIconVitePlugin()
1335

1436
const externalPlugins: Plugin[] = []
1537

38+
// Configure GitChangelog with repoURL as default
1639
if (gitChangelog !== false) {
17-
externalPlugins.push(GitChangelog(gitChangelog))
40+
const changelogConfig = gitChangelog || (repoURL ? { repoURL } : {})
41+
if (Object.keys(changelogConfig).length > 0) {
42+
externalPlugins.push(GitChangelog(changelogConfig))
43+
}
44+
}
45+
46+
// Store configuration in Vite config for use in composables
47+
const nimiqConfig = {
48+
repoURL,
49+
contentPath,
1850
}
1951

2052
return [
@@ -26,6 +58,9 @@ export async function NimiqVitepressVitePlugin({ gitChangelog }: NimiqVitepressV
2658
load,
2759
transform,
2860
config: () => ({
61+
define: {
62+
__NIMIQ_VITEPRESS_CONFIG__: JSON.stringify(nimiqConfig),
63+
},
2964
optimizeDeps: {
3065
exclude: [
3166
'nimiq-vitepress-theme',

0 commit comments

Comments
 (0)