@ecency/render-helper 2.3.17 → 2.4.1

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.
Files changed (155) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +171 -17
  3. package/dist/browser/index.d.ts +32 -0
  4. package/dist/browser/index.js +1556 -0
  5. package/dist/browser/index.js.map +1 -0
  6. package/dist/node/index.cjs +1575 -0
  7. package/dist/node/index.cjs.map +1 -0
  8. package/dist/node/index.mjs +1559 -0
  9. package/dist/node/index.mjs.map +1 -0
  10. package/package.json +49 -47
  11. package/lib/cache.d.ts +0 -3
  12. package/lib/cache.js +0 -21
  13. package/lib/cache.js.map +0 -1
  14. package/lib/catch-post-image.d.ts +0 -2
  15. package/lib/catch-post-image.js +0 -76
  16. package/lib/catch-post-image.js.map +0 -1
  17. package/lib/consts/allowed-attributes.const.d.ts +0 -2
  18. package/lib/consts/allowed-attributes.const.js +0 -71
  19. package/lib/consts/allowed-attributes.const.js.map +0 -1
  20. package/lib/consts/dom-parser.const.d.ts +0 -2
  21. package/lib/consts/dom-parser.const.js +0 -12
  22. package/lib/consts/dom-parser.const.js.map +0 -1
  23. package/lib/consts/index.d.ts +0 -5
  24. package/lib/consts/index.js +0 -22
  25. package/lib/consts/index.js.map +0 -1
  26. package/lib/consts/regexes.const.d.ts +0 -44
  27. package/lib/consts/regexes.const.js +0 -49
  28. package/lib/consts/regexes.const.js.map +0 -1
  29. package/lib/consts/section-list.const.d.ts +0 -1
  30. package/lib/consts/section-list.const.js +0 -24
  31. package/lib/consts/section-list.const.js.map +0 -1
  32. package/lib/consts/white-list.const.d.ts +0 -2
  33. package/lib/consts/white-list.const.js +0 -37
  34. package/lib/consts/white-list.const.js.map +0 -1
  35. package/lib/helper.d.ts +0 -6
  36. package/lib/helper.js +0 -72
  37. package/lib/helper.js.map +0 -1
  38. package/lib/index.d.ts +0 -8
  39. package/lib/index.js +0 -19
  40. package/lib/index.js.map +0 -1
  41. package/lib/markdown-2-html.d.ts +0 -2
  42. package/lib/markdown-2-html.js +0 -25
  43. package/lib/markdown-2-html.js.map +0 -1
  44. package/lib/methods/a.method.d.ts +0 -1
  45. package/lib/methods/a.method.js +0 -647
  46. package/lib/methods/a.method.js.map +0 -1
  47. package/lib/methods/clean-reply.method.d.ts +0 -1
  48. package/lib/methods/clean-reply.method.js +0 -37
  49. package/lib/methods/clean-reply.method.js.map +0 -1
  50. package/lib/methods/get-inner-html.method.d.ts +0 -1
  51. package/lib/methods/get-inner-html.method.js +0 -16
  52. package/lib/methods/get-inner-html.method.js.map +0 -1
  53. package/lib/methods/iframe.method.d.ts +0 -1
  54. package/lib/methods/iframe.method.js +0 -159
  55. package/lib/methods/iframe.method.js.map +0 -1
  56. package/lib/methods/img.method.d.ts +0 -4
  57. package/lib/methods/img.method.js +0 -50
  58. package/lib/methods/img.method.js.map +0 -1
  59. package/lib/methods/index.d.ts +0 -7
  60. package/lib/methods/index.js +0 -24
  61. package/lib/methods/index.js.map +0 -1
  62. package/lib/methods/linkify.method.d.ts +0 -1
  63. package/lib/methods/linkify.method.js +0 -56
  64. package/lib/methods/linkify.method.js.map +0 -1
  65. package/lib/methods/markdown-to-html.method.d.ts +0 -1
  66. package/lib/methods/markdown-to-html.method.js +0 -98
  67. package/lib/methods/markdown-to-html.method.js.map +0 -1
  68. package/lib/methods/noop.method.d.ts +0 -1
  69. package/lib/methods/noop.method.js +0 -6
  70. package/lib/methods/noop.method.js.map +0 -1
  71. package/lib/methods/p.method.d.ts +0 -1
  72. package/lib/methods/p.method.js +0 -11
  73. package/lib/methods/p.method.js.map +0 -1
  74. package/lib/methods/remove-child-nodes.method.d.ts +0 -1
  75. package/lib/methods/remove-child-nodes.method.js +0 -10
  76. package/lib/methods/remove-child-nodes.method.js.map +0 -1
  77. package/lib/methods/sanitize-html.method.d.ts +0 -1
  78. package/lib/methods/sanitize-html.method.js +0 -42
  79. package/lib/methods/sanitize-html.method.js.map +0 -1
  80. package/lib/methods/text.method.d.ts +0 -1
  81. package/lib/methods/text.method.js +0 -69
  82. package/lib/methods/text.method.js.map +0 -1
  83. package/lib/methods/traverse.method.d.ts +0 -3
  84. package/lib/methods/traverse.method.js +0 -38
  85. package/lib/methods/traverse.method.js.map +0 -1
  86. package/lib/post-body-summary.d.ts +0 -2
  87. package/lib/post-body-summary.js +0 -123
  88. package/lib/post-body-summary.js.map +0 -1
  89. package/lib/proxify-image-src.d.ts +0 -5
  90. package/lib/proxify-image-src.js +0 -86
  91. package/lib/proxify-image-src.js.map +0 -1
  92. package/lib/render-helper.js +0 -1
  93. package/lib/types/entry.interface.d.ts +0 -7
  94. package/lib/types/entry.interface.js +0 -3
  95. package/lib/types/entry.interface.js.map +0 -1
  96. package/lib/types/index.d.ts +0 -2
  97. package/lib/types/index.js +0 -19
  98. package/lib/types/index.js.map +0 -1
  99. package/lib/types/xss-white-list.interface.d.ts +0 -6
  100. package/lib/types/xss-white-list.interface.js +0 -3
  101. package/lib/types/xss-white-list.interface.js.map +0 -1
  102. package/src/cache.ts +0 -15
  103. package/src/catch-post-image.spec.ts +0 -144
  104. package/src/catch-post-image.ts +0 -78
  105. package/src/consts/allowed-attributes.const.ts +0 -69
  106. package/src/consts/dom-parser.const.ts +0 -6
  107. package/src/consts/index.ts +0 -5
  108. package/src/consts/regexes.const.ts +0 -46
  109. package/src/consts/section-list.const.ts +0 -20
  110. package/src/consts/white-list.const.ts +0 -33
  111. package/src/external-types/multihashes.d.ts +0 -3
  112. package/src/helper.spec.ts +0 -16
  113. package/src/helper.ts +0 -77
  114. package/src/index.ts +0 -18
  115. package/src/markdown-2-html.spec.ts +0 -1251
  116. package/src/markdown-2-html.ts +0 -25
  117. package/src/methods/a.method.ts +0 -791
  118. package/src/methods/clean-reply.method.ts +0 -32
  119. package/src/methods/get-inner-html.method.ts +0 -11
  120. package/src/methods/iframe.method.ts +0 -176
  121. package/src/methods/img.method.ts +0 -62
  122. package/src/methods/index.ts +0 -7
  123. package/src/methods/linkify.method.ts +0 -61
  124. package/src/methods/markdown-to-html.method.ts +0 -104
  125. package/src/methods/noop.method.ts +0 -1
  126. package/src/methods/p.method.ts +0 -6
  127. package/src/methods/remove-child-nodes.method.ts +0 -5
  128. package/src/methods/sanitize-html.method.ts +0 -32
  129. package/src/methods/text.method.ts +0 -77
  130. package/src/methods/traverse.method.ts +0 -33
  131. package/src/post-body-summary.spec.ts +0 -150
  132. package/src/post-body-summary.ts +0 -130
  133. package/src/proxify-image-src.spec.ts +0 -76
  134. package/src/proxify-image-src.ts +0 -77
  135. package/src/sanitize-html.spec.ts +0 -15
  136. package/src/test/data/index.ts +0 -12
  137. package/src/test/data/json/muratkbesiroglu____sci-fi-novel-underground-city-part-13.json +0 -2120
  138. package/src/test/data/json/steemitboard____steemitboard-notify-dunsky-20181210t153450000z.json +0 -47
  139. package/src/test/data/legacy/10.json +0 -5
  140. package/src/test/data/legacy/20.json +0 -5
  141. package/src/test/data/legacy/21.json +0 -5
  142. package/src/test/data/legacy/2112524.json +0 -5
  143. package/src/test/data/legacy/22.json +0 -5
  144. package/src/test/data/legacy/23.json +0 -5
  145. package/src/test/data/legacy/24.json +0 -5
  146. package/src/test/data/legacy/25.json +0 -5
  147. package/src/test/data/legacy/26.json +0 -5
  148. package/src/test/data/legacy/27.JSON +0 -5
  149. package/src/test/data/legacy/28.json +0 -5
  150. package/src/test/data/legacy/29.json +0 -5
  151. package/src/test/data/legacy/31.json +0 -5
  152. package/src/test/snaps.json +0 -8
  153. package/src/types/entry.interface.ts +0 -7
  154. package/src/types/index.ts +0 -2
  155. package/src/types/xss-white-list.interface.ts +0 -7
