mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 18:58:36 +02:00
* Fix last updated time misleading, only show when file content change or otherwise when it is first created Fix #1015 * Fix prettier * - Simplify regex - Uses shelljs instead of cross-spawn - Make logic clearer * Add test when repositories is moved * Use shell.exec mock I initially try to mock the whole shelljs. But it returns error shell.exec is not a function when i try to provide the mockResolvedValue I think it is because of the inner code of shelljs who run a forEach to require each of its method which make it a promise. I tried moving the jest.mock inside beforeAll and also adding babel-dynamic-import but it did not solve the problem. In the end, I decided to just mock shelljs.exec since it is the only function used anyway
83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
/**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
const shell = require('shelljs');
|
|
|
|
const TRUNCATE_MARKER = /<!--\s*truncate\s*-->/;
|
|
|
|
function blogPostHasTruncateMarker(content) {
|
|
return TRUNCATE_MARKER.test(content);
|
|
}
|
|
|
|
function extractBlogPostBeforeTruncate(content) {
|
|
return content.split(TRUNCATE_MARKER)[0];
|
|
}
|
|
|
|
function removeExtension(pathStr) {
|
|
return pathStr.replace(/\.[^/.]+$/, '');
|
|
}
|
|
|
|
function getPath(pathStr, cleanUrl = false) {
|
|
if (!pathStr || !cleanUrl || !pathStr.endsWith('.html')) {
|
|
return pathStr;
|
|
}
|
|
return pathStr.endsWith('/index.html')
|
|
? pathStr.replace(/index\.html$/, '')
|
|
: removeExtension(pathStr);
|
|
}
|
|
|
|
function idx(target, keyPaths) {
|
|
return (
|
|
target &&
|
|
(Array.isArray(keyPaths)
|
|
? keyPaths.reduce((obj, key) => obj && obj[key], target)
|
|
: target[keyPaths])
|
|
);
|
|
}
|
|
|
|
function isNormalInteger(str) {
|
|
return /^\d+$/.test(str);
|
|
}
|
|
|
|
function getGitLastUpdated(filepath) {
|
|
// To differentiate between content change and file renaming / moving, use --summary
|
|
// To follow the file history until before it is moved (when we create new version), use
|
|
// --follow
|
|
const silentState = shell.config.silent; // save old silent state
|
|
shell.config.silent = true;
|
|
const result = shell
|
|
.exec(`git log --follow --summary --format=%ct ${filepath}`)
|
|
.stdout.trim();
|
|
shell.config.silent = silentState;
|
|
|
|
// Format the log results to be ['1234567', 'rename ...', '1234566', 'move ...', '1234565', '1234564']
|
|
const records = result
|
|
.toString('utf-8')
|
|
.replace(/\n\s*\n/g, '\n')
|
|
.split('\n')
|
|
.filter(String);
|
|
|
|
const timeSpan = records.find((item, index, arr) => {
|
|
const isTimestamp = isNormalInteger(item);
|
|
const isLastTwoItem = index + 2 >= arr.length;
|
|
const nextItemIsTimestamp = isNormalInteger(arr[index + 1]);
|
|
return isTimestamp && (isLastTwoItem || nextItemIsTimestamp);
|
|
});
|
|
if (timeSpan) {
|
|
const date = new Date(parseInt(timeSpan, 10) * 1000);
|
|
return date.toLocaleString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = {
|
|
blogPostHasTruncateMarker,
|
|
extractBlogPostBeforeTruncate,
|
|
getGitLastUpdated,
|
|
getPath,
|
|
removeExtension,
|
|
idx,
|
|
};
|