diff --git a/packages/vitepress-plugin-git-changelog/src/vite/git.ts b/packages/vitepress-plugin-git-changelog/src/vite/git.ts index 8f6a440..7397224 100644 --- a/packages/vitepress-plugin-git-changelog/src/vite/git.ts +++ b/packages/vitepress-plugin-git-changelog/src/vite/git.ts @@ -1,4 +1,3 @@ -import { posix, sep, win32 } from 'node:path' import { exec } from 'node:child_process' import { promisify } from 'node:util' @@ -14,6 +13,7 @@ import { type CommitToStringsHandler, type RewritePathsBy, digestStringAsSHA256, + generateCommitPathsRegExp, normalizeGitLogPath, parseGitLogRefsAsTags, returnOrResolvePromise, @@ -49,6 +49,7 @@ async function aggregateCommit( getReleaseTagsURL: CommitToStringsHandler, log: Commit, includeDirs: string[] = [], + includeExtensions: `.${string}`[] = [], optsRewritePaths?: Record, optsRewritePathsByPatterns?: RewritePathsBy, ) { @@ -77,11 +78,7 @@ async function aggregateCommit( new Set( files .filter((i) => { - // include is not set, it is /^.+\md$/ - // include is set, it is /^(${include.join('|')})\/.+\md$/ - // in another word, /^(includeItem1|includeItem2|includeItem3)\/.+\md$/ - const regexp = new RegExp(`^${includeDirs.length > 0 ? `(${includeDirs.join('|')})${sep === win32.sep ? win32.sep : `\\${posix.sep}`}` : ''}.+\\.md$`) - return !!i[1]?.match(regexp)?.[0] + return !!i[1]?.match(generateCommitPathsRegExp(includeDirs, includeExtensions))?.[0] }), ), ) @@ -110,6 +107,7 @@ async function aggregateCommits( getReleaseTagsURL: CommitToStringsHandler, commits: SimpleGitCommit, includeDirs: string[] = [], + includeExtensions: `.${string}`[] = [], rewritePaths?: Record, rewritePathsBy?: RewritePathsBy, ) { @@ -144,7 +142,7 @@ async function aggregateCommits( const processedCommits = await Promise.all( transformedCommits.map( async (commit) => { - return aggregateCommit(getReleaseTagURL, getReleaseTagsURL, commit, includeDirs, rewritePaths, rewritePathsBy) + return aggregateCommit(getReleaseTagURL, getReleaseTagsURL, commit, includeDirs, includeExtensions, rewritePaths, rewritePathsBy) }, ), ) @@ -157,6 +155,10 @@ export interface GitChangelogOptions { * When fetching git logs, what directories should be included? */ includeDirs?: string[] + /** + * When fetching git logs, what extensions should be included? + */ + includeExtensions?: `.${string}`[] /** * Your repository URL. * Yes, you can dynamically generate it. @@ -248,6 +250,7 @@ export function GitChangelog(options: GitChangelogOptions = {}): Plugin { maxGitLogCount, maxConcurrentProcesses, includeDirs = [], + includeExtensions = [], repoURL = 'https://github.com/example/example', getReleaseTagURL = defaultReleaseTagURLHandler, getReleaseTagsURL = defaultReleaseTagsURLHandler, @@ -313,6 +316,7 @@ export function GitChangelog(options: GitChangelogOptions = {}): Plugin { getReleaseTagsURL, gitLogsRaw.all, includeDirs, + includeExtensions, options.rewritePaths, options.rewritePathsBy, ) diff --git a/packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts b/packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts index 4825f8e..df8eda7 100644 --- a/packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts +++ b/packages/vitepress-plugin-git-changelog/src/vite/helpers.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from 'vitest' import type { Commit } from '../types' -import { parseGitLogRefsAsTags, rewritePathsByPatterns, rewritePathsByRewritingExtension } from './helpers' +import { generateCommitPathsRegExp, parseGitLogRefsAsTags, rewritePathsByPatterns, rewritePathsByRewritingExtension } from './helpers' describe('rewritePathsByRewritingExtension', () => { it('should rewrite paths', () => { @@ -74,3 +74,18 @@ describe('parseGitLogRefsAsTags', () => { expect(parseGitLogRefsAsTags('tag: v1.0.0, tag: v1.0.1')).toEqual(['v1.0.0', 'v1.0.1']) }) }) + +describe('generateCommitPathsRegExp', () => { + it('default', () => { + expect(generateCommitPathsRegExp([], [])).toEqual(/^.+.md$/) + }) + it('includeDirs', () => { + expect(generateCommitPathsRegExp(['docs', 'packages'], [])).toEqual(/^(docs|packages)\/.+.md$/) + }) + it('includeExtensions', () => { + expect(generateCommitPathsRegExp([], ['.md', '.ts'])).toEqual(/^.+(.md|.ts)$/) + }) + it('includeDirs and includeExtensions', () => { + expect(generateCommitPathsRegExp(['docs', 'packages'], ['.md', '.ts'])).toEqual(/^(docs|packages)\/.+(.md|.ts)$/) + }) +}) diff --git a/packages/vitepress-plugin-git-changelog/src/vite/helpers.ts b/packages/vitepress-plugin-git-changelog/src/vite/helpers.ts index 13b7b87..082d848 100644 --- a/packages/vitepress-plugin-git-changelog/src/vite/helpers.ts +++ b/packages/vitepress-plugin-git-changelog/src/vite/helpers.ts @@ -1,4 +1,4 @@ -import { extname, relative } from 'node:path' +import { extname, posix, relative, sep, win32 } from 'node:path' import { subtle } from 'uncrypto' import { normalizePath } from 'vite' import type { Commit } from '../types' @@ -207,3 +207,16 @@ export function parseGitLogRefsAsTags(refs?: string): string[] { return tags.map(tag => tag.replace('tag: ', '').trim()) } + +/** + * Generate RegExp for filtering out paths of commits. + * + * It follows the rules that: + * - includes is not set, it is /^.+.md$/ + * - includeDirs is set, it is /^(${includeDirs.join('|')})\/.+.md$/ + * - includeExtensions is set, it is /^.+(${includeExtensions.join('|')})$/ + * - in another word, /^(includeDir1|includeDir2)\/.+(includeExtension1|includeExtensions2)$/ + */ +export function generateCommitPathsRegExp(includeDirs: string[], includeExtensions: `.${string}`[]): RegExp { + return new RegExp(`^${includeDirs.length > 0 ? `(${includeDirs.join('|')})${sep === win32.sep ? win32.sep : `\\${posix.sep}`}` : ''}.+${includeExtensions.length > 0 ? `(${includeExtensions.join('|')})` : '.md'}$`) +}