Skip to content

Commit 140d013

Browse files
committed
Merge pull request draft-js-plugins#175 from adrianmc/master
Adding ability to set prefix and target for Linkify plugin
2 parents d63c5dd + 0f5c242 commit 140d013

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

docs/client/components/pages/Linkify/CustomLinkifyEditor/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Editor from 'draft-js-plugins-editor';
44
import createLinkifyPlugin from 'draft-js-linkify-plugin';
55
import editorStyles from './editorStyles.css';
66

7-
const linkifyPlugin = createLinkifyPlugin();
7+
const linkifyPlugin = createLinkifyPlugin({ target: '_blank' });
88
const plugins = [linkifyPlugin];
99

1010
export default class CustomMentionEditor extends Component {

docs/client/components/pages/Linkify/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export default class App extends Component {
7171
<div className={ styles.subParam }><span className={ styles.subParamName }>link:</span> CSS class to be applied to link text</div>
7272
</div>
7373
</div>
74+
<div className={ styles.param }>
75+
<span className={ styles.paramName }>target</span>
76+
<span>String value for the target attribute. (Default value is _self)</span>
77+
</div>
7478
</Container>
7579
<Container>
7680
<Heading level={ 2 }>Simple Example</Heading>

draft-js-linkify-plugin/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99

1010
- Move to a flat configuration. Instead of plugin properties (decorators & hooks) being stored within pluginProps they now moved to the root object. See the changes here [#150](https://github.com/draft-js-plugins/draft-js-plugins/pull/150/files) as well as the initial discussion here [#143](https://github.com/draft-js-plugins/draft-js-plugins/issues/143)
1111

12+
- Added the ability to set a target attribute through `config.target`. The default value is `_self`.
13+
14+
- Added the ability to utilize the [linkify-it](https://github.com/markdown-it/linkify-it) library to generate smart href values for the resulting component.
15+
1216
## 0.0.3 - 2016-03-25
1317
### Released the first working of DraftJS Linkify Plugin
1418

draft-js-linkify-plugin/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import createLinkifyPlugin from 'draft-js-linkify-plugin';
1212
const linkifyPlugin = createLinkifyPlugin();
1313
```
1414

15+
You may also optionally set the target value for the resulting `<a>` tag:
16+
17+
```js
18+
const linkifyPlugin = createLinkifyPlugin({
19+
target: '_blank' // default is '_self'
20+
});
21+
```
22+
1523
## Importing the default styles
1624

1725
The plugin ships with a default styling available at this location in the installed package:

draft-js-linkify-plugin/src/Link/__test__/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,34 @@ describe('Link', () => {
3232
const result = shallow(<Link decoratedText="https://www.draft-js-plugins.com/" />);
3333
expect(result).to.have.prop('href').to.contain('https://www.draft-js-plugins.com/');
3434
});
35+
36+
it('applies http prefix when none is supplied', () => {
37+
const result = shallow(<Link decoratedText="draft-js-plugins.com/" />);
38+
expect(result).to.have.prop('href').to.contain('http://draft-js-plugins.com/');
39+
});
40+
41+
it('does not apply a prefix when one is already supplied', () => {
42+
const result = shallow(<Link decoratedText="ftp://draft-js-plugins.com/" />);
43+
expect(result).to.have.prop('href').to.contain('ftp://draft-js-plugins.com/');
44+
});
45+
46+
it('generates correct href to localhost with port', () => {
47+
const result = shallow(<Link decoratedText="http://localhost:8000" />);
48+
expect(result).to.have.prop('href').to.contain('http://localhost:8000');
49+
});
50+
51+
it('generates mailto href when supplied with email', () => {
52+
const result = shallow(<Link decoratedText="name@example.com" />);
53+
expect(result).to.have.prop('href').to.contain('mailto:name@example.com');
54+
});
55+
56+
it('uses _self as the default target value', () => {
57+
const result = shallow(<Link />);
58+
expect(result).to.have.prop('target').to.contain('_self');
59+
});
60+
61+
it('applies custom target value', () => {
62+
const result = shallow(<Link target="_blank" />);
63+
expect(result).to.have.prop('target').to.contain('_blank');
64+
});
3565
});

draft-js-linkify-plugin/src/Link/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import React, { Component } from 'react';
22
import unionClassNames from 'union-class-names';
33
import { Map } from 'immutable';
4+
import linkifyIt from 'linkify-it';
5+
import tlds from 'tlds';
6+
7+
const linkify = linkifyIt();
8+
linkify.tlds(tlds);
49

510
// The component we render when we encounter a hyperlink in the text
611
export default class Link extends Component {
712
render() {
8-
const { theme = Map(), className, ...props } = this.props; // eslint-disable-line no-use-before-define
13+
/* eslint-disable no-use-before-define */
14+
const {
15+
decoratedText = '',
16+
theme = Map(),
17+
target = '_self',
18+
className,
19+
...props,
20+
} = this.props;
21+
/* eslint-enable */
922
const combinedClassName = unionClassNames(theme.get('link'), className);
23+
const links = linkify.match(decoratedText);
24+
const href = links && links[0] ? links[0].url : '';
1025
return (
1126
<a
1227
{ ...props }
13-
href={ this.props.decoratedText }
28+
href={ href }
1429
className={ combinedClassName }
30+
target={ target }
1531
/>
1632
);
1733
}

draft-js-linkify-plugin/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ const linkPlugin = (config = {}) => {
1616
// errors when upgrading as basically every styling change would become a major
1717
// breaking change. 1px of an increased padding can break a whole layout.
1818
const theme = config.theme ? config.theme : defaultTheme;
19+
const target = config.target ? config.target : '_self';
1920
return {
2021
decorators: [
2122
{
2223
strategy: linkStrategy,
23-
component: decorateComponentWithProps(Link, { theme }),
24+
component: decorateComponentWithProps(Link, { theme, target }),
2425
},
2526
],
2627
};

0 commit comments

Comments
 (0)