Skip to content

Commit 8769e1b

Browse files
committed
chore: tweak
1 parent 82f8599 commit 8769e1b

13 files changed

+148
-82
lines changed

plugins/development/plugin-git/src/client/composables/useChangelog.ts

+36-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,23 @@ import type {
66
GitPluginFrontmatter,
77
GitPluginPageData,
88
} from '../../shared/index.js'
9+
import { gitOptions } from '../options.js'
10+
import { resolveRepoLink } from '../utils/index.js'
911

1012
export interface GitChangelogItem extends GitChangelogInfo {
1113
date: string
1214
}
1315

16+
const RE_ISSUE = /#(\d+)/g
17+
1418
export const useChangelog = (): ComputedRef<GitChangelogItem[]> => {
1519
const frontmatter = usePageFrontmatter<GitPluginFrontmatter>()
1620
const lang = usePageLang()
1721
const page = usePageData<GitPluginPageData>()
1822

23+
const { pattern = {}, provider } = gitOptions
24+
const repo = resolveRepoLink(gitOptions.repo, provider)
25+
1926
return computed(() => {
2027
if (frontmatter.value.changelog === false) return []
2128

@@ -24,9 +31,34 @@ export const useChangelog = (): ComputedRef<GitChangelogItem[]> => {
2431
})
2532

2633
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27-
return (page.value.git?.changelog ?? []).map((item) => ({
28-
date: formatter.format(item.time),
29-
...item,
30-
}))
34+
return (page.value.git?.changelog ?? []).map((item) => {
35+
const res: GitChangelogItem = {
36+
date: formatter.format(item.time),
37+
...item,
38+
}
39+
40+
if (pattern.issue && repo) {
41+
res.message = res.message.replace(
42+
RE_ISSUE,
43+
(matched, issue: string) => {
44+
const url = pattern
45+
.issue!.replace(':issue', issue)
46+
.replace(':repo', repo)
47+
return `<a href="${url}" target="_blank" rel="noopener noreferrer">${matched}</a>`
48+
},
49+
)
50+
}
51+
52+
if (pattern.commit && repo) {
53+
res.commitUrl = pattern.commit
54+
.replace(':hash', res.hash)
55+
.replace(':repo', repo)
56+
}
57+
58+
if (pattern.tag && repo && res.tag)
59+
res.tagUrl = pattern.tag.replace(':tag', res.tag).replace(':repo', repo)
60+
61+
return res
62+
})
3163
})
3264
}

plugins/development/plugin-git/src/client/composables/useGitLocales.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import type { GitLocaleData } from '../../shared/index.js'
55

66
declare const __GIT_LOCALES__: ExactLocaleConfig<GitLocaleData>
77

8-
export const locales =
9-
typeof __GIT_LOCALES__ === 'undefined' ? {} : __GIT_LOCALES__
8+
export const locales = __GIT_LOCALES__
109

1110
export const useGitLocaleConfig = (): ComputedRef<GitLocaleData> =>
1211
useLocaleConfig(locales)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { GitInjectOptions } from '../shared/index.js'
2+
3+
declare const __GIT_OPTIONS__: GitInjectOptions
4+
5+
export const gitOptions = __GIT_OPTIONS__
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './resolveRepoLink.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { isLinkHttp } from '@vuepress/helper/client'
2+
import type { KnownGitProvider } from '../../shared/index.js'
3+
4+
export const resolveRepoLink = (
5+
link?: string,
6+
provider?: KnownGitProvider | null,
7+
): string | undefined => {
8+
if (!link || isLinkHttp(link)) return link
9+
10+
if (provider === 'github') return `https://github.com/${link}`
11+
12+
if (provider === 'gitee') return `https://gitee.com/${link}`
13+
14+
return link
15+
}

plugins/development/plugin-git/src/node/gitPlugin.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import type { GitPluginOptions } from './options.js'
1111
import { prepareClientConfigFile } from './prepareClientConfigFile.js'
1212
import { resolveChangelog } from './resolveChangelog.js'
1313
import { resolveContributors } from './resolveContributors.js'
14-
import { checkGitRepo, getCommits, inferGitProvider } from './utils/index.js'
14+
import {
15+
checkGitRepo,
16+
getCommits,
17+
inferGitProvider,
18+
injectGitOptions,
19+
} from './utils/index.js'
1520

