mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 03:37:48 +02:00
* Adding last updated time for docs * Making file path general and other suggested changes * Checking if time returned is null due to absence of git or some other issue * Adding option to enable/disable update time feature and test-doc * Adding simple unit tests for getGitUpdateTime() * nits & rewrote failing test * consistent test naming * Adding optional updateEnableTime in documentation * package-lock & yarn.lock
54 lines
1.3 KiB
JavaScript
54 lines
1.3 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 spawn = require('cross-spawn');
|
|
|
|
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, path) {
|
|
return path.reduce((obj, key) => obj && obj[key], target);
|
|
}
|
|
|
|
function getGitLastUpdated(filepath) {
|
|
const timeSpan = spawn
|
|
.sync('git', ['log', '-1', '--format=%ct', filepath])
|
|
.stdout.toString('utf-8');
|
|
if (timeSpan) {
|
|
const date = new Date(parseInt(timeSpan, 10) * 1000);
|
|
return date.toLocaleString();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = {
|
|
blogPostHasTruncateMarker,
|
|
extractBlogPostBeforeTruncate,
|
|
getGitLastUpdated,
|
|
getPath,
|
|
removeExtension,
|
|
idx,
|
|
};
|