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

feature: implement mention link #65

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/atoms/avatar/elements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from 'styled-components/macro';
import styled from "styled-components/macro";

/**
* The avatar container "masks" the underlying image and makes it circular.
Expand Down
10 changes: 5 additions & 5 deletions src/atoms/highlight/highlight.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import decorator from './decorator';
import { HighlightContainer } from './elements';
import React from "react";
import PropTypes from "prop-types";
import decorator from "./decorator";
import { HighlightContainer } from "./elements";

export default function HighlightAtom(props) {
return (
Expand All @@ -15,5 +15,5 @@ HighlightAtom.propTypes = {
/** The text to highlight with the decorators. */
children: PropTypes.string.isRequired,
/** All decorators with their starting character. */
decorators: PropTypes.object.isRequired,
decorators: PropTypes.object.isRequired
};
7 changes: 4 additions & 3 deletions src/molecules/user/elements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from 'styled-components/macro';
import styled from "styled-components/macro";

/**
* The user container creates a vertical-directed flexbox.
Expand Down Expand Up @@ -58,7 +58,8 @@ export const UserDescription = styled.div`
/**
* The user description highlight styles a single word within this description.
*/
export const UserDescriptionHighlight = styled.strong`
export const UserDescriptionHighlight = styled.a`
color: #24292e;
font-weight: 600;
font-weight: 700;
text-decoration: none;
`;
44 changes: 24 additions & 20 deletions src/molecules/user/user.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import React from 'react';
import Avatar from 'src/atoms/avatar';
import Highlight from 'src/atoms/highlight';
import { propTypes, defaultProps } from './prop-type';
import UserMeta from './user-meta';
import React from "react";
import UserMeta from "./user-meta";
import Avatar from "src/atoms/avatar";
import Highlight from "src/atoms/highlight";
import { propTypes, defaultProps } from "./prop-type";
import { mentionLink } from "./util";
import {
UserContainer,
UserAvatar,
UserName,
UserDescription,
UserDescriptionHighlight,
} from './elements';
UserDescriptionHighlight
} from "./elements";

const decorators = {
'@': (segment, match, key) => <UserDescriptionHighlight key={key}>{segment}</UserDescriptionHighlight>,
'#': (segment, match, key) => <UserDescriptionHighlight key={key}>{match}</UserDescriptionHighlight>,
"@": (segment, match, key) => (
<UserDescriptionHighlight
key={key}
href={mentionLink(segment)}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I must add a bit more documentation to the highlighter atom I think. In the mentionLink you separate the special character @ from the text right? Luckily I thought about this use case when I built the highlighter, it's the match parameter 😄 But again, this is fully my fault for lack of documentation.


TL;DR; The match parameter is the value you can use here, without having to separate the character from text yourself 😄


The decorator is invoked with the segment, match and key. Take this text for example:

Something something @abc somehting @qwe

This will result in 2x method invocations with the @ decorator with the following arguments:

invocation segment match key
# 0 @abc abc 0
# 1 @qwe qwe 1

target="_blank"
rel="noopener noreferrer"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always forget the rel attributes on these things 😄 Thanks! I think the noopener is a safe one, but I'm not sure about the noreferrer. Do you have any thoughts in this one?

>
{segment}
</UserDescriptionHighlight>
),
"#": (segment, match, key) => (
<UserDescriptionHighlight key={key}>{match}</UserDescriptionHighlight>
)
};

export default function UserMolecule(props) {
Expand All @@ -23,19 +35,11 @@ export default function UserMolecule(props) {
<UserContainer>
<UserMeta {...props} />
<UserAvatar>
<Avatar
url={props.avatarUrl}
name={identifier}
title={identifier}
/>
<Avatar url={props.avatarUrl} name={identifier} title={identifier} />
</UserAvatar>
<UserName title={identifier}>
{props.name}
</UserName>
<UserName title={identifier}>{props.name}</UserName>
<UserDescription>
<Highlight decorators={decorators}>
{props.description}
</Highlight>
<Highlight decorators={decorators}>{props.description}</Highlight>
</UserDescription>
</UserContainer>
);
Expand Down
11 changes: 11 additions & 0 deletions src/molecules/user/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function mentionLink(name) {
let mention = name.split("");

mention.splice(0, 1);

let username = mention.join("");

const url = `https://github.com/${username}`;

return url;
}