Skip to content
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

fix: improve last updated time feature #1036

Merged
merged 1 commit into from Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions v1/lib/core/DocsLayout.js
Expand Up @@ -41,9 +41,11 @@ class DocsLayout extends React.Component {
const id = metadata.localized_id;
const defaultTitle = metadata.title;
let DocComponent = Doc;

if (this.props.Doc) {
DocComponent = this.props.Doc;
}

let updateTime;
if (this.props.config.enableUpdateTime) {
const filepath = docs.getFilePath(metadata);
Expand Down Expand Up @@ -88,6 +90,15 @@ class DocsLayout extends React.Component {
version={metadata.version}
language={metadata.language}
/>
{this.props.config.enableUpdateTime &&
updateTime && (
<div className="docLastUpdateTimestamp">
<em>
<strong>Last updated: </strong>
{updateTime}
</em>
</div>
)}
<div className="docs-prevnext">
{metadata.previous_id && (
<a
Expand Down Expand Up @@ -123,13 +134,6 @@ class DocsLayout extends React.Component {
</a>
)}
</div>
{this.props.config.enableUpdateTime &&
updateTime && (
<p style={{fontSize: '12px', textAlign: 'right'}}>
<strong>Last updated: </strong>
{updateTime}
</p>
)}
</Container>
{hasOnPageNav && (
<nav className="onPageNav docOnPageNav">
Expand Down
64 changes: 36 additions & 28 deletions v1/lib/core/utils.js
Expand Up @@ -38,38 +38,46 @@ function idx(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;
function isTimestamp(str) {
return /^\d+$/.test(str);
}

// 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);
// Wrap in try/catch in case the shell commands fail (e.g. project doesn't use Git, etc).
try {
// 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;

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();
// 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 currentItemIsTimestamp = isTimestamp(item);
const isLastTwoItem = index + 2 >= arr.length;
const nextItemIsTimestamp = isTimestamp(arr[index + 1]);
return currentItemIsTimestamp && (isLastTwoItem || nextItemIsTimestamp);
});

if (timeSpan) {
const date = new Date(parseInt(timeSpan, 10) * 1000);
return date.toLocaleString();
}
} catch (error) {
console.error(error);
}

return null;
}

Expand Down
11 changes: 9 additions & 2 deletions v1/lib/static/css/main.css
Expand Up @@ -1500,7 +1500,7 @@ input::placeholder {

@media only screen and (min-width: 1024px) {
.docMainWrapper {
max-width:100% !important ;
max-width: 100% !important ;
margin: 0;
}

Expand Down Expand Up @@ -1535,6 +1535,14 @@ input::placeholder {
display: none;
}
}

.docLastUpdateTimestamp {
font-size: 13px;
font-style: italic;
margin: 20px 0;
text-align: right;
}

/* End of Docs Main */

/* Start of Docs Navigation */
Expand Down Expand Up @@ -2041,7 +2049,6 @@ input::placeholder {
}
}


@media only screen and (min-width: 1024px) {
.separateOnPageNav.sideNavVisible .navPusher .docMainContainer {
flex-basis: 784px;
Expand Down