This repository has been archived by the owner on Apr 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: prototype github mentions support as a component (#56)
- Loading branch information
1 parent
167a5f3
commit 1788b32
Showing
2 changed files
with
41 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const MENTIONS_PATTERN = /@([a-z0-9]+)/gi; | ||
|
||
export default function GithubMentionsAtom(props) { | ||
console.log('PROPS', props); | ||
const splitText = props.children.split(MENTIONS_PATTERN); | ||
const matches = props.children.match(MENTIONS_PATTERN); | ||
|
||
if (!splitText) return props.children; | ||
|
||
return ( | ||
<p> | ||
{splitText.reduce((arr, element) => { | ||
if (!element) return arr; | ||
|
||
if (matches.includes(`@${element}`)) { | ||
return [ | ||
...arr, | ||
(<a href={`https://github.com/${element}`} target="_blank" rel="noopener noreferrer"> | ||
@{element} | ||
</a>), | ||
]; | ||
} | ||
|
||
return [...arr, element]; | ||
}, [])} | ||
</p> | ||
); | ||
} | ||
|
||
GithubMentionsAtom.propTypes = { | ||
/** The content that has mentions that needs transformed to GitHub links */ | ||
children: PropTypes.string.isRequired, | ||
}; | ||
|
||
GithubMentionsAtom.defaultProps = { | ||
children: '', | ||
}; |
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 @@ | ||
export { default } from './github-mentions'; |