-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ui: Markdown timestamp support (#3558)
* add timestamp support to markdown * support unixstamp format * stamp -> date
- Loading branch information
1 parent
373d073
commit d3c0d89
Showing
5 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
import { | ||
FindAndReplaceTuple, | ||
findAndReplace, | ||
} from 'mdast-util-find-and-replace' | ||
import { Time } from './Time' | ||
import CopyText from './CopyText' | ||
|
||
type Node = { | ||
type: 'element' | ||
value: React.ReactNode | ||
} | ||
|
||
// 2006-01-02T15:04:05.999999999Z07:00 | ||
const isoTimestampRegex = | ||
/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(\.\d+)?([+-][0-2]\d:[0-5]\d|Z)/g | ||
function fromISO(iso: string): Node { | ||
return { | ||
type: 'element', | ||
value: <CopyText noTypography title={<Time time={iso} />} value={iso} />, | ||
} | ||
} | ||
|
||
// Mon Jan _2 15:04:05 MST 2006 | ||
const unixDateRegex = /\w+\s+\w+\s+\d+\s+\d{2}:\d{2}:\d{2}\s\w+\s\d{4}/g | ||
function fromUnixDate(unix: string): Node { | ||
return fromISO(new Date(unix).toISOString()) | ||
} | ||
|
||
// mdast types are wrong, so we have to cast to unknown and then to the correct | ||
// type. | ||
const isoTuple = [isoTimestampRegex, fromISO] as unknown as FindAndReplaceTuple | ||
const unixTuple = [ | ||
unixDateRegex, | ||
fromUnixDate, | ||
] as unknown as FindAndReplaceTuple | ||
|
||
export default function timestampSupport() { | ||
return function (tree: Parameters<typeof findAndReplace>[0]) { | ||
findAndReplace(tree, [isoTuple, unixTuple]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters