Skip to content

Commit

Permalink
feat(git-changelog): added new contributors section (#38)
Browse files Browse the repository at this point in the history
Signed-off-by: Neko Ayaka <[email protected]>
  • Loading branch information
nekomeowww authored Dec 30, 2023
1 parent 3f298f4 commit efa4025
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 4 deletions.
22 changes: 21 additions & 1 deletion docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import {
NolebaseHighlightTargetedHeading,
} from '@nolebase/vitepress-plugin-highlight-targeted-heading'

import { NolebaseGitChangelogPlugin } from '@nolebase/vitepress-plugin-git-changelog/client'
import {
InjectionKey as NolebaseGitChangelogInjectionKey,
NolebaseGitChangelogPlugin,
} from '@nolebase/vitepress-plugin-git-changelog/client'

import 'virtual:uno.css'

Expand Down Expand Up @@ -60,6 +63,23 @@ export const Theme: ThemeConfig = {
defaultToggle: true,
},
})

app.provide(NolebaseGitChangelogInjectionKey, {
mapContributors: [
{
name: 'Neko',
avatar: 'https://github.com/nekomeowww.png',
nameAliases: ['Neko Ayaka', 'Ayaka Neko'],
emailAliases: ['[email protected]'],
},
{
name: 'Rizumu',
avatar: 'https://github.com/LittleSound.png',
nameAliases: ['Rizumu Ayaka', 'Ayaka Rizumu'],
emailAliases: ['[email protected]'],
},
],
})
},
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vitepress-plugin-git-changelog/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nolebase/vitepress-plugin-git-changelog",
"type": "module",
"version": "1.2.0",
"version": "1.3.0",
"description": "A VitePress plugin that adds a changelog fetched from git to your documentation.",
"author": {
"name": "Nólëbase",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<script setup lang="ts">
import { computed, inject } from 'vue'
import Changelog from 'virtual:nolebase-git-changelog'
import { useRawPath } from '../composables/path'
import { useCommits } from '../composables/commits'
import { useI18n } from '../composables/i18n'
import { InjectionKey } from '../constants'
interface ContributorInfo {
name: string
commitsCount: number
avatarUrl?: string
url?: string
}
const options = inject(InjectionKey, {})
const { t } = useI18n()
const rawPath = useRawPath()
const commits = useCommits(Changelog.commits, rawPath)
const contributors = computed<ContributorInfo[]>(() => {
const map: Record<string, ContributorInfo> = {}
commits.value.forEach((c) => {
const targetCreatorByName = options.mapContributors?.find(item => item.nameAliases && Array.isArray(item.nameAliases) && item.nameAliases.includes(c.author_name))
const targetCreatorByEmail = options.mapContributors?.find(item => item.emailAliases && Array.isArray(item.emailAliases) && item.emailAliases.includes(c.author_email))
let name = ''
let avatar = ''
let url: string | undefined
if (targetCreatorByName) {
name = targetCreatorByName.name || c.author_name
avatar = targetCreatorByName.avatar || `https://gravatar.com/avatar/${c.author_avatar}?d=retro`
const foundGitHubLink = targetCreatorByName.links?.find(item => item.type === 'github')
if (foundGitHubLink)
url = foundGitHubLink.link
}
else if (targetCreatorByEmail) {
name = targetCreatorByEmail.name || c.author_name
avatar = targetCreatorByEmail.avatar || `https://gravatar.com/avatar/${c.author_avatar}?d=retro`
const foundGitHubLink = targetCreatorByEmail.links?.find(item => item.type === 'github')
if (foundGitHubLink)
url = foundGitHubLink.link
}
else {
name = c.author_name
avatar = `https://gravatar.com/avatar/${c.author_avatar}?d=retro`
}
if (!map[name]) {
map[name] = {
name,
commitsCount: 0,
avatarUrl: avatar,
url,
}
}
map[name].commitsCount++
})
return Object.values(map).sort((a, b) => b.commitsCount - a.commitsCount)
})
</script>

<template>
<div class="flex flex-wrap gap-4 pt-2">
<em
v-if="!contributors.length"
>
{{ t('noContributors') }}
</em>
<template
v-else
>
<template
v-for="c of contributors"
:key="c.name"
>
<a
v-if="typeof c.url !== 'undefined'"
:href="c.url"
>
<div class="flex items-center gap-2">
<img :src="c.avatarUrl" class="h-8 w-8 rounded-full">
{{ c.name }}
</div>
</a>
<div
v-else
class="flex items-center gap-2"
>
<img :src="c.avatarUrl" class="h-8 w-8 rounded-full">
{{ c.name }}
</div>
</template>
</template>
</div>
</template>
3 changes: 3 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import type { Changelog, Commit } from '../types'
import type { Locale, Options } from './types'
import { InjectionKey } from './constants'
import NolebaseGitChangelog from './components/Changelog.vue'
import NolebaseGitContributors from './components/Contributors.vue'

import 'virtual:uno.css'

const components = {
NolebaseGitChangelog,
NolebaseGitContributors,
}

export const NolebaseGitChangelogPlugin: Plugin = {
Expand All @@ -23,6 +25,7 @@ export const NolebaseGitChangelogPlugin: Plugin = {

export {
NolebaseGitChangelog,
NolebaseGitContributors,
InjectionKey,
}

Expand Down
2 changes: 2 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/client/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { Locale } from './types'

export const defaultEnLocale: Locale = {
noLogs: 'No recent changes',
noContributors: 'No contributors',
lastEdited: 'Last edited {{daysAgo}}',
viewFullHistory: 'View full history',
committedOn: ' on {{date}}',
}

export const defaultZhCNLocale: Locale = {
noLogs: '暂无最近变更历史',
noContributors: '暂无相关贡献者',
lastEdited: '最后编辑于 {{daysAgo}}',
viewFullHistory: '查看完整历史',
committedOn: ' 于 {{date}}',
Expand Down
4 changes: 4 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Contributor } from '../types'

export interface Locale {
noLogs?: string
noContributors?: string
lastEdited?: string
viewFullHistory?: string
committedOn?: string
Expand Down Expand Up @@ -36,4 +39,5 @@ export interface Options {
* ```
*/
locales?: Record<string, Locale>
mapContributors?: Contributor[]
}
15 changes: 15 additions & 0 deletions packages/vitepress-plugin-git-changelog/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@ export interface Commit {
export interface Changelog {
commits: Commit[]
}

export interface SocialEntry {
type: 'github' | 'twitter' | 'email' | string
icon: string
link: string
}

export interface Contributor {
name?: string
avatar?: string
email?: string
links?: SocialEntry[]
nameAliases?: string[]
emailAliases?: string[]
}
28 changes: 26 additions & 2 deletions packages/vitepress-plugin-git-changelog/src/vite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,21 @@ export function GitChangelog(options: {

export function GitChangelogMarkdownSection(options?: {
getChangelogTitle?: (code: string, id: string) => string
getContributorsTitle?: (code: string, id: string) => string
excludes?: string[]
exclude?: (id: string) => boolean
sections?: {
disableChangelog?: boolean
disableContributors?: boolean
}
}): Plugin {
const {
getChangelogTitle = () => {
return 'Changelog'
},
getContributorsTitle = () => {
return 'Contributors'
},
excludes = ['index.md'],
exclude = () => false,
} = options ?? {}
Expand All @@ -154,11 +162,27 @@ export function GitChangelogMarkdownSection(options?: {
if (exclude(id))
return null

return `${code}
const contributorsSection = options?.sections?.disableContributors
? ''
: `
## ${getContributorsTitle(code, id)}
<NolebaseGitContributors />
`

const changelogSection = options?.sections?.disableChangelog
? ''
: `
## ${getChangelogTitle(code, id)}
<NolebaseGitChangelog />`
<NolebaseGitChangelog />
`

return `${code}${contributorsSection}${changelogSection}`
},
}
}

0 comments on commit efa4025

Please sign in to comment.