Skip to content

Commit 751c84d

Browse files
committed
Implement commit hash feature for doc footers, but extendMarkdownPageData is not executing, so feature is not working as intended
Signed-off-by: devesh <chouhanlim2003@gmail.com>
1 parent 6d6959f commit 751c84d

File tree

5 files changed

+67
-23
lines changed

5 files changed

+67
-23
lines changed

docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ const config = {
329329
},
330330
};
331331
},
332+
[require.resolve('./src/plugins/git-commit-info'), {}],
332333
],
333334
};
334335

package-lock.json

Lines changed: 6 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@node-rs/jieba": "^1.10.3",
2626
"@svgr/webpack": "^6.3.1",
2727
"autoprefixer": "^10.4.14",
28+
"caniuse-lite": "^1.0.30001726",
2829
"classnames": "^2.2.6",
2930
"clsx": "^1.1.1",
3031
"file-loader": "^6.2.0",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { exec } = require('child_process');
2+
3+
function getLastCommitHashAsync(filePath) {
4+
return new Promise((resolve) => {
5+
exec(`git log -n 1 --pretty=format:%h -- "${filePath}"`, { encoding: 'utf8' }, (err, stdout) => {
6+
if (err) return resolve(null);
7+
resolve(stdout.trim());
8+
});
9+
});
10+
}
11+
12+
console.log('[git-commit-info] Plugin loaded');
13+
14+
module.exports = function (context, options) {
15+
return {
16+
name: 'docusaurus-plugin-git-commit-info',
17+
async extendMarkdownPageData(mdData) {
18+
console.log('[git-commit-info] mdData:', mdData);
19+
console.log('[git-commit-info] filePath:', mdData.filePath);
20+
if (mdData.filePath) {
21+
const commitHash = await getLastCommitHashAsync(mdData.filePath);
22+
console.log('[git-commit-info] commitHash for', mdData.filePath, ':', commitHash);
23+
if (commitHash) {
24+
mdData.frontMatter.lastUpdatedCommit = commitHash;
25+
mdData.metadata = mdData.metadata || {};
26+
mdData.metadata.lastUpdatedCommit = commitHash;
27+
}
28+
}
29+
return mdData;
30+
},
31+
};
32+
};

src/theme/DocItem/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import OriginalDocItem from '@theme-original/DocItem';
3+
import { useDoc } from '@docusaurus/theme-common';
4+
5+
export default function DocItem(props) {
6+
const { metadata } = useDoc();
7+
const commit = metadata?.lastUpdatedCommit;
8+
9+
return (
10+
<>
11+
<OriginalDocItem {...props} />
12+
{commit && (
13+
<div style={{
14+
marginTop: '2em',
15+
padding: '0.75em 1em',
16+
background: '#f6f8fa',
17+
border: '1px solid #e1e4e8',
18+
borderRadius: '6px',
19+
fontSize: '0.95em',
20+
color: '#555',
21+
}}>
22+
<strong>Last updated commit:</strong> <code>{commit}</code>
23+
</div>
24+
)}
25+
</>
26+
);
27+
}

0 commit comments

Comments
 (0)