Skip to content

Git commit feature-- which adds commit hash at the footer of doc page. #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ const config = {
},
};
},
[require.resolve('./src/plugins/git-commit-info'), {}],
],
};

Expand Down
29 changes: 6 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@node-rs/jieba": "^1.10.3",
"@svgr/webpack": "^6.3.1",
"autoprefixer": "^10.4.14",
"caniuse-lite": "^1.0.30001726",
"classnames": "^2.2.6",
"clsx": "^1.1.1",
"file-loader": "^6.2.0",
Expand Down
32 changes: 32 additions & 0 deletions src/plugins/git-commit-info/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { exec } = require('child_process');

function getLastCommitHashAsync(filePath) {
return new Promise((resolve) => {
exec(`git log -n 1 --pretty=format:%h -- "${filePath}"`, { encoding: 'utf8' }, (err, stdout) => {
if (err) return resolve(null);
resolve(stdout.trim());
});
});
}

console.log('[git-commit-info] Plugin loaded');

module.exports = function (context, options) {
return {
name: 'docusaurus-plugin-git-commit-info',
async extendMarkdownPageData(mdData) {
console.log('[git-commit-info] mdData:', mdData);
console.log('[git-commit-info] filePath:', mdData.filePath);
if (mdData.filePath) {
const commitHash = await getLastCommitHashAsync(mdData.filePath);
console.log('[git-commit-info] commitHash for', mdData.filePath, ':', commitHash);
if (commitHash) {
mdData.frontMatter.lastUpdatedCommit = commitHash;
mdData.metadata = mdData.metadata || {};
mdData.metadata.lastUpdatedCommit = commitHash;
}
}
return mdData;
},
};
};
27 changes: 27 additions & 0 deletions src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import OriginalDocItem from '@theme-original/DocItem';
import { useDoc } from '@docusaurus/theme-common';

export default function DocItem(props) {
const { metadata } = useDoc();
const commit = metadata?.lastUpdatedCommit;

return (
<>
<OriginalDocItem {...props} />
{commit && (
<div style={{
marginTop: '2em',
padding: '0.75em 1em',
background: '#f6f8fa',
border: '1px solid #e1e4e8',
borderRadius: '6px',
fontSize: '0.95em',
color: '#555',
}}>
<strong>Last updated commit:</strong> <code>{commit}</code>
</div>
)}
</>
);
}
Loading