1621
export const gitPlugin =
1722
({
@@ -39,6 +44,7 @@ export const gitPlugin =
3944
default: gitLocaleInfo,
4045
config: locales,
4146
}),
47+
__GIT_OPTIONS__: injectGitOptions(gitProvider, changelog),
4248
},
4349

4450
extendsPage: async (
@@ -109,7 +115,6 @@ export const gitPlugin =
109115
app,
110116
commits,
111117
changelogOptions,
112-
gitProvider,
113118
contributorsOptions.info ?? [],
114119
)
115120
}

plugins/development/plugin-git/src/node/resolveChangelog.ts

+1-67
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,14 @@
11
import type { App } from 'vuepress'
22
import type { GitChangelogInfo } from '../shared/index.js'
33
import type { ChangelogOptions, ContributorInfo } from './options.js'
4-
import type { KnownGitProvider, MergedRawCommit } from './typings.js'
4+
import type { MergedRawCommit } from './typings.js'
55
import {
66
getContributorInfo,
77
getUserNameWithNoreplyEmail,
88
} from './utils/index.js'
99

10-
interface Pattern {
11-
issue?: string
12-
tag?: string
13-
commit?: string
14-
}
15-
16-
const RE_ISSUE = /#(\d+)/g
1710
const RE_CLEAN_REFS = /[()]/g
1811

