ibs-format 1.4.10 → 1.4.12

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 (3) hide show
  1. package/README.md +210 -210
  2. package/index.js +340 -320
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,210 +1,210 @@
1
- # Description
2
-
3
- Text formatting in Javascript. Detect the user-defined identifiers in the text and convert them into HTML tags like bold, italic, strike, and many more having XSS (Cross-site scripting) security with escaping functionality, also detect the links like URLs, email, and IP addresses and wrap them into Anchor tag `<a>` with also other user define formatting.
4
-
5
- ## Table of Contents
6
- - [Online Demo](#online-demo)
7
- - [Supported browsers](#browsers)
8
- - [Installation](#installation)
9
- - [Usage](#usage)
10
- - [Text Formatting](#text-formatting)
11
- - [Links Detecting](#links-detecting)
12
- - [Cross Site Scripting (XSS)](#cross-site-scripting-(xss))
13
- - [Format the text at run time using custom Pipe](#format-the-text-at-run-time-using-custom-pipe)
14
- - [Use the external 'ngx-linkifyjs' library for detecting the links](#use-the-external-'ngx-linkifyjs'-library-for-detecting-the-links)
15
- - [Precautions](#precautions)
16
-
17
- <a name="online-demo"/>
18
-
19
- # Online Demo
20
-
21
- <a href="https://stackblitz.com/edit/angular-ivy-up1fwx?file=src%2Fapp%2Fcustom-pipe.pipe.ts" target='_blank'>CLICK HERE</a>
22
-
23
- <a name="browsers"/>
24
-
25
- # Supported browsers
26
-
27
- Fully supported and tested, over Google Chrome, Microsoft Edge, Mozilla Firefox and Internet Explorer 11.
28
-
29
- <a name="installation"/>
30
-
31
- # Installation
32
-
33
- ```bash
34
- npm i ibs-format --save
35
- ```
36
-
37
- <a name="usage"/>
38
-
39
- # Usage
40
-
41
- <a name="text-formatting"/>
42
-
43
- ## Text Formatting
44
-
45
- ```js
46
- import { ibsFormat } from 'ibs-format';
47
- ```
48
-
49
- For formatting the function 'ibsFormat' needs two arguments.
50
- 1) the text with identifiers in the first argument, in the form of string.
51
- 2) tags and identifiers in the second argument, in the form of string array.
52
-
53
- ```js
54
- var myText = "Once upon a time, there was a *thristy* ~_crow_~."
55
- ```
56
-
57
- In the array, the tag symbols in the first index and their identifier in the second index.
58
-
59
- ```js
60
- var tagArray = [['b','*'],['i','_'],['strike','~'],["mark","!"]];
61
- ```
62
-
63
- * Here symbol, 'b' is using for 'bold', 'i' for 'italic', 'strike' for 'strike' and 'mark' for 'mark' tag with their respective Identifiers.
64
- * The user can use as many tags and their identifiers of his own choice.
65
- * Some special characters can't be used as identifiers for example, dollar sign '$'.
66
- * Now the function will look like.
67
-
68
- ```js
69
- myText = ibsFormat(myText, tagArray);
70
- ```
71
-
72
- ### The function will return the result with tags
73
-
74
- `Once upon a time, there was a <b>thristy</b> <strike><i>crow</i></strike>.`
75
-
76
-
77
- ### HTML
78
-
79
- `<p [innerHTML]="myText"></p>`
80
-
81
-
82
- ### The result will
83
-
84
- Once upon a time, there was a <b>thristy</b> <strike><i>crow</i></strike>.
85
-
86
- <a name="links-detecting"/>
87
-
88
- # Links Detecting
89
-
90
- For auto detecting links in to the text and converting them to HTML `<a>` tags, the function 'ibsFormat' needs three arguments
91
- * To enable auto detecting links create an object and set its 'detectLinks' property to true.
92
- * You can also specify the target of the links by creating a property 'target' in the object, it is optional with default value '_self'.
93
- * The value of 'target' property can be set to, '_blank', '_self', '_parent', '_top'.
94
- * Put the object in the third argument.
95
- Like:
96
-
97
- ```js
98
- var myText = "The *best* website for learning _JS_ is https://www.w3schools.com/ and my email is info@myemail.com."
99
-
100
- var tagArray = [['b','*'],['i','_'],['strike','~'],["mark","!"]];
101
-
102
- var obj = {detectLinks: true, target: '_blank'};
103
-
104
- myText = ibsFormat(myText, tagArray, obj);
105
- ```
106
-
107
- ### The function will return
108
-
109
- ````
110
- The <b>best</b> website for learning <i>JS</i> is <a href='https://www.w3schools.com/' target='_blank'>https://www.w3schools.com/</a>
111
- and my email is <a href='mailto:info@myemail.com' target='_blank'>info@myemail.com</a>.
112
- ````
113
-
114
- ### The result will
115
-
116
- The <b>best</b> website for learning <i>JS</i> is <a href='https://www.w3schools.com/' target='_blank'>https://www.w3schools.com/</a>
117
- and my email is <a href='mailto:info@myemail.com' target='_blank'>info@myemail.com</a>.
118
-
119
-
120
- ### In order to skip the text formatting set the second argument null, like:
121
-
122
- ```js
123
- myText = ibsFormat(myText, null, obj);
124
- ```
125
- <a name="cross-site-scripting-(xss)"/>
126
-
127
- # Cross Site Scripting (XSS).
128
-
129
- XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. In order to prevent those scripts, the
130
- client side tags are converted into nonexecutable through escaping. These security checks are enabled by default and it is recommended to
131
- keep them enabled, but in order to bypass these security checks place a forth argument in the function.
132
-
133
- ### In order to skip the XSS security checking:
134
-
135
- Place a JSON object in the forth argument and set it's value to false, if the forth argument is missing then it's value will be true by default.
136
-
137
- ```js
138
- myText = ibsFormat(myText, tagArray, obj, { allowXssEscaping : false });
139
- ```
140
-
141
- <a name="format-the-text-at-run-time-using-custom-pipe"/>
142
-
143
- # Format the text at run time using custom Pipe.
144
-
145
- In order to format the text at run time in HTML, create a custom pipe and use the function there.
146
-
147
- ### Create a custom pipe, 'custom-pipe.pipe.ts'.
148
-
149
- ```js
150
- import { Pipe, PipeTransform } from '@angular/core';
151
- import { ibsFormat } from "ibs-format";
152
-
153
- @Pipe({ name: 'ibsformat' })
154
- export class ibsformatPipe implements PipeTransform {
155
- transform(value: any, args?: any): any {
156
-
157
- value = ibsFormat(value, [["b", "*"], ["i", "_"], ["strike", "~"],["mark","!"]],{ detectLinks: true, target: "_blank" });
158
-
159
- return value;
160
- }
161
- }
162
- ```
163
-
164
- ### Make its entry in 'module.ts'.
165
-
166
- ```js
167
- import { ibsformatPipe } from './custom-pipe.pipe';
168
-
169
- // also add in declarations array
170
- @NgModule({
171
- declarations: [ AppComponent, ibsformatPipe ],
172
- })
173
- ```
174
-
175
- ### Now use the pipe directly in HTMl
176
-
177
- `<p [innerHTML]="myText | ibsformat"></p>`
178
-
179
- <a name="use-the-external-'ngx-linkifyjs'-library-for-detecting-the-links"/>
180
-
181
- # Use the external 'ngx-linkifyjs' library for detecting the links
182
-
183
- If you do not want to use the built-in 'detectLinks' functionality and want to use any other library for detecting the links, like 'ngx-linkifyjs', so after installing and configuring the 'ngx-linkifyjs' you can use the 'linkify' pipe before the 'ibsFormat' pipe,
184
- and set the library's 'detectLinks' and 'allowXssEscaping' properties to false.
185
-
186
- `<p [innerHTML]="myText | linkify | ibsformat"></p>`
187
-
188
- ```js
189
- value = ibsFormat(
190
- value,
191
- [["b", "*"], ["i", "_"], ["strike", "~"], ["mark", "!"]],
192
- { detectLinks: false, target: "_blank" },
193
- { allowXssEscaping: false }
194
- )
195
- ```
196
-
197
-
198
- For full example of custom pipe, see the live demo mention above.
199
-
200
- ### Feel free to report any bugs or improvements.
201
-
202
- <a name="precautions"/>
203
-
204
- # Precautions
205
-
206
- * Don't change the index positioning.
207
- * The function does not supports double or multiple identifiers rather than double asterisks '**'.
208
- * Don't use same identifiers for multiple tags.
209
- * Some special characters can't be used as identifiers for example, dollar sign '$'.
210
-
1
+ # Description
2
+
3
+ Detect the user-defined identifiers in the text and convert them into HTML tags like bold, italic, strike, and many more having XSS (Cross-site scripting) security with escaping functionality, also detect the links like URLs, email, and IP addresses and wrap them into Anchor tag `<a>`.
4
+
5
+ ## Table of Contents
6
+ - [Online Demo](#demo)
7
+ - [Supported browsers](#browsers)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Text Formatting](#text-formatting)
11
+ - [Links Detecting](#links-detecting)
12
+ - [Cross Site Scripting (XSS)](#cross-site-scripting)
13
+ - [Format the text at run time using custom Pipe](#pipe)
14
+ - [Use the external 'ngx-linkifyjs' library for detecting the links](#linkifyjs)
15
+ - [Precautions](#precautions)
16
+
17
+ <a name="demo"/>
18
+
19
+ # Online Demo
20
+
21
+ <a href="https://stackblitz.com/edit/angular-ivy-up1fwx?file=src%2Fapp%2Fcustom-pipe.pipe.ts" target='_blank'>CLICK HERE</a>
22
+
23
+ <a name="browsers"/>
24
+
25
+ # Supported browsers
26
+
27
+ Fully supported and tested, over Google Chrome, Microsoft Edge, Mozilla Firefox and Internet Explorer 11.
28
+
29
+ <a name="installation"/>
30
+
31
+ # Installation
32
+
33
+ ```bash
34
+ npm i ibs-format --save
35
+ ```
36
+
37
+ <a name="usage"/>
38
+
39
+ # Usage
40
+
41
+ <a name="text-formatting"/>
42
+
43
+ ## Text Formatting
44
+
45
+ ```js
46
+ import { ibsFormat } from 'ibs-format';
47
+ ```
48
+
49
+ For formatting the function 'ibsFormat' needs two arguments.
50
+ 1) the text with identifiers in the first argument, in the form of string.
51
+ 2) tags and identifiers in the second argument, in the form of string array.
52
+
53
+ ```js
54
+ var myText = "Once upon a time, there was a *thristy* ~_crow_~."
55
+ ```
56
+
57
+ In the array, the tag symbols in the first index and their identifier in the second index.
58
+
59
+ ```js
60
+ var tagArray = [['b','*'],['i','_'],['strike','~'],["mark","!"]];
61
+ ```
62
+
63
+ * Here symbol, 'b' is using for 'bold', 'i' for 'italic', 'strike' for 'strike' and 'mark' for 'mark' tag with their respective Identifiers.
64
+ * The user can use as many tags and their identifiers of his own choice.
65
+ * Some special characters can't be used as identifiers for example, dollar sign '$'.
66
+ * Now the function will look like.
67
+
68
+ ```js
69
+ myText = ibsFormat(myText, tagArray);
70
+ ```
71
+
72
+ ### The function will return the result with tags
73
+
74
+ `Once upon a time, there was a <b>thristy</b> <strike><i>crow</i></strike>.`
75
+
76
+
77
+ ### HTML
78
+
79
+ `<p [innerHTML]="myText"></p>`
80
+
81
+
82
+ ### The result will
83
+
84
+ Once upon a time, there was a <b>thristy</b> <strike><i>crow</i></strike>.
85
+
86
+ <a name="links-detecting"/>
87
+
88
+ # Links Detecting
89
+
90
+ For auto detecting links in to the text and converting them to HTML `<a>` tags, the function 'ibsFormat' needs three arguments
91
+ * To enable auto detecting links create an object and set its 'detectLinks' property to true.
92
+ * You can also specify the target of the links by creating a property 'target' in the object, it is optional with default value '_self'.
93
+ * The value of 'target' property can be set to, '_blank', '_self', '_parent', '_top'.
94
+ * Put the object in the third argument.
95
+ Like:
96
+
97
+ ```js
98
+ var myText = "The *best* website for learning _JS_ is https://www.w3schools.com/ and my email is info@myemail.com."
99
+
100
+ var tagArray = [['b','*'],['i','_'],['strike','~'],["mark","!"]];
101
+
102
+ var obj = {detectLinks: true, target: '_blank'};
103
+
104
+ myText = ibsFormat(myText, tagArray, obj);
105
+ ```
106
+
107
+ ### The function will return
108
+
109
+ ````
110
+ The <b>best</b> website for learning <i>JS</i> is <a href='https://www.w3schools.com/' target='_blank'>https://www.w3schools.com/</a>
111
+ and my email is <a href='mailto:info@myemail.com' target='_blank'>info@myemail.com</a>.
112
+ ````
113
+
114
+ ### The result will
115
+
116
+ The <b>best</b> website for learning <i>JS</i> is <a href='https://www.w3schools.com/' target='_blank'>https://www.w3schools.com/</a>
117
+ and my email is <a href='mailto:info@myemail.com' target='_blank'>info@myemail.com</a>.
118
+
119
+
120
+ ### In order to skip the text formatting set the second argument null, like:
121
+
122
+ ```js
123
+ myText = ibsFormat(myText, null, obj);
124
+ ```
125
+ <a name="cross-site-scripting"/>
126
+
127
+ # Cross Site Scripting (XSS).
128
+
129
+ XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. In order to prevent those scripts, the
130
+ client side tags are converted into nonexecutable through escaping. These security checks are enabled by default and it is recommended to
131
+ keep them enabled, but in order to bypass these security checks place a forth argument in the function.
132
+
133
+ ### In order to skip the XSS security checking:
134
+
135
+ Place a JSON object in the forth argument and set it's value to false, if the forth argument is missing then it's value will be true by default.
136
+
137
+ ```js
138
+ myText = ibsFormat(myText, tagArray, obj, { allowXssEscaping : false });
139
+ ```
140
+
141
+ <a name="pipe"/>
142
+
143
+ # Format the text at run time using custom Pipe.
144
+
145
+ In order to format the text at run time in HTML, create a custom pipe and use the function there.
146
+
147
+ ### Create a custom pipe, 'custom-pipe.pipe.ts'.
148
+
149
+ ```js
150
+ import { Pipe, PipeTransform } from '@angular/core';
151
+ import { ibsFormat } from "ibs-format";
152
+
153
+ @Pipe({ name: 'ibsformat' })
154
+ export class ibsformatPipe implements PipeTransform {
155
+ transform(value: any, args?: any): any {
156
+
157
+ value = ibsFormat(value, [["b", "*"], ["i", "_"], ["strike", "~"],["mark","!"]],{ detectLinks: true, target: "_blank" });
158
+
159
+ return value;
160
+ }
161
+ }
162
+ ```
163
+
164
+ ### Make its entry in 'module.ts'.
165
+
166
+ ```js
167
+ import { ibsformatPipe } from './custom-pipe.pipe';
168
+
169
+ // also add in declarations array
170
+ @NgModule({
171
+ declarations: [ AppComponent, ibsformatPipe ],
172
+ })
173
+ ```
174
+
175
+ ### Now use the pipe directly in HTMl
176
+
177
+ `<p [innerHTML]="myText | ibsformat"></p>`
178
+
179
+ <a name="linkifyjs"/>
180
+
181
+ # Use the external 'ngx-linkifyjs' library for detecting the links
182
+
183
+ If you do not want to use the built-in 'detectLinks' functionality and want to use any other library for detecting the links, like 'ngx-linkifyjs', so after installing and configuring the 'ngx-linkifyjs' you can use the 'linkify' pipe before the 'ibsFormat' pipe,
184
+ and set the library's 'detectLinks' and 'allowXssEscaping' properties to false.
185
+
186
+ `<p [innerHTML]="myText | linkify | ibsformat"></p>`
187
+
188
+ ```js
189
+ value = ibsFormat(
190
+ value,
191
+ [["b", "*"], ["i", "_"], ["strike", "~"], ["mark", "!"]],
192
+ { detectLinks: false, target: "_blank" },
193
+ { allowXssEscaping: false }
194
+ )
195
+ ```
196
+
197
+
198
+ For full example of custom pipe, see the live demo mention above.
199
+
200
+ ### Feel free to report any bugs or improvements.
201
+
202
+ <a name="precautions"/>
203
+
204
+ # Precautions
205
+
206
+ * Don't change the index positioning.
207
+ * The function does not supports double or multiple identifiers rather than double asterisks '**'.
208
+ * Don't use same identifiers for multiple tags.
209
+ * Some special characters can't be used as identifiers for example, dollar sign '$'.
210
+
package/index.js CHANGED
@@ -1,320 +1,340 @@
1
- function ibsFormat(value, arr, linky, escaping) {
2
- let output = null;
3
- escaping = escaping && escaping.allowXssEscaping == false ? false : true;
4
- if (value) {
5
- if (escaping) {
6
- value = value.replace(/</g, "&lt;");
7
- value = value.replace(/>/g, "&gt;");
8
- }
9
- value = value.replace(/\n/g, " <br> ");
10
- }
11
- if (value != "" && value != null && value != undefined && arr && arr.length > 0) {
12
- if (arr[0].constructor === Array) {
13
- arr.map(function (e) {
14
- e[2] = (e[0].length + 1).toString();
15
- if (e[1] == "*") {
16
- value = astericHandler(value, e[0], e[1], parseInt(e[2]), " ");
17
- } else if (e[1] == '**') {
18
- value = doubleAstericHandler(value, e[0], e[1], parseInt(e[2]), " ");
19
- } else {
20
- value = getFormat(value, e[0], e[1], parseInt(e[2]), " ");
21
- }
22
- });
23
- } else {
24
- arr[2] = (arr[0].length + 1).toString();
25
- if (arr[1] == "*") {
26
- value = astericHandler(value, arr[0], arr[1], parseInt(arr[2]), " ");
27
- } else if (arr[1] == '**') {
28
- value = doubleAstericHandler(value, arr[0], arr[1], parseInt(arr[2]), " ");
29
- } else {
30
- value = getFormat(value, arr[0], arr[1], parseInt(arr[2]), " ");
31
- }
32
- }
33
- output = value;
34
- } else {
35
- output = null;
36
- }
37
- if (value != "" && value != null && value != undefined && linky && linky.detectLinks == true) {
38
- let targ;
39
- if (linky.target != null && linky.target != "") {
40
- targ = linky.target;
41
- } else {
42
- targ = "_self";
43
- }
44
- output = linkfy(value, targ);
45
- }
46
- if (output) {
47
- output = output.replace(/ <br> /g, "<br>");
48
- }
49
- return output ? output.trim() : "";
50
- }
51
-
52
- function doubleAstericHandler(text, tag, iden, trim, space) {
53
- let box = [];
54
- let arr = [];
55
- let finalText = "";
56
- let a = text.split(" ");
57
- let singleStericCounter = 0;
58
- a.forEach(function (e) {
59
- if (
60
- e.length > 1 &&
61
- e.includes(iden) &&
62
- (e.match(/\*\*/g) || []).length == 1
63
- ) {
64
- ++singleStericCounter;
65
- }
66
- });
67
- singleStericCounter = singleStericCounter - 1;
68
- let flag = "1";
69
- let loopCounter = 0;
70
- a.forEach(function (e) {
71
- if (
72
- e.length > 1 &&
73
- e.includes(iden) &&
74
- (e.match(/\*\*/g) || []).length == 1
75
- ) {
76
- if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
77
- } else {
78
- if (flag == "1") {
79
- e = e.replace(/\*\*/g, "<" + tag + ">");
80
- flag = "2";
81
- } else {
82
- e = e.replace(/\*\*/g, "</" + tag + ">");
83
- flag = "1";
84
- }
85
- ++loopCounter;
86
- }
87
- }
88
- if (e.length > 1 && (e.match(/\*\*/g) || []).length > 1) {
89
- const n = (e.match(/\*\*/g) || []).length;
90
-
91
- for (let i = 0; i < n; i++) {
92
- if (i == 0) {
93
- box.push(e.indexOf(iden, i));
94
- } else {
95
- let len = box.length - 1;
96
- let v = box[len];
97
- v = v + 1;
98
- box.push(e.indexOf(iden, v));
99
- }
100
- }
101
- e = e.replace('**', "<" + tag + ">");
102
- e = e.replace('**', "</" + tag + ">");
103
- box = [];
104
- }
105
- arr.push(e);
106
- });
107
- arr.forEach(function (e) {
108
- if (e == "") {
109
- finalText = finalText + space;
110
- } else {
111
- finalText = finalText + e + space;
112
- }
113
- });
114
-
115
- return finalText;
116
- }
117
-
118
-
119
- function astericHandler(text, tag, iden, trim, space) {
120
- let box = [];
121
- let arr = [];
122
- let finalText = "";
123
- let a = text.split(" ");
124
- let singleStericCounter = 0;
125
- a.forEach(function (e) {
126
- if (
127
- e.length > 1 &&
128
- e.includes(iden) &&
129
- (e.match(/\x2a/g) || []).length == 1
130
- ) {
131
- ++singleStericCounter;
132
- }
133
- });
134
- singleStericCounter = singleStericCounter - 1;
135
- let flag = "1";
136
- let loopCounter = 0;
137
- a.forEach(function (e) {
138
- if (
139
- e.length > 1 &&
140
- e.includes(iden) &&
141
- (e.match(/\x2a/g) || []).length == 1
142
- ) {
143
- if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
144
- } else {
145
- if (flag == "1") {
146
- e = e.replace(/\x2a/g, "<" + tag + ">");
147
- flag = "2";
148
- } else {
149
- e = e.replace(/\x2a/g, "</" + tag + ">");
150
- flag = "1";
151
- }
152
- ++loopCounter;
153
- }
154
- }
155
-
156
- if (e.length > 1 && (e.match(/\x2a/g) || []).length > 1) {
157
- const n = (e.match(/\x2a/g) || []).length;
158
-
159
- for (let i = 0; i < n; i++) {
160
- if (i == 0) {
161
- box.push(e.indexOf(iden, i));
162
- } else {
163
- let len = box.length - 1;
164
- let v = box[len];
165
- v = v + 1;
166
- box.push(e.indexOf(iden, v));
167
- }
168
- }
169
- let firstIndex = box[0];
170
- let lastIndex = box[box.length - 1];
171
- e = replaceChar(e, "<" + tag + ">", firstIndex);
172
- e = replaceChar(e, "</" + tag + ">", lastIndex + trim);
173
- box = [];
174
- }
175
-
176
- arr.push(e);
177
- });
178
- arr.forEach(function (e) {
179
- if (e == "") {
180
- finalText = finalText + space;
181
- } else {
182
- finalText = finalText + e + space;
183
- }
184
- });
185
- function replaceChar(origString, replaceChar, index) {
186
- let firstPart = origString.substr(0, index);
187
- let lastPart = origString.substr(index + 1);
188
-
189
- let newString = firstPart + replaceChar + lastPart;
190
- return newString;
191
- }
192
- return finalText;
193
- }
194
-
195
-
196
- function getFormat(text, tag, iden, trim, space) {
197
- let box = [];
198
- let arr = [];
199
- let finalText = "";
200
- let a = text.split(" ");
201
- let singleStericCounter = 0;
202
- a.forEach(function (e) {
203
- if (
204
- e.length > 1 &&
205
- e.includes(iden) &&
206
- (e.match(new RegExp(iden, "g")) || []).length == 1
207
- ) {
208
- ++singleStericCounter;
209
- }
210
- });
211
- singleStericCounter = singleStericCounter - 1;
212
- let flag = "1";
213
- let loopCounter = 0;
214
- a.forEach(function (e) {
215
- if (
216
- e.length > 1 &&
217
- e.includes(iden) &&
218
- (e.match(new RegExp(iden, "g")) || []).length == 1
219
- ) {
220
- if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
221
- } else {
222
- if (flag == "1") {
223
- e = e.replace(new RegExp(iden, "g"), "<" + tag + ">");
224
- flag = "2";
225
- } else {
226
- e = e.replace(new RegExp(iden, "g"), "</" + tag + ">");
227
- flag = "1";
228
- }
229
- ++loopCounter;
230
- }
231
- }
232
- if (e.length > 1 && (e.match(new RegExp(iden, "g")) || []).length > 1) {
233
- const n = (e.match(new RegExp(iden, "g")) || []).length;
234
-
235
- for (let i = 0; i < n; i++) {
236
- if (i == 0) {
237
- box.push(e.indexOf(iden, i));
238
- } else {
239
- let len = box.length - 1;
240
- let v = box[len];
241
- v = v + 1;
242
- box.push(e.indexOf(iden, v));
243
- }
244
- }
245
- let firstIndex = box[0];
246
- let lastIndex = box[box.length - 1];
247
-
248
- e = replaceChar(e, "<" + tag + ">", firstIndex);
249
- e = replaceChar(e, "</" + tag + ">", lastIndex + trim);
250
-
251
- box = [];
252
- }
253
- arr.push(e);
254
- });
255
-
256
- arr.forEach(function (e) {
257
- if (e == "") {
258
- finalText = finalText + space;
259
- } else {
260
- finalText = finalText + e + space;
261
- }
262
- });
263
- function replaceChar(origString, replaceChar, index) {
264
- let firstPart = origString.substr(0, index);
265
- let lastPart = origString.substr(index + 1);
266
-
267
- let newString = firstPart + replaceChar + lastPart;
268
- return newString;
269
- }
270
- return finalText;
271
- }
272
-
273
- function linkfy(text, target) {
274
- let strictUrlExpression = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/gi;
275
- let looseUrlExpression = /^https?\:\/\/[^\/\s]+(\/.*)?$/;
276
- let ip4Expression = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[0-9]+\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/=]*)?/g;
277
- let emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
278
- let httpVerify = /^((http|https|ftp):\/\/)/;
279
- let strictUrlRegex = new RegExp(strictUrlExpression);
280
- let looseUrlRegex = new RegExp(looseUrlExpression);
281
- let ip4Regex = new RegExp(ip4Expression);
282
- let emailRegexx = new RegExp(emailRegex);
283
- let a = text.split(" ");
284
- let finalText = "";
285
- a.map(function (part, index) {
286
- if (part != " " && part != "" && part != undefined && part != "<br>") {
287
- if (part.match(emailRegexx)) {
288
- let ref = part;
289
- ref = ref.replace(/<[^>]*>?/gm, "");
290
- a[index] = "<a href='mailto:" + ref + "' target='" + target + "'>" + part + "</a>";
291
- } else if (part.match(looseUrlRegex)) {
292
- let ref = part;
293
- ref = ref.replace(/<[^>]*>?/gm, "");
294
- a[index] = "<a href='" + ref + "' target='" + target + "'>" + part + "</a>";
295
- } else if (part.match(strictUrlRegex)) {
296
- let ref = part;
297
- ref = ref.replace(/<[^>]*>?/gm, "");
298
- if (ref.match(httpVerify)) {
299
- a[index] = "<a href='" + ref + "' target='" + target + "'>" + part + "</a>";
300
- } else {
301
- a[index] = "<a href='http://" + ref + "' target='" + target + "'>" + part + "</a>";
302
- }
303
- } else if (part.match(ip4Regex)) {
304
- let ref = part;
305
- ref = ref.replace(/<[^>]*>?/gm, "");
306
- a[index] = "<a href='http://" + ref + "' target='" + target + "'>" + part + "</a>";
307
- }
308
- }
309
- });
310
- a.forEach(function (e) {
311
- if (e == "") {
312
- finalText = finalText + " ";
313
- } else {
314
- finalText = finalText + e + " ";
315
- }
316
- });
317
- return finalText;
318
- }
319
-
320
- module.exports.ibsFormat = ibsFormat;
1
+ function ibsFormat(value, arr, linky, escaping) {
2
+ originalArr = JSON.parse(JSON.stringify(arr));
3
+ let output = null;
4
+ escaping = escaping && escaping.allowXssEscaping == false ? false : true;
5
+ if (value) {
6
+ if (escaping) {
7
+ value = value.replace(/</g, "&lt;");
8
+ value = value.replace(/>/g, "&gt;");
9
+ }
10
+ value = value.replace(/\n/g, " <br> ");
11
+ }
12
+ if (value != "" && value != null && value != undefined && arr && arr.length > 0) {
13
+ if (arr[0].constructor === Array) {
14
+ arr.map(function (e) {
15
+ e[2] = (e[0].length + 1).toString();
16
+ if (e[1] == "*") {
17
+ value = astericHandler(value, e[0], e[1], parseInt(e[2]), " ");
18
+ } else if (e[1] == '**') {
19
+ value = doubleAstericHandler(value, e[0], e[1], parseInt(e[2]), " ");
20
+ } else {
21
+ value = getFormat(value, e[0], e[1], parseInt(e[2]), " ");
22
+ }
23
+ });
24
+ } else {
25
+ arr[2] = (arr[0].length + 1).toString();
26
+ if (arr[1] == "*") {
27
+ value = astericHandler(value, arr[0], arr[1], parseInt(arr[2]), " ");
28
+ } else if (arr[1] == '**') {
29
+ value = doubleAstericHandler(value, arr[0], arr[1], parseInt(arr[2]), " ");
30
+ } else {
31
+ value = getFormat(value, arr[0], arr[1], parseInt(arr[2]), " ");
32
+ }
33
+ }
34
+ output = value;
35
+ } else {
36
+ output = null;
37
+ }
38
+ if (value != "" && value != null && value != undefined && linky && linky.detectLinks == true) {
39
+ let targ;
40
+ if (linky.target != null && linky.target != "") {
41
+ targ = linky.target;
42
+ } else {
43
+ targ = "_self";
44
+ }
45
+ output = linkfy(value, targ);
46
+ }
47
+ if (output) {
48
+ output = output.replace(/ <br> /g, "<br>");
49
+ }
50
+ output = transformContentInsideEm(output);
51
+ return output ? output.trim() : "";
52
+ }
53
+
54
+ function transformContentInsideEm(inputString) {
55
+ // Define the tag-to-replacement mapping as an array with 3 elements: [tag, symbol, priority]
56
+
57
+ // Match the <em> tag and its content
58
+ return inputString.replace(/<em>(.*?)<\/em>/gs, function(match, content) {
59
+ // Perform tag-to-symbol replacement inside the <em> content
60
+ originalArr.forEach(([tag, replacement]) => {
61
+ // Replace opening and closing tags of each mapped tag with the symbol
62
+ const openTag = new RegExp(`<${tag}>`, 'g');
63
+ const closeTag = new RegExp(`</${tag}>`, 'g');
64
+ content = content.replace(openTag, replacement).replace(closeTag, replacement);
65
+ });
66
+
67
+ // Return the modified <em> tag with converted content
68
+ return `<em>${content}</em>`;
69
+ });
70
+ }
71
+
72
+ function doubleAstericHandler(text, tag, iden, trim, space) {
73
+ let box = [];
74
+ let arr = [];
75
+ let finalText = "";
76
+ let a = text.split(" ");
77
+ let singleStericCounter = 0;
78
+ a.forEach(function (e) {
79
+ if (
80
+ e.length > 1 &&
81
+ e.includes(iden) &&
82
+ (e.match(/\*\*/g) || []).length == 1
83
+ ) {
84
+ ++singleStericCounter;
85
+ }
86
+ });
87
+ singleStericCounter = singleStericCounter - 1;
88
+ let flag = "1";
89
+ let loopCounter = 0;
90
+ a.forEach(function (e) {
91
+ if (
92
+ e.length > 1 &&
93
+ e.includes(iden) &&
94
+ (e.match(/\*\*/g) || []).length == 1
95
+ ) {
96
+ if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
97
+ } else {
98
+ if (flag == "1") {
99
+ e = e.replace(/\*\*/g, "<" + tag + ">");
100
+ flag = "2";
101
+ } else {
102
+ e = e.replace(/\*\*/g, "</" + tag + ">");
103
+ flag = "1";
104
+ }
105
+ ++loopCounter;
106
+ }
107
+ }
108
+ if (e.length > 1 && (e.match(/\*\*/g) || []).length > 1) {
109
+ const n = (e.match(/\*\*/g) || []).length;
110
+
111
+ for (let i = 0; i < n; i++) {
112
+ if (i == 0) {
113
+ box.push(e.indexOf(iden, i));
114
+ } else {
115
+ let len = box.length - 1;
116
+ let v = box[len];
117
+ v = v + 1;
118
+ box.push(e.indexOf(iden, v));
119
+ }
120
+ }
121
+ e = e.replace('**', "<" + tag + ">");
122
+ e = e.replace('**', "</" + tag + ">");
123
+ box = [];
124
+ }
125
+ arr.push(e);
126
+ });
127
+ arr.forEach(function (e) {
128
+ if (e == "") {
129
+ finalText = finalText + space;
130
+ } else {
131
+ finalText = finalText + e + space;
132
+ }
133
+ });
134
+
135
+ return finalText;
136
+ }
137
+
138
+
139
+ function astericHandler(text, tag, iden, trim, space) {
140
+ let box = [];
141
+ let arr = [];
142
+ let finalText = "";
143
+ let a = text.split(" ");
144
+ let singleStericCounter = 0;
145
+ a.forEach(function (e) {
146
+ if (
147
+ e.length > 1 &&
148
+ e.includes(iden) &&
149
+ (e.match(/\x2a/g) || []).length == 1
150
+ ) {
151
+ ++singleStericCounter;
152
+ }
153
+ });
154
+ singleStericCounter = singleStericCounter - 1;
155
+ let flag = "1";
156
+ let loopCounter = 0;
157
+ a.forEach(function (e) {
158
+ if (
159
+ e.length > 1 &&
160
+ e.includes(iden) &&
161
+ (e.match(/\x2a/g) || []).length == 1
162
+ ) {
163
+ if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
164
+ } else {
165
+ if (flag == "1") {
166
+ e = e.replace(/\x2a/g, "<" + tag + ">");
167
+ flag = "2";
168
+ } else {
169
+ e = e.replace(/\x2a/g, "</" + tag + ">");
170
+ flag = "1";
171
+ }
172
+ ++loopCounter;
173
+ }
174
+ }
175
+
176
+ if (e.length > 1 && (e.match(/\x2a/g) || []).length > 1) {
177
+ const n = (e.match(/\x2a/g) || []).length;
178
+
179
+ for (let i = 0; i < n; i++) {
180
+ if (i == 0) {
181
+ box.push(e.indexOf(iden, i));
182
+ } else {
183
+ let len = box.length - 1;
184
+ let v = box[len];
185
+ v = v + 1;
186
+ box.push(e.indexOf(iden, v));
187
+ }
188
+ }
189
+ let firstIndex = box[0];
190
+ let lastIndex = box[box.length - 1];
191
+ e = replaceChar(e, "<" + tag + ">", firstIndex);
192
+ e = replaceChar(e, "</" + tag + ">", lastIndex + trim);
193
+ box = [];
194
+ }
195
+
196
+ arr.push(e);
197
+ });
198
+ arr.forEach(function (e) {
199
+ if (e == "") {
200
+ finalText = finalText + space;
201
+ } else {
202
+ finalText = finalText + e + space;
203
+ }
204
+ });
205
+ function replaceChar(origString, replaceChar, index) {
206
+ let firstPart = origString.substr(0, index);
207
+ let lastPart = origString.substr(index + 1);
208
+
209
+ let newString = firstPart + replaceChar + lastPart;
210
+ return newString;
211
+ }
212
+ return finalText;
213
+ }
214
+
215
+
216
+ function getFormat(text, tag, iden, trim, space) {
217
+ let box = [];
218
+ let arr = [];
219
+ let finalText = "";
220
+ let a = text.split(" ");
221
+ let singleStericCounter = 0;
222
+ a.forEach(function (e) {
223
+ if (
224
+ e.length > 1 &&
225
+ e.includes(iden) &&
226
+ (e.match(new RegExp(iden, "g")) || []).length == 1
227
+ ) {
228
+ ++singleStericCounter;
229
+ }
230
+ });
231
+ singleStericCounter = singleStericCounter - 1;
232
+ let flag = "1";
233
+ let loopCounter = 0;
234
+ a.forEach(function (e) {
235
+ if (
236
+ e.length > 1 &&
237
+ e.includes(iden) &&
238
+ (e.match(new RegExp(iden, "g")) || []).length == 1
239
+ ) {
240
+ if (loopCounter == singleStericCounter && loopCounter % 2 == 0) {
241
+ } else {
242
+ if (flag == "1") {
243
+ e = e.replace(new RegExp(iden, "g"), "<" + tag + ">");
244
+ flag = "2";
245
+ } else {
246
+ e = e.replace(new RegExp(iden, "g"), "</" + tag + ">");
247
+ flag = "1";
248
+ }
249
+ ++loopCounter;
250
+ }
251
+ }
252
+ if (e.length > 1 && (e.match(new RegExp(iden, "g")) || []).length > 1) {
253
+ const n = (e.match(new RegExp(iden, "g")) || []).length;
254
+
255
+ for (let i = 0; i < n; i++) {
256
+ if (i == 0) {
257
+ box.push(e.indexOf(iden, i));
258
+ } else {
259
+ let len = box.length - 1;
260
+ let v = box[len];
261
+ v = v + 1;
262
+ box.push(e.indexOf(iden, v));
263
+ }
264
+ }
265
+ let firstIndex = box[0];
266
+ let lastIndex = box[box.length - 1];
267
+
268
+ e = replaceChar(e, "<" + tag + ">", firstIndex);
269
+ e = replaceChar(e, "</" + tag + ">", lastIndex + trim);
270
+
271
+ box = [];
272
+ }
273
+ arr.push(e);
274
+ });
275
+
276
+ arr.forEach(function (e) {
277
+ if (e == "") {
278
+ finalText = finalText + space;
279
+ } else {
280
+ finalText = finalText + e + space;
281
+ }
282
+ });
283
+ function replaceChar(origString, replaceChar, index) {
284
+ let firstPart = origString.substr(0, index);
285
+ let lastPart = origString.substr(index + 1);
286
+
287
+ let newString = firstPart + replaceChar + lastPart;
288
+ return newString;
289
+ }
290
+ return finalText;
291
+ }
292
+
293
+ function linkfy(text, target) {
294
+ let strictUrlExpression = /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/gi;
295
+ let looseUrlExpression = /^https?\:\/\/[^\/\s]+(\/.*)?$/;
296
+ let ip4Expression = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]):[0-9]+\b(\/[-a-zA-Z0-9@:%_\+.~#?&\/=]*)?/g;
297
+ let emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
298
+ let httpVerify = /^((http|https|ftp):\/\/)/;
299
+ let strictUrlRegex = new RegExp(strictUrlExpression);
300
+ let looseUrlRegex = new RegExp(looseUrlExpression);
301
+ let ip4Regex = new RegExp(ip4Expression);
302
+ let emailRegexx = new RegExp(emailRegex);
303
+ let a = text.split(" ");
304
+ let finalText = "";
305
+ a.map(function (part, index) {
306
+ if (part != " " && part != "" && part != undefined && part != "<br>") {
307
+ if (part.match(emailRegexx)) {
308
+ let ref = part;
309
+ ref = ref.replace(/<[^>]*>?/gm, "");
310
+ a[index] = "<a href='mailto:" + ref + "' target='" + target + "'>" + part + "</a>";
311
+ } else if (part.match(looseUrlRegex)) {
312
+ let ref = part;
313
+ ref = ref.replace(/<[^>]*>?/gm, "");
314
+ a[index] = "<a href='" + ref + "' target='" + target + "'>" + part + "</a>";
315
+ } else if (part.match(strictUrlRegex)) {
316
+ let ref = part;
317
+ ref = ref.replace(/<[^>]*>?/gm, "");
318
+ if (ref.match(httpVerify)) {
319
+ a[index] = "<a href='" + ref + "' target='" + target + "'>" + part + "</a>";
320
+ } else {
321
+ a[index] = "<a href='http://" + ref + "' target='" + target + "'>" + part + "</a>";
322
+ }
323
+ } else if (part.match(ip4Regex)) {
324
+ let ref = part;
325
+ ref = ref.replace(/<[^>]*>?/gm, "");
326
+ a[index] = "<a href='http://" + ref + "' target='" + target + "'>" + part + "</a>";
327
+ }
328
+ }
329
+ });
330
+ a.forEach(function (e) {
331
+ if (e == "") {
332
+ finalText = finalText + " ";
333
+ } else {
334
+ finalText = finalText + e + " ";
335
+ }
336
+ });
337
+ return finalText;
338
+ }
339
+
340
+ module.exports.ibsFormat = ibsFormat;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ibs-format",
3
- "version": "1.4.10",
3
+ "version": "1.4.12",
4
4
  "description": "Detect the user-defined identifiers in the text and convert them into HTML tags like bold, italic, strike, and many more having XSS (Cross-site scripting) security with escaping functionality, also detect the links like URLs, email, and IP addresses and wrap them into Anchor tag `<a>`.",
5
5
  "main": "index.js",
6
6
  "scripts": {