Skip to content
This repository has been archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
feature: prototype github mentions support as a component (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsWendell authored and byCedric committed Dec 1, 2018
1 parent 167a5f3 commit 1788b32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/atoms/github-mentions/github-mentions.js
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: '',
};
1 change: 1 addition & 0 deletions src/atoms/github-mentions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './github-mentions';

0 comments on commit 1788b32

Please sign in to comment.