@@ -1,150 +0,0 @@
1
- import { getPostBodySummary } from './post-body-summary'
2
-
3
- describe('postBodySummary', () => {
4
- it('1- Should return empty string', () => {
5
- const input = {
6
- author: 'foo81',
7
- permlink: 'bar81',
8
- last_update: '2019-05-10T09:15:21',
9
- body: ''
10
- }
11
- const expected = ''
12
- expect(getPostBodySummary(input)).toBe(expected)
13
- })
14
-
15
- it('2- Should remove html tags', () => {
16
- const input = {
17
- author: 'foo82',
18
- permlink: 'bar82',
19
- last_update: '2019-05-10T09:15:21',
20
- body: '<center>Lorem Ipsum Dolor</center>'
21
- }
22
- const expected = 'Lorem Ipsum Dolor'
23
- expect(getPostBodySummary(input)).toBe(expected)
24
- })
25
-
26
- it('3- Should remove new lines', () => {
27
- const input = {
28
- author: 'foo83',
29
- permlink: 'bar83',
30
- last_update: '2019-05-10T09:15:21',
31
- body: 'Lorem \n Ipsum \n Dolor'
32
- }
33
- const expected = 'Lorem Ipsum Dolor'
34
- expect(getPostBodySummary(input)).toBe(expected)
35
- })
36
-
37
- it('4- Should trim', () => {
38
- const input = {
39
- author: 'foo84',
40
- permlink: 'bar84',
41
- last_update: '2019-05-10T09:15:21',
42
- body: ' Lorem Ipsum Dolor '
43
- }
44
- const expected = 'Lorem Ipsum Dolor'
45
- expect(getPostBodySummary(input)).toBe(expected)
46
- })
47
-
48
- it('5- Should remove urls', () => {
49
- const input = {
50
- author: 'foo85',
51
- permlink: 'bar85',
52
- last_update: '2019-05-10T09:15:21',
53
- body: 'Lorem http://lorem.com Ipsum Dolor https://ipsum.com'
54
- }
55
- const expected = 'Lorem Ipsum Dolor'
56
- expect(getPostBodySummary(input)).toBe(expected)
57
- })
58
-
59
- it('6- Should remove white spaces between words', () => {
60
- const input = {
61
- author: 'foo86',
62
- permlink: 'bar86',
63
- last_update: '2019-05-10T09:15:21',
64
- body: ' Lorem Ipsum Dolor '
65
- }
66
- const expected = 'Lorem Ipsum Dolor'
67
- expect(getPostBodySummary(input)).toBe(expected)
68
- })
69
-
70
- it('7- Should limit to 20', () => {
71
- const input = {
72
- author: 'foo87',
73
- permlink: 'bar87',
74
- last_update: '2019-05-10T09:15:21',
75
- body: 'lorem ipsum dolor sit amet'
76
- }
77
- const expected = 'lorem ipsum dolor sit'
78
- expect(getPostBodySummary(input, 20)).toBe(expected)
79
- })
80
-
81
- it('8- Test with long markdown', () => {
82
- const input = {
83
- author: 'foo88',
84
- permlink: 'bar88',
85
- last_update: '2019-05-10T09:15:21',
86
- body: `https://youtu.be/DII2VTXDP7A
87
- In this post, we want to bring you up to speed on what is happening inside Steemit, as well as give you our perspective on the successes (and failures) of the past year, let you know what we see as our mission going forward, and provide some insight into what we have planned.
88
- <h1>Steemit’s Vision and Mission</h1>
89
- Through our vision of **empowering entrepreneurs to tokenize the internet**, our primary roles in the Steem ecosystem are providing the community with software enhancements to the Steem blockchain, modular framework applications made up of components that can be leveraged by application developers and inspiration through these platforms to entrepreneurial end-users. We believe we must build in ways that create as many opportunities — and catalyze as many amazing Steem-based entrepreneurs and communities — as possible.`
90
- }
91
-
92
- const expected = 'In this post, we want to bring you up to speed on what is happening inside Steemit, as well as give you our perspective on the successes (and failures) of the past year, let you know what we see as our'
93
- expect(getPostBodySummary(input, 200)).toBe(expected)
94
- })
95
-
96
- it('9- Test with long markdown', () => {
97
- const input = {
98
- author: 'foo89',
99
- permlink: 'bar89',
100
- last_update: '2019-05-10T09:15:21',
101
- body: `<html>
102
- <center><a href='https://d.tube/#!/v/surfermarly/x2zm8s26'><img src='https://images.ecency.com/DQmdxVxFgLu8PT1TchgQUHtd7LGvKtF5u1DbHmDDKALiwxi/dreamsd1.jpg'></a></center><hr>
103
- <p><b>Everybody has a dream. Most of the time it takes us a while to turn these great wishes into reality, especially because they usually come with a bigger price tag.</b></p>
104
- <p>Now thanks to Steem some of us will be able to cut corners in order to achieve their goals more quickly. The additional income can be a game changer to many players.</p>
105
- <p>My Steem earnings enabled me to buy a piece of land where I'll build my first own house. Writing down these words still seems a bit unreal to me, since I didn't believe to come to that point so quickly.</p>
106
- <h3>Click on the above image or <a href='https://d.tube/#!/v/surfermarly/x2zm8s26'>HERE ▶️</a> to watch my video statement.</h3>
107
- <p>Without hard work and dedication as well as the great support from the community this wouldn't have been possible. Also the timing was brilliant, I joined in the earlier stages.</p>
108
- <p><b>I'm both grateful and proud.</b></p>
109
- <p>Many people supported me along the way, some tried to tear me down. I'm glad I only stayed with those who pushed me, they have a large stake in my success story.</p></html>`
110
- }
111
-
112
- const expected = 'Everybody has a dream. Most of the time it takes us a while to turn these great wishes into reality, especially because they usually come with a bigger price tag. Now thanks to Steem some of us will be'
113
- expect(getPostBodySummary(input, 200)).toBe(expected)
114
- })
115
-
116
- it('10- Test with long markdown', () => {
117
- const input = {
118
- author: 'foo90',
119
- permlink: 'bar90',
120
- last_update: '2019-05-10T09:15:21',
121
- body: `<center><a href='https://d.tube/#!/v/marpemusic/gi5e9yrl'><img src='https://ipfs.io/ipfs/QmYK5yzDHyoDVQQ5xgV4RLzcZz5Qy95Hz1n6qbEusJGHYB'></a></center><hr>
122
- ![DTUBE.jpg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519196266/ebleqjokesumzw3mwpcz.jpg)
123
- ###### Hey Dtube!
124
- ###### Hey Steemian
125
- ###### It's your boy marpe @marpemusic. I greet you from my stable, Ibadan Nigeria.
126
- **There's excitement in the air! The epoch making STEEMIB (that is, STEEMIT IBADAN) meet up is around the corner!! Where would you rather be on the 24th of February 2018? You don't want to miss this peeps.**
127
- ![meet up.jpeg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519196411/ihzgikihusxnazqpzltr.jpg)
128
- ### Boooommm!!!
129
- I'm giving Three DTUBE & STEEMIT BRANDED TEES to three people for free. Yes, for free. *E fit be you ooo!*
130
- So, how can you qualify to get one?`
131
- }
132
-
133
- const expected = 'Hey Dtube! Hey Steemian It\'s your boy marpe @marpemusic. I greet you from my stable, Ibadan Nigeria. There\'s excitement in the air! The epoch making STEEMIB (that is, STEEMIT IBADAN) meet up is around'
134
-
135
- expect(getPostBodySummary(input, 200)).toBe(expected)
136
- })
137
-
138
- it('11- Test with not obj param', () => {
139
- const input = '<center>Lorem Ipsum Dolor</center>'
140
- const expected = 'Lorem Ipsum Dolor'
141
- expect(getPostBodySummary(input)).toBe(expected)
142
- })
143
-
144
-
145
- it('12- Test entity parsing', () => {
146
- const input = 'http://lorem.com Lorem &lt; Ipsum &amp; Dolor &euro; '
147
- const expected = 'Lorem < Ipsum & Dolor €'
148
- expect(getPostBodySummary(input)).toBe(expected)
149
- })
150
- })
@@ -1,130 +0,0 @@
1
- import he from 'he'
2
- import { makeEntryCacheKey } from './helper'
3
- import { cacheGet, cacheSet } from './cache'
4
- import { Entry } from './types'
5
- import { cleanReply } from './methods'
6
- import { ENTITY_REGEX } from './consts'
7
-
8
-
9
- const { Remarkable } = require('remarkable')
10
- const { linkify } = require('remarkable/linkify')
11
-
12
- const joint = (arr: string[], limit = 200) => {
13
- let result = '';
14
- if (arr) {
15
- for (let i = 0; i < arr.length; i++) {
16
- // join array with space separator
17
- if (result) {
18
- result += " ";
19
- }
20
- // break with length reaches limit
21
- if (result.length > limit) {
22
- break;
23
- } else {
24
- // make sure last join doesn't break the limit too much
25
- if ((result + arr[i]).length < limit + 10) {
26
- result += arr[i];
27
- } else {
28
- break;
29
- }
30
- }
31
- }
32
- }
33
- return result.trim();
34
- };
35
-
36
- function postBodySummary(entryBody: string, length?: number, platform:'ios'|'android'|'web' = 'web'): string {
37
- if (!entryBody) {
38
- return ''
39
- }
40
- entryBody = cleanReply(entryBody)
41
-
42
- const mdd = new Remarkable({
43
- html: true,
44
- breaks: true,
45
- typographer: false,
46
- }).use(linkify)
47
- mdd.core.ruler.enable([
48
- 'abbr'
49
- ]);
50
- mdd.block.ruler.enable([
51
- 'footnote',
52
- 'deflist'
53
- ]);
54
- mdd.inline.ruler.enable([
55
- 'footnote_inline',
56
- 'ins',
57
- 'mark',
58
- 'sub',
59
- 'sup'
60
- ]);
61
-
62
- //encrypt entities
63
- const entities = entryBody.match(ENTITY_REGEX);
64
- const encEntities:string[] = [];
65
- if(entities && platform !== 'web'){
66
- entities.forEach((entity)=>{
67
- var CryptoJS = require("react-native-crypto-js");
68
- const encData = CryptoJS.AES.encrypt(entity, 'key').toString();
69
- let encyptedEntity = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encData));
70
- encEntities.push(encyptedEntity);
71
- entryBody = entryBody.replace(entity, encyptedEntity);
72
- })
73
- }
74
-
75
- // Convert markdown to html
76
- let text = '';
77
- try {
78
- text = mdd.render(entryBody)
79
- } catch (err) {
80
- console.log(err)
81
- }
82
-
83
-
84
- //decrypt and put back entiteis
85
- if(platform !== 'web'){
86
- encEntities.forEach((encEntity)=>{
87
- var CryptoJS = require("react-native-crypto-js");
88
- let decData = CryptoJS.enc.Base64.parse(encEntity).toString(CryptoJS.enc.Utf8);
89
- let entity = CryptoJS.AES.decrypt(decData, 'key').toString(CryptoJS.enc.Utf8);
90
- text = text.replace(encEntity, entity);
91
- })
92
- }
93
-
94
-
95
- text = text
96
- .replace(/(<([^>]+)>)/gi, '') // Remove html tags
97
- .replace(/\r?\n|\r/g, ' ') // Remove new lines
98
- .replace(/(?:https?|ftp):\/\/[\n\S]+/g, '') // Remove urls
99
- .trim()
100
- .replace(/ +(?= )/g, '') // Remove all multiple spaces
101
-
102
- if (length) {
103
- // Truncate
104
- text = joint(text.split(' '), length)
105
- }
106
-
107
- if (text) {
108
- text = he.decode(text) // decode html entities
109
- }
110
-
111
- return text
112
- }
113
-
114
- export function getPostBodySummary(obj: Entry | string, length?: number, platform?:'ios'|'android'|'web'): any {
115
- if (typeof obj === 'string') {
116
- return postBodySummary(obj as string, length, platform)
117
- }
118
-
119
- const key = `${makeEntryCacheKey(obj)}-sum-${length}`
120
-
121
- const item = cacheGet(key)
122
- if (item) {
123
- return item
124
- }
125
-
126
- const res = postBodySummary(obj.body, length, platform)
127
- cacheSet(key, res)
128
-
129
- return res
130
- }
@@ -1,76 +0,0 @@
1
- import { proxifyImageSrc, setProxyBase, getLatestUrl, extractPHash } from './proxify-image-src'
2
-
3
- describe('getLatestUrl', () => {
4
- it('1- should get the latest url', () => {
5
- const input = 'https://i.imgur.com/muESb0B.png'
6
-
7
- expect(getLatestUrl(input)).toBe('https://i.imgur.com/muESb0B.png')
8
- })
9
-
10
- it('2- should get the latest url', () => {
11
- const input = 'https://images.ecency.com/0x0/https://i.imgur.com/muESb0B.png'
12
-
13
- expect(getLatestUrl(input)).toBe('https://i.imgur.com/muESb0B.png')
14
- })
15
-
16
- it('3- should get the latest url', () => {
17
- const input = 'https://images.ecency.com/0x0/https://images.hive.io/0x0/https://i.imgur.com/muESb0B.png'
18
-
19
- expect(getLatestUrl(input)).toBe('https://i.imgur.com/muESb0B.png')
20
- })
21
- })
22
-
23
- describe('extractPHash', () => {
24
- it('1- should extract hash', () => {
25
- const input = 'https://images.ecency.com/p/RGgukq5E6HBNvuPpuJoWwfXPpi5ckcLESTB3nmmnMt8YnPwgHbJegFaUzokkErqT8JVe4zPL7GD3gy6aaZQERs3MF5KAGJQ1AL4MmhLWfmceyk6XXSqWaECh1YXC7aV.png?format=match&mode=fit'
26
-
27
- expect(extractPHash(input)).toBe('RGgukq5E6HBNvuPpuJoWwfXPpi5ckcLESTB3nmmnMt8YnPwgHbJegFaUzokkErqT8JVe4zPL7GD3gy6aaZQERs3MF5KAGJQ1AL4MmhLWfmceyk6XXSqWaECh1YXC7aV')
28
- })
29
-
30
- it('2- no hash. should return null ', () => {
31
- const input = 'https://i.imgur.com/muESb0B.png'
32
-
33
- expect(extractPHash(input)).toBe(null)
34
- })
35
- })
36
-
37
- describe('Proxify image src', () => {
38
- it('1- should proxify image src', () => {
39
- const input = 'https://i.imgur.com/muESb0B.png'
40
- const expected = 'https://images.ecency.com/p/2bP4pJr4wVimqCWjYimXJe2cnCgnJdyHYxb4dfF6gmC.png?format=match&mode=fit'
41
-
42
- expect(proxifyImageSrc(input)).toBe(expected)
43
- })
44
-
45
- it('2- should not proxify if already proxified', () => {
46
- const input = 'https://images.ecency.com/0x0/https://i.imgur.com/muESb0B.png'
47
- const expected = 'https://images.ecency.com/p/2bP4pJr4wVimqCWjYimXJe2cnCgnJdyHYxb4dfF6gmC.png?format=match&mode=fit'
48
-
49
- expect(proxifyImageSrc(input)).toBe(expected)
50
- })
51
-
52
- it('3- set proxy base', () => {
53
- setProxyBase('https://images.hive.blog')
54
-
55
- const input = 'https://i.imgur.com/muESb0B.png'
56
- const expected = 'https://images.hive.blog/p/2bP4pJr4wVimqCWjYimXJe2cnCgnJdyHYxb4dfF6gmC?format=match&mode=fit'
57
-
58
- expect(proxifyImageSrc(input)).toBe(expected)
59
- })
60
-
61
- it('4- replace already proxified', () => {
62
- setProxyBase('https://images.ecency.com')
63
- const input = 'https://images.hive.blog/60x70/http://hivebuzz.me/@hiveonboard/upvotes.png?202008050233'
64
- const expected = 'https://images.ecency.com/60x70/http://hivebuzz.me/@hiveonboard/upvotes.png?202008050233'
65
-
66
- expect(proxifyImageSrc(input)).toBe(expected)
67
- })
68
-
69
- it('5- normal proxify for uploads', () => {
70
- setProxyBase('https://images.ecency.com')
71
- const input = 'https://images.hive.blog/DQmT7UTd6JTP3bB2fXzV6tv8u4cJ6fLijy2bUxatkLChzHD/IMG_6631.JPG'
72
- const expected = 'https://images.ecency.com/p/Zskj9C56UonZ32EJw6nMctrTQ6kTQ3swaDmbMFtRtMzyrHs9JdvWTXeiP6cW6a7F6pv2A4qkeHLiAPVtmfYMDf3iYbydFZ7e8iYY4MZP74TgyWo8WnJa.png?format=match&mode=fit'
73
-
74
- expect(proxifyImageSrc(input)).toBe(expected)
75
- })
76
- })
@@ -1,77 +0,0 @@
1
- import multihash from 'multihashes'
2
- import querystring from 'querystring'
3
-
4
- let proxyBase = 'https://images.ecency.com'
5
- let fileExtension = true
6
-
7
- export function setProxyBase(p: string): void {
8
- proxyBase = p
9
- fileExtension = proxyBase == 'https://images.ecency.com';
10
- }
11
-
12
- export function extractPHash(url: string): string | null {
13
- if (url.startsWith(`${proxyBase}/p/`)) {
14
- const [hash] = url.split('/p/')[1].split('?')
15
- return hash.replace(/.webp/,'').replace(/.png/,'')
16
- }
17
- return null
18
- }
19
-
20
- export function isValidUrl(url: string): boolean {
21
- try {
22
- return Boolean(new URL(url));
23
- }
24
- catch(e){
25
- return false;
26
- }
27
- }
28
-
29
- export function getLatestUrl(str: string): string {
30
- const [last] = [...str.replace(/https?:\/\//g, '\n$&').trim().split('\n')].reverse()
31
- return last
32
- }
33
-
34
- export function proxifyImageSrc(url?: string, width = 0, height = 0, format = 'match') {
35
- if (!url || typeof url !== 'string' || !isValidUrl(url)) {
36
- return ''
37
- }
38
-
39
- // skip images already proxified with images.hive.blog
40
- if (url.indexOf('https://images.hive.blog/') === 0 && url.indexOf('https://images.hive.blog/D') !== 0) {
41
- return url.replace('https://images.hive.blog', proxyBase)
42
- }
43
-
44
- if (url.indexOf('https://steemitimages.com/') === 0 && url.indexOf('https://steemitimages.com/D') !== 0) {
45
- return url.replace('https://steemitimages.com', proxyBase)
46
- }
47
-
48
- const realUrl = getLatestUrl(url)
49
- const pHash = extractPHash(realUrl)
50
-
51
- const options: Record<string, string | number> = {
52
- format,
53
- mode: 'fit',
54
- }
55
-
56
- if (width > 0) {
57
- options.width = width
58
- }
59
-
60
- if (height > 0) {
61
- options.height = height
62
- }
63
-
64
- const qs = querystring.stringify(options)
65
-
66
- if (pHash) {
67
- if (fileExtension) {
68
- return `${proxyBase}/p/${pHash}${format==='webp'?'.webp':'.png'}?${qs}`
69
- } else {
70
- return `${proxyBase}/p/${pHash}?${qs}`
71
- }
72
- }
73
-
74
- const b58url = multihash.toB58String(Buffer.from(realUrl.toString()))
75
-
76
- return `${proxyBase}/p/${b58url}${fileExtension ? format==='webp'?'.webp':'.png' : ''}?${qs}`
77
- }
@@ -1,15 +0,0 @@
1
- import { sanitizeHtml } from './methods/sanitize-html.method'
2
-
3
- describe('sanitizeHtml', () => {
4
- it('1- should allow video tag with src and controls', () => {
5
- const input = '<video src="https://example.com/video.mp4" controls></video>'
6
- const expected = '<video src="https://example.com/video.mp4" controls></video>'
7
- expect(sanitizeHtml(input)).toBe(expected)
8
- })
9
-
10
- it('2- should strip dangerous video src', () => {
11
- const input = "<video src=\"javascript:alert('XSS')\" controls></video>"
12
- const expected = '<video controls></video>'
13
- expect(sanitizeHtml(input)).toBe(expected)
14
- })
15
- })
@@ -1,12 +0,0 @@
1
- const fs = require('fs')
2
-
3
- export function getTestData(author: string, permlink: string): any {
4
- const fileName = `${author}____${permlink}.json`
5
- const readPath = `${__dirname}/json/${fileName}`
6
-
7
- const raw = fs.readFileSync(readPath, 'utf-8')
8
-
9
- return JSON.parse(raw)
10
- }
11
-
12
- export const foo = 'foo'