19-
const patterns: Record<KnownGitProvider, Pattern> = {
20-
github: {
21-
issue: ':repo/issues/:issue',
22-
tag: ':repo/releases/tag/:tag',
23-
commit: ':repo/commit/:hash',
24-
},
25-
gitlab: {
26-
issue: ':repo/-/issues/:issue',
27-
tag: ':repo/-/releases/:tag',
28-
commit: ':repo/-/commit/:hash',
29-
},
30-
gitee: {
31-
issue: ':repo/issues/:issue',
32-
tag: ':repo/releases/tag/:tag',
33-
commit: ':repo/commit/:hash',
34-
},
35-
bitbucket: {
36-
issue: ':repo/issues/:issue',
37-
tag: ':repo/src/:hash',
38-
commit: ':repo/commits/:hash',
39-
},
40-
}
41-
42-
const getPattern = (
43-
{ commitUrlPattern, issueUrlPattern, tagUrlPattern }: ChangelogOptions,
44-
provider: KnownGitProvider | null,
45-
): Pattern => {
46-
const fallback = provider ? patterns[provider] : {}
47-
48-
return {
49-
commit: commitUrlPattern ?? fallback.commit,
50-
issue: issueUrlPattern ?? fallback.issue,
51-
tag: tagUrlPattern ?? fallback.tag,
52-
}
53-
}
54-
5512
const parseTagName = (refs: string): string | undefined => {
5613
if (!refs) return
5714

@@ -67,11 +24,8 @@ export const resolveChangelog = (
6724
app: App,
6825
commits: MergedRawCommit[],
6926
options: ChangelogOptions,
70-
gitProvider: KnownGitProvider | null,
7127
contributors: ContributorInfo[],
7228
): GitChangelogInfo[] => {
73-
const pattern = getPattern(options, gitProvider)
74-
const repo = options.repoUrl
7529
const result: GitChangelogInfo[] = []
7630

7731
const sliceCommits = options.maxCount
@@ -95,28 +49,8 @@ export const resolveChangelog = (
9549

9650
if (coAuthors.length) resolved.coAuthors = coAuthors
9751

98-
if (pattern.issue && repo) {
99-
resolved.message = resolved.message.replace(
100-
RE_ISSUE,
101-
(matched, issue: string) => {
102-
const url = pattern
103-
.issue!.replace(':issue', issue)
104-
.replace(':repo', repo)
105-
return `<a href="${url}" target="_blank" rel="noopener noreferrer">${matched}</a>`
106-
},
107-
)
108-
}
109-
110-
if (pattern.commit && repo)
111-
resolved.commitUrl = pattern.commit
112-
.replace(':hash', hash)
113-
.replace(':repo', repo)
114-
11552
if (tag) resolved.tag = tag
11653

117-
if (pattern.tag && repo && tag)
118-
resolved.tagUrl = pattern.tag.replace(':tag', tag).replace(':repo', repo)
119-
12054
result.push(resolved)
12155
}
12256

plugins/development/plugin-git/src/node/resolveContributors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { GitContributorInfo } from '../shared/index.js'
1+
import type { GitContributorInfo, KnownGitProvider } from '../shared/index.js'
22
import type { ContributorsOptions } from './options.js'
3-
import type { KnownGitProvider, MergedRawCommit } from './typings.js'
3+
import type { MergedRawCommit } from './typings.js'
44
import {
55
digestSHA256,
66
getContributorInfo,

plugins/development/plugin-git/src/node/typings.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import type { CoAuthorInfo } from '../shared/index.js'
2-
/**
3-
* Git provider
4-
*/
5-
export type KnownGitProvider = 'bitbucket' | 'gitee' | 'github' | 'gitlab'
62

73
export interface RawCommit {
84
/**

plugins/development/plugin-git/src/node/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './getCommits.js'
44
export * from './getContributorInfo.js'
55
export * from './getUserNameWithNoreplyEmail.js'
66
export * from './inferGitProvider.js'
7+
export * from './injectGitOptions.js'

plugins/development/plugin-git/src/node/utils/inferGitProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { execaCommandSync } from 'execa'
2-
import type { KnownGitProvider } from '../typings.js'
2+
import type { KnownGitProvider } from '../../shared/index.js'
33

44
export const getRemoteUrl = (cwd: string): string => {
55
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { isPlainObject } from 'vuepress/shared'
2+
import type {
3+
GitInjectOptions,
4+
GitUrlPattern,
5+
KnownGitProvider,
6+
} from '../../shared/index.js'
7+
import type { ChangelogOptions } from '../options.js'
8+
9+
const PATTERN_PRESET: Record<KnownGitProvider, GitUrlPattern> = {
10+
github: {
11+
issue: ':repo/issues/:issue',
12+
tag: ':repo/releases/tag/:tag',
13+
commit: ':repo/commit/:hash',
14+
},
15+
gitlab: {
16+
issue: ':repo/-/issues/:issue',
17+
tag: ':repo/-/releases/:tag',
18+
commit: ':repo/-/commit/:hash',
19+
},
20+
gitee: {
21+
issue: ':repo/issues/:issue',
22+
tag: ':repo/releases/tag/:tag',
23+
commit: ':repo/commit/:hash',
24+
},
25+
bitbucket: {
26+
issue: ':repo/issues/:issue',
27+
tag: ':repo/src/:hash',
28+
commit: ':repo/commits/:hash',
29+
},
30+
}
31+
32+
const getPattern = (
33+
{ commitUrlPattern, issueUrlPattern, tagUrlPattern }: ChangelogOptions,
34+
provider: KnownGitProvider | null,
35+
): GitUrlPattern => {
36+
const fallback = provider ? PATTERN_PRESET[provider] : {}
37+
38+
return {
39+
commit: commitUrlPattern ?? fallback.commit,
40+
issue: issueUrlPattern ?? fallback.issue,
41+
tag: tagUrlPattern ?? fallback.tag,
42+
}
43+
}
44+
45+
export const injectGitOptions = (
46+
provider: KnownGitProvider | null,
47+
changelog: ChangelogOptions | boolean,
48+
): GitInjectOptions => {
49+
const data: GitInjectOptions = {
50+
provider,
51+
}
52+
if (changelog) {
53+
const changelogOptions = isPlainObject(changelog) ? changelog : {}
54+
data.pattern = getPattern(changelogOptions, provider)
55+
data.repo = changelogOptions.repoUrl
56+
}
57+
58+
return data
59+
}

plugins/development/plugin-git/src/shared/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { PageFrontmatter } from 'vuepress/shared'
22

3+
/**
4+
* Git provider
5+
*/
6+
export type KnownGitProvider = 'bitbucket' | 'gitee' | 'github' | 'gitlab'
7+
38
/**
49
* Co-author information
510
*/
@@ -145,3 +150,17 @@ export interface GitLocaleData {
145150
*/
146151
latestUpdateAt: string
147152
}
153+
154+
/* @internal */
155+
export interface GitUrlPattern {
156+
issue?: string
157+
tag?: string
158+
commit?: string
159+
}
160+
161+
/* @internal */
162+
export interface GitInjectOptions {
163+
provider?: KnownGitProvider | null
164+
repo?: string
165+
pattern?: GitUrlPattern
166+
}

0 commit comments

Comments
 (0)