test02/node_modules/@vuepress/plugin-git/lib/node/gitPlugin.js
罗佳鸿 6aa1ebe342
Some checks are pending
部署文档 / deploy-gh-pages (push) Waiting to run
first commit
2024-08-13 10:11:19 +08:00

35 lines
1.3 KiB
JavaScript

import { path } from 'vuepress/utils';
import { checkGitRepo, getContributors, getCreatedTime, getUpdatedTime, } from './utils/index.js';
export const gitPlugin = ({ createdTime, updatedTime, contributors } = {}) => (app) => {
const cwd = app.dir.source();
const isGitRepoValid = checkGitRepo(cwd);
return {
name: '@vuepress/plugin-git',
extendsPage: async (page) => {
page.data.git = {};
if (!isGitRepoValid || page.filePathRelative === null) {
return;
}
const filePaths = [
page.filePathRelative,
...(page.frontmatter.gitInclude ?? []).map((item) => path.join(page.filePathRelative, '..', item)),
];
if (createdTime !== false) {
page.data.git.createdTime = await getCreatedTime(filePaths, cwd);
}
if (updatedTime !== false) {
page.data.git.updatedTime = await getUpdatedTime(filePaths, cwd);
}
if (contributors !== false) {
page.data.git.contributors = await getContributors(filePaths, cwd);
}
},
// remove `gitInclude` from frontmatter
onInitialized: (app) => {
app.pages.forEach((page) => {
delete page.frontmatter.gitInclude;
});
},
};
};