@automattic/social-previews 1.0.4 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -5
- package/README.md +38 -0
- package/dist/cjs/facebook-preview/index.js +51 -72
- package/dist/cjs/helpers.js +10 -29
- package/dist/cjs/index.js +8 -8
- package/dist/cjs/search-preview/index.js +31 -31
- package/dist/cjs/twitter-preview/index.js +32 -79
- package/dist/cjs/twitter-preview/style.scss +214 -98
- package/dist/cjs/twitter-preview/tweet.js +346 -0
- package/dist/esm/facebook-preview/index.js +49 -67
- package/dist/esm/helpers.js +9 -28
- package/dist/esm/search-preview/index.js +31 -30
- package/dist/esm/twitter-preview/index.js +30 -74
- package/dist/esm/twitter-preview/style.scss +214 -98
- package/dist/esm/twitter-preview/tweet.js +325 -0
- package/package.json +58 -52
- package/src/facebook-preview/index.jsx +2 -9
- package/src/helpers.js +5 -7
- package/src/search-preview/index.jsx +0 -8
- package/src/twitter-preview/index.jsx +31 -46
- package/src/twitter-preview/style.scss +214 -98
- package/src/twitter-preview/tweet.jsx +296 -0
- package/LICENSE.md +0 -264
|
@@ -1,75 +1,57 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
-
import _inherits from "@babel/runtime/helpers/inherits";
|
|
4
|
-
import _createSuper from "@babel/runtime/helpers/createSuper";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* External dependencies
|
|
8
|
-
*/
|
|
9
|
-
import PropTypes from 'prop-types';
|
|
10
|
-
import React, { PureComponent } from 'react';
|
|
11
1
|
import { compact } from 'lodash';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
import { PureComponent } from 'react';
|
|
12
4
|
import { firstValid, hardTruncation, shortEnough, stripHtmlTags } from '../helpers';
|
|
13
|
-
/**
|
|
14
|
-
* Style dependencies
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
5
|
import './style.scss';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
6
|
+
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
7
|
+
import { jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
8
|
+
const TITLE_LENGTH = 80;
|
|
9
|
+
const DESCRIPTION_LENGTH = 200;
|
|
10
|
+
|
|
11
|
+
const baseDomain = url => url && url.replace(/^[^/]+[/]*/, '') // strip leading protocol
|
|
12
|
+
.replace(/\/.*$/, ''); // strip everything after the domain
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
const facebookTitle = firstValid(shortEnough(TITLE_LENGTH), hardTruncation(TITLE_LENGTH));
|
|
16
|
+
const facebookDescription = firstValid(shortEnough(DESCRIPTION_LENGTH), hardTruncation(DESCRIPTION_LENGTH));
|
|
17
|
+
export class FacebookPreview extends PureComponent {
|
|
18
|
+
render() {
|
|
19
|
+
const {
|
|
20
|
+
url,
|
|
21
|
+
type,
|
|
22
|
+
title,
|
|
23
|
+
description,
|
|
24
|
+
image,
|
|
25
|
+
author
|
|
26
|
+
} = this.props;
|
|
27
|
+
return _jsx("div", {
|
|
28
|
+
className: `facebook-preview facebook-preview__${type}`,
|
|
29
|
+
children: _jsxs("div", {
|
|
30
|
+
className: "facebook-preview__content",
|
|
31
|
+
children: [_jsx("div", {
|
|
32
|
+
className: "facebook-preview__image",
|
|
33
|
+
children: image && _jsx("img", {
|
|
34
|
+
alt: "Facebook Preview Thumbnail",
|
|
35
|
+
src: image
|
|
36
|
+
})
|
|
37
|
+
}), _jsxs("div", {
|
|
38
|
+
className: "facebook-preview__body",
|
|
39
|
+
children: [_jsx("div", {
|
|
40
|
+
className: "facebook-preview__url",
|
|
41
|
+
children: compact([baseDomain(url), author]).join(' | ')
|
|
42
|
+
}), _jsx("div", {
|
|
43
|
+
className: "facebook-preview__title",
|
|
44
|
+
children: facebookTitle(title || '')
|
|
45
|
+
}), _jsx("div", {
|
|
46
|
+
className: "facebook-preview__description",
|
|
47
|
+
children: facebookDescription(stripHtmlTags(description))
|
|
48
|
+
})]
|
|
49
|
+
})]
|
|
50
|
+
})
|
|
51
|
+
});
|
|
38
52
|
}
|
|
39
53
|
|
|
40
|
-
|
|
41
|
-
key: "render",
|
|
42
|
-
value: function render() {
|
|
43
|
-
var _this$props = this.props,
|
|
44
|
-
url = _this$props.url,
|
|
45
|
-
type = _this$props.type,
|
|
46
|
-
title = _this$props.title,
|
|
47
|
-
description = _this$props.description,
|
|
48
|
-
image = _this$props.image,
|
|
49
|
-
author = _this$props.author;
|
|
50
|
-
return /*#__PURE__*/React.createElement("div", {
|
|
51
|
-
className: "facebook-preview facebook-preview__".concat(type)
|
|
52
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
53
|
-
className: "facebook-preview__content"
|
|
54
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
55
|
-
className: "facebook-preview__image"
|
|
56
|
-
}, image && /*#__PURE__*/React.createElement("img", {
|
|
57
|
-
alt: "Facebook Preview Thumbnail",
|
|
58
|
-
src: image
|
|
59
|
-
})), /*#__PURE__*/React.createElement("div", {
|
|
60
|
-
className: "facebook-preview__body"
|
|
61
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
62
|
-
className: "facebook-preview__url"
|
|
63
|
-
}, compact([baseDomain(url), author]).join(' | ')), /*#__PURE__*/React.createElement("div", {
|
|
64
|
-
className: "facebook-preview__title"
|
|
65
|
-
}, facebookTitle(title || '')), /*#__PURE__*/React.createElement("div", {
|
|
66
|
-
className: "facebook-preview__description"
|
|
67
|
-
}, facebookDescription(stripHtmlTags(description))))));
|
|
68
|
-
}
|
|
69
|
-
}]);
|
|
70
|
-
|
|
71
|
-
return FacebookPreview;
|
|
72
|
-
}(PureComponent);
|
|
54
|
+
}
|
|
73
55
|
FacebookPreview.propTypes = {
|
|
74
56
|
url: PropTypes.string,
|
|
75
57
|
type: PropTypes.string,
|
package/dist/esm/helpers.js
CHANGED
|
@@ -1,35 +1,16 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
1
|
import { find } from 'lodash';
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
export const shortEnough = limit => title => title.length <= limit ? title : false;
|
|
3
|
+
export const truncatedAtSpace = (lower, upper) => fullTitle => {
|
|
4
|
+
const title = fullTitle.slice(0, upper);
|
|
5
|
+
const lastSpace = title.lastIndexOf(' ');
|
|
6
|
+
return lastSpace > lower && lastSpace < upper ? title.slice(0, lastSpace).concat('…') : false;
|
|
9
7
|
};
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
var title = fullTitle.slice(0, upper);
|
|
13
|
-
var lastSpace = title.lastIndexOf(' ');
|
|
14
|
-
return lastSpace > lower && lastSpace < upper ? title.slice(0, lastSpace).concat('…') : false;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
export var hardTruncation = function hardTruncation(limit) {
|
|
18
|
-
return function (title) {
|
|
19
|
-
return title.slice(0, limit).concat('…');
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
export var firstValid = function firstValid() {
|
|
8
|
+
export const hardTruncation = limit => title => title.slice(0, limit).concat('…');
|
|
9
|
+
export const firstValid = function () {
|
|
23
10
|
for (var _len = arguments.length, predicates = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
24
11
|
predicates[_key] = arguments[_key];
|
|
25
12
|
}
|
|
26
13
|
|
|
27
|
-
return
|
|
28
|
-
return find(predicates, function (p) {
|
|
29
|
-
return false !== p(a);
|
|
30
|
-
})(a);
|
|
31
|
-
};
|
|
14
|
+
return a => find(predicates, p => false !== p(a))(a);
|
|
32
15
|
};
|
|
33
|
-
export
|
|
34
|
-
return description ? description.replace(/(<([^>]+)>)/gi, '') : '';
|
|
35
|
-
};
|
|
16
|
+
export const stripHtmlTags = description => description ? description.replace(/(<([^>]+)>)/gi, '') : '';
|
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
1
|
import PropTypes from 'prop-types';
|
|
5
|
-
import React from 'react';
|
|
6
2
|
import { firstValid, hardTruncation, shortEnough, truncatedAtSpace, stripHtmlTags } from '../helpers';
|
|
7
|
-
/**
|
|
8
|
-
* Style dependencies
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
3
|
import './style.scss';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
import { jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
5
|
+
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
6
|
+
const URL_LENGTH = 68;
|
|
7
|
+
const TITLE_LENGTH = 63;
|
|
8
|
+
const DESCRIPTION_LENGTH = 160;
|
|
15
9
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
10
|
+
const googleUrl = url => {
|
|
11
|
+
const breadcrumb = url.replace(/^[^/]+[/]*/, '').split('/').join(' › ');
|
|
12
|
+
const truncateBreadcrumb = firstValid(shortEnough(URL_LENGTH), hardTruncation(URL_LENGTH));
|
|
19
13
|
return truncateBreadcrumb(breadcrumb);
|
|
20
14
|
};
|
|
21
15
|
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
const googleTitle = firstValid(shortEnough(TITLE_LENGTH), truncatedAtSpace(TITLE_LENGTH - 40, TITLE_LENGTH + 10), hardTruncation(TITLE_LENGTH));
|
|
17
|
+
const googleDescription = firstValid(shortEnough(DESCRIPTION_LENGTH), truncatedAtSpace(DESCRIPTION_LENGTH - 80, DESCRIPTION_LENGTH + 10), hardTruncation(DESCRIPTION_LENGTH));
|
|
24
18
|
export default function SearchPreview(_ref) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
className: "search-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
19
|
+
let {
|
|
20
|
+
description,
|
|
21
|
+
title,
|
|
22
|
+
url
|
|
23
|
+
} = _ref;
|
|
24
|
+
return _jsx("div", {
|
|
25
|
+
className: "search-preview",
|
|
26
|
+
children: _jsxs("div", {
|
|
27
|
+
className: "search-preview__display",
|
|
28
|
+
children: [_jsxs("div", {
|
|
29
|
+
className: "search-preview__url",
|
|
30
|
+
children: [googleUrl(url), " \u25BE"]
|
|
31
|
+
}), _jsx("div", {
|
|
32
|
+
className: "search-preview__title",
|
|
33
|
+
children: googleTitle(title)
|
|
34
|
+
}), _jsx("div", {
|
|
35
|
+
className: "search-preview__description",
|
|
36
|
+
children: googleDescription(stripHtmlTags(description))
|
|
37
|
+
})]
|
|
38
|
+
})
|
|
39
|
+
});
|
|
39
40
|
}
|
|
40
41
|
SearchPreview.propTypes = {
|
|
41
42
|
title: PropTypes.string,
|
|
@@ -1,81 +1,37 @@
|
|
|
1
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
-
import _inherits from "@babel/runtime/helpers/inherits";
|
|
4
|
-
import _createSuper from "@babel/runtime/helpers/createSuper";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* External dependencies
|
|
8
|
-
*/
|
|
9
1
|
import PropTypes from 'prop-types';
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
* Internal dependencies
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { firstValid, hardTruncation, shortEnough, stripHtmlTags } from '../helpers';
|
|
16
|
-
/**
|
|
17
|
-
* Style dependencies
|
|
18
|
-
*/
|
|
19
|
-
|
|
2
|
+
import { PureComponent } from 'react';
|
|
3
|
+
import { Tweet } from './tweet';
|
|
20
4
|
import './style.scss';
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
5
|
+
import { jsx as _jsx } from "@emotion/react/jsx-runtime";
|
|
6
|
+
export class TwitterPreview extends PureComponent {
|
|
7
|
+
render() {
|
|
8
|
+
// Previous versions expected passed props to be the Twitter Card details for a single tweet,
|
|
9
|
+
// rather than an array of tweets. If the tweet array isn't passed, we can construct it from
|
|
10
|
+
// the old props.
|
|
11
|
+
const tweets = this.props.tweets || [{
|
|
12
|
+
text: '',
|
|
13
|
+
media: [],
|
|
14
|
+
card: { ...this.props,
|
|
15
|
+
type: 'large_image_summary' === this.props.type ? 'summary_large_image' : this.props.type
|
|
16
|
+
},
|
|
17
|
+
date: Date.now(),
|
|
18
|
+
name: 'Account Name',
|
|
19
|
+
profileImage: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_bigger.png',
|
|
20
|
+
screenName: '@account'
|
|
21
|
+
}];
|
|
22
|
+
return _jsx("div", {
|
|
23
|
+
className: "twitter-preview",
|
|
24
|
+
children: tweets && tweets.map((tweet, index) => {
|
|
25
|
+
return _jsx(Tweet, {
|
|
26
|
+
isLast: index + 1 === tweets.length,
|
|
27
|
+
...tweet
|
|
28
|
+
}, `twitter-preview__tweet-${index}`);
|
|
29
|
+
})
|
|
30
|
+
});
|
|
39
31
|
}
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
key: "render",
|
|
43
|
-
value: function render() {
|
|
44
|
-
var _this$props = this.props,
|
|
45
|
-
url = _this$props.url,
|
|
46
|
-
title = _this$props.title,
|
|
47
|
-
type = _this$props.type,
|
|
48
|
-
description = _this$props.description,
|
|
49
|
-
image = _this$props.image;
|
|
50
|
-
var previewImageStyle = {
|
|
51
|
-
backgroundImage: 'url(' + image + ')'
|
|
52
|
-
};
|
|
53
|
-
return /*#__PURE__*/React.createElement("div", {
|
|
54
|
-
className: "twitter-preview"
|
|
55
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
56
|
-
className: "twitter-preview__".concat(type)
|
|
57
|
-
}, image && /*#__PURE__*/React.createElement("div", {
|
|
58
|
-
className: "twitter-preview__image",
|
|
59
|
-
style: previewImageStyle
|
|
60
|
-
}), /*#__PURE__*/React.createElement("div", {
|
|
61
|
-
className: "twitter-preview__body"
|
|
62
|
-
}, /*#__PURE__*/React.createElement("div", {
|
|
63
|
-
className: "twitter-preview__title"
|
|
64
|
-
}, title), /*#__PURE__*/React.createElement("div", {
|
|
65
|
-
className: "twitter-preview__description"
|
|
66
|
-
}, twitterDescription(stripHtmlTags(description))), /*#__PURE__*/React.createElement("div", {
|
|
67
|
-
className: "twitter-preview__url"
|
|
68
|
-
}, baseDomain(url || '')))));
|
|
69
|
-
}
|
|
70
|
-
}]);
|
|
71
|
-
|
|
72
|
-
return TwitterPreview;
|
|
73
|
-
}(PureComponent);
|
|
33
|
+
}
|
|
74
34
|
TwitterPreview.propTypes = {
|
|
75
|
-
|
|
76
|
-
title: PropTypes.string,
|
|
77
|
-
type: PropTypes.string,
|
|
78
|
-
description: PropTypes.string,
|
|
79
|
-
image: PropTypes.string
|
|
35
|
+
tweets: PropTypes.array
|
|
80
36
|
};
|
|
81
37
|
export default TwitterPreview;
|