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: add highlights to user description (#55)
* feature: add highlight atom with customisable decorators * feature: implement highlight in user description for hashtags and mentions * fix: add support for hashtags in github mentions * refactor: rename github mentions to keywords This matches the purpose a bit more, since its being used for page keywords.
- Loading branch information
Showing
8 changed files
with
105 additions
and
8 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,51 @@ | ||
/** | ||
* Create a decorator pattern based on starting characters. | ||
* | ||
* @param {string[]} | ||
* @return {RegExp} | ||
*/ | ||
export const createPattern = (chars) => ( | ||
new RegExp(`([${chars.join('')}][a-z0-9-_]+)`, 'gi') | ||
); | ||
|
||
/** | ||
* Create a list of segments relevant to the decorators. | ||
* | ||
* @param {string} text | ||
* @param {Object} decorators | ||
* @return {string[]} | ||
*/ | ||
export const createSegments = (text, decorators) => ( | ||
text.split(createPattern(Object.keys(decorators))) | ||
); | ||
|
||
/** | ||
* Apply one of the decorators to a single segment. | ||
* It will invoke a decorator with the matched segment, inner text, and an optional key. | ||
* | ||
* @param {string} segment | ||
* @param {Object} decorators | ||
* @param {string|number} [key] | ||
* @return {string|ReactNode} | ||
*/ | ||
export const applyDecorator = (segment, decorators, key = undefined) => { | ||
const decorator = decorators[segment.charAt(0)]; | ||
|
||
return decorator | ||
? decorator(segment, segment.substr(1), key) | ||
: segment; | ||
}; | ||
|
||
/** | ||
* Decorate a string and return a list of text or components. | ||
* These can be rendered directly with React. | ||
* | ||
* @param {string} text | ||
* @param {Object} decorators | ||
* @return {(string|ReactNode)[]} | ||
*/ | ||
export default (text, decorators) => ( | ||
createSegments(text, decorators).map( | ||
(segment, key) => applyDecorator(segment, decorators, key) | ||
) | ||
); |
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,9 @@ | ||
import styled from 'styled-components'; | ||
|
||
/** | ||
* The highlight container groups the content using a paragraph. | ||
* It is used to mark the content as readable text only. | ||
*/ | ||
export const HighlightContainer = styled.p` | ||
margin: 0; | ||
`; |
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,19 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import decorator from './decorator'; | ||
import { HighlightContainer } from './elements'; | ||
|
||
export default function HighlightAtom(props) { | ||
return ( | ||
<HighlightContainer> | ||
{decorator(props.children, props.decorators)} | ||
</HighlightContainer> | ||
); | ||
} | ||
|
||
HighlightAtom.propTypes = { | ||
/** The text to highlight with the decorators. */ | ||
children: PropTypes.string.isRequired, | ||
/** All decorators with their starting character. */ | ||
decorators: PropTypes.object.isRequired, | ||
}; |
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 './highlight'; |
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