@salesforcedevs/dx-components 0.58.0 → 0.59.0-alpha.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.
- package/package.json +2 -3
- package/src/assets/svg/subscribe-background-left.svg +14 -0
- package/src/assets/svg/subscribe-background-right.svg +11 -0
- package/src/modules/dx/codeBlock/codeBlock.ts +13 -0
- package/src/modules/dxUtils/codeLanguageDetector/codeLanguageDetector.js +439 -0
- package/LICENSE +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@salesforcedevs/dx-components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.59.0-alpha.1",
|
|
4
4
|
"description": "DX Lightning web components",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -24,6 +24,5 @@
|
|
|
24
24
|
"@types/debounce": "^1.2.0",
|
|
25
25
|
"@types/lodash.get": "^4.4.6",
|
|
26
26
|
"@types/vimeo__player": "^2.16.2"
|
|
27
|
-
}
|
|
28
|
-
"gitHead": "88a4d6c7d2387c527c47a5e87c0c00820436b091"
|
|
27
|
+
}
|
|
29
28
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<svg width="45px" height="34px" viewBox="0 0 45 34" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
+
<title>blog-cta-graphic-left</title>
|
|
4
|
+
<g id="Slack-App-CTA" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
|
|
5
|
+
<g id="Blog---v1" transform="translate(-424.000000, -2216.000000)" stroke-width="2">
|
|
6
|
+
<g id="CTA-signup" transform="translate(52.000000, 2169.000000)">
|
|
7
|
+
<g id="blog-cta-graphic-left" transform="translate(373.000000, 47.500000)">
|
|
8
|
+
<circle id="Oval-Copy-12" stroke="#90D1FF" cx="39.5" cy="4.5" r="3.5"></circle>
|
|
9
|
+
<path d="M7.42619649,16.2472709 L5.80463043,20.6293714 C5.68941141,20.9429183 5.44197383,21.1894114 5.12842699,21.3046304 L0.747270909,22.9271409 C-0.249090303,23.295464 -0.249090303,24.704536 0.747270909,25.0738035 L5.12842699,26.6953696 C5.44197383,26.8105886 5.68941141,27.0570817 5.80463043,27.3706286 L7.42619649,31.7527291 C7.79546401,32.7490903 9.20453599,32.7490903 9.57285909,31.7527291 L11.1944252,27.3706286 C11.3096442,27.0570817 11.5570817,26.8105886 11.8706286,26.6953696 L16.2527291,25.0738035 C17.2481459,24.704536 17.2481459,23.295464 16.2527291,22.9271409 L11.8706286,21.3046304 C11.5570817,21.1894114 11.3096442,20.9429183 11.1944252,20.6293714 L9.57285909,16.2472709 C9.20453599,15.2509097 7.79546401,15.2509097 7.42619649,16.2472709 Z" id="Stroke-7-Copy-2" stroke="#8FD0FE" stroke-linecap="round" stroke-linejoin="round"></path>
|
|
10
|
+
</g>
|
|
11
|
+
</g>
|
|
12
|
+
</g>
|
|
13
|
+
</g>
|
|
14
|
+
</svg>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<svg width="23px" height="22px" viewBox="0 0 23 22" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
3
|
+
<title>blog-cta-graphic-right</title>
|
|
4
|
+
<g id="Slack-App-CTA" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round">
|
|
5
|
+
<g id="Blog---v1" transform="translate(-936.000000, -2378.000000)" stroke="#8FD0FE" stroke-width="2">
|
|
6
|
+
<g id="CTA-signup" transform="translate(52.000000, 2169.000000)">
|
|
7
|
+
<path d="M894.236764,210.879142 L892.329039,216.034555 C892.193488,216.403433 891.902385,216.693425 891.533506,216.828977 L886.379205,218.737813 C885.207015,219.171134 885.207015,220.828866 886.379205,221.263298 L891.533506,223.171023 C891.902385,223.306575 892.193488,223.596567 892.329039,223.965445 L894.236764,229.120858 C894.671197,230.293047 896.328928,230.293047 896.76225,229.120858 L898.669974,223.965445 C898.805526,223.596567 899.096629,223.306575 899.465508,223.171023 L904.62092,221.263298 C905.791999,220.828866 905.791999,219.171134 904.62092,218.737813 L899.465508,216.828977 C899.096629,216.693425 898.805526,216.403433 898.669974,216.034555 L896.76225,210.879142 C896.328928,209.706953 894.671197,209.706953 894.236764,210.879142 Z" id="blog-cta-graphic-right"></path>
|
|
8
|
+
</g>
|
|
9
|
+
</g>
|
|
10
|
+
</g>
|
|
11
|
+
</svg>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LightningElement, api, wire, track } from "lwc";
|
|
2
2
|
import { CodeBlockTheme, CodeBlockLanguage } from "typings/custom";
|
|
3
3
|
import cx from "classnames";
|
|
4
|
+
import { detect } from "dxUtils/codeLanguageDetector";
|
|
4
5
|
import Prism from "dxUtils/prismjs";
|
|
5
6
|
import {
|
|
6
7
|
getLocalStorageData,
|
|
@@ -212,6 +213,18 @@ export default class CodeBlock extends LightningElement {
|
|
|
212
213
|
) || this.allLanguages[0];
|
|
213
214
|
this.selectedLanguageLabel = this.selectedLanguage.label;
|
|
214
215
|
this.selectedLanguageId = this.selectedLanguage.id;
|
|
216
|
+
|
|
217
|
+
// Auto detect code language if it isn't specified
|
|
218
|
+
if (!this.language) {
|
|
219
|
+
const detectedLanguage = detect(this.codeBlock);
|
|
220
|
+
this.selectedLanguage = {
|
|
221
|
+
label: detectedLanguage,
|
|
222
|
+
id: detectedLanguage.toLowerCase()
|
|
223
|
+
};
|
|
224
|
+
this.selectedLanguageLabel = detectedLanguage;
|
|
225
|
+
this.selectedLanguageId = detectedLanguage.toLowerCase();
|
|
226
|
+
}
|
|
227
|
+
|
|
215
228
|
this.formatCodeBlock();
|
|
216
229
|
this._codeBlockRendered = true;
|
|
217
230
|
}
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
const LANGUAGES = {
|
|
2
|
+
JavaScript: [
|
|
3
|
+
// undefined keyword
|
|
4
|
+
{ pattern: /undefined/g, points: 2 },
|
|
5
|
+
// console.log('ayy lmao')
|
|
6
|
+
{ pattern: /console\.log( )*\(/, points: 2 },
|
|
7
|
+
// Variable declaration
|
|
8
|
+
{ pattern: /(var|const|let)( )+\w+( )*=?/, points: 2 },
|
|
9
|
+
// Array/Object declaration
|
|
10
|
+
{ pattern: /(('|").+('|")( )*|\w+):( )*[{[]/, points: 2 },
|
|
11
|
+
// === operator
|
|
12
|
+
{ pattern: /===/g, points: 1 },
|
|
13
|
+
// !== operator
|
|
14
|
+
{ pattern: /!==/g, points: 1 },
|
|
15
|
+
// Function definition
|
|
16
|
+
{
|
|
17
|
+
pattern: /function\*?(( )+[$\w]+( )*\(.*\)|( )*\(.*\))/g,
|
|
18
|
+
points: 1
|
|
19
|
+
},
|
|
20
|
+
// null keyword
|
|
21
|
+
{ pattern: /null/g, points: 1 },
|
|
22
|
+
// lambda expression
|
|
23
|
+
{ pattern: /\(.*\)( )*=>( )*.+/, points: 1 },
|
|
24
|
+
// (else )if statement
|
|
25
|
+
{ pattern: /(else )?if( )+\(.+\)/, points: 1 },
|
|
26
|
+
// while loop
|
|
27
|
+
{ pattern: /while( )+\(.+\)/, points: 1 },
|
|
28
|
+
// C style variable declaration.
|
|
29
|
+
{
|
|
30
|
+
pattern: /(^|\s)(char|long|int|float|double)( )+\w+( )*=?/,
|
|
31
|
+
points: -1
|
|
32
|
+
},
|
|
33
|
+
// pointer
|
|
34
|
+
{ pattern: /(\w+)( )*\*( )*\w+/, points: -1 },
|
|
35
|
+
// HTML <script> tag
|
|
36
|
+
{
|
|
37
|
+
pattern: /<(\/)?script( type=('|")text\/javascript('|"))?>/,
|
|
38
|
+
points: -50
|
|
39
|
+
},
|
|
40
|
+
// ES6 import / export
|
|
41
|
+
{
|
|
42
|
+
pattern:
|
|
43
|
+
/(import|export(\s+)default)\s+({\s+[\w\s,]+\s+}|\w+)\s+from\s/,
|
|
44
|
+
points: 2
|
|
45
|
+
},
|
|
46
|
+
// ES6 arrow function
|
|
47
|
+
{ pattern: /\([^()]{0,}\)\s+=>(\s+{)?/, points: 3 }
|
|
48
|
+
// () => {}
|
|
49
|
+
// (a) => {}
|
|
50
|
+
// (a, b) => {}
|
|
51
|
+
// ({ a, b}) => {}
|
|
52
|
+
// ([ a, b ]) => {}
|
|
53
|
+
],
|
|
54
|
+
|
|
55
|
+
C: [
|
|
56
|
+
// Primitive variable declaration.
|
|
57
|
+
{ pattern: /(char|long|int|float|double)( )+\w+( )*=?/, points: 2 },
|
|
58
|
+
// malloc function call
|
|
59
|
+
{ pattern: /malloc\(.+\)/, points: 2 },
|
|
60
|
+
// #include <whatever.h>
|
|
61
|
+
{ pattern: /#include (<|")\w+\.h(>|")/, points: 2, nearTop: true },
|
|
62
|
+
// pointer
|
|
63
|
+
{ pattern: /(\w+)( )*\*( )*\w+/, points: 2 },
|
|
64
|
+
// Variable declaration and/or initialisation.
|
|
65
|
+
{ pattern: /(\w+)( )+\w+(;|( )*=)/, points: 1 },
|
|
66
|
+
// Array declaration.
|
|
67
|
+
{ pattern: /(\w+)( )+\w+\[.+\]/, points: 1 },
|
|
68
|
+
// #define macro
|
|
69
|
+
{ pattern: /#define( )+.+/, points: 1 },
|
|
70
|
+
// NULL constant
|
|
71
|
+
{ pattern: /NULL/, points: 1 },
|
|
72
|
+
// void keyword
|
|
73
|
+
{ pattern: /void/g, points: 1 },
|
|
74
|
+
// (else )if statement
|
|
75
|
+
{ pattern: /(else )?if( )*\(.+\)/, points: 1 },
|
|
76
|
+
// while loop
|
|
77
|
+
{ pattern: /while( )+\(.+\)/, points: 1 },
|
|
78
|
+
// printf function
|
|
79
|
+
{ pattern: /(printf|puts)( )*\(.+\)/, points: 1 },
|
|
80
|
+
// new Keyword from C++
|
|
81
|
+
{ pattern: /new \w+/, points: -1 },
|
|
82
|
+
// Single quote multicharacter string
|
|
83
|
+
{ pattern: /'.{2,}'/, points: -1 },
|
|
84
|
+
// JS variable declaration
|
|
85
|
+
{ pattern: /var( )+\w+( )*=?/, points: -1 }
|
|
86
|
+
],
|
|
87
|
+
|
|
88
|
+
"C++": [
|
|
89
|
+
// Primitive variable declaration.
|
|
90
|
+
{ pattern: /(char|long|int|float|double)( )+\w+( )*=?/, points: 2 },
|
|
91
|
+
// #include <whatever.h>
|
|
92
|
+
{
|
|
93
|
+
pattern: /#include( )*(<|")\w+(\.h)?(>|")/,
|
|
94
|
+
points: 2,
|
|
95
|
+
nearTop: true
|
|
96
|
+
},
|
|
97
|
+
// using namespace something
|
|
98
|
+
{ pattern: /using( )+namespace( )+.+( )*;/, points: 2 },
|
|
99
|
+
// template declaration
|
|
100
|
+
{ pattern: /template( )*<.*>/, points: 2 },
|
|
101
|
+
// std
|
|
102
|
+
{ pattern: /std::\w+/g, points: 2 },
|
|
103
|
+
// cout/cin/endl
|
|
104
|
+
{ pattern: /(cout|cin|endl)/g, points: 2 },
|
|
105
|
+
// Visibility specifiers
|
|
106
|
+
{ pattern: /(public|protected|private):/, points: 2 },
|
|
107
|
+
// nullptr
|
|
108
|
+
{ pattern: /nullptr/, points: 2 },
|
|
109
|
+
// new Keyword
|
|
110
|
+
{ pattern: /new \w+(\(.*\))?/, points: 1 },
|
|
111
|
+
// #define macro
|
|
112
|
+
{ pattern: /#define( )+.+/, points: 1 },
|
|
113
|
+
// template usage
|
|
114
|
+
{ pattern: /\w+<\w+>/, points: 1 },
|
|
115
|
+
// class keyword
|
|
116
|
+
{ pattern: /class( )+\w+/, points: 1 },
|
|
117
|
+
// void keyword
|
|
118
|
+
{ pattern: /void/g, points: 1 },
|
|
119
|
+
// (else )if statement
|
|
120
|
+
{ pattern: /(else )?if( )*\(.+\)/, points: 1 },
|
|
121
|
+
// while loop
|
|
122
|
+
{ pattern: /while( )+\(.+\)/, points: 1 },
|
|
123
|
+
// Scope operator
|
|
124
|
+
{ pattern: /\w*::\w+/, points: 1 },
|
|
125
|
+
// Single quote multicharacter string
|
|
126
|
+
{ pattern: /'.{2,}'/, points: -1 },
|
|
127
|
+
// Java List/ArrayList
|
|
128
|
+
{
|
|
129
|
+
pattern: /(List<\w+>|ArrayList<\w*>( )*\(.*\))(( )+[\w]+|;)/,
|
|
130
|
+
points: -1
|
|
131
|
+
}
|
|
132
|
+
],
|
|
133
|
+
|
|
134
|
+
Python: [
|
|
135
|
+
// Function definition
|
|
136
|
+
{ pattern: /def( )+\w+\(.*\)( )*:/, points: 2 },
|
|
137
|
+
// while loop
|
|
138
|
+
{ pattern: /while (.+):/, points: 2 },
|
|
139
|
+
// from library import something
|
|
140
|
+
{ pattern: /from [\w.]+ import (\w+|\*)/, points: 2 },
|
|
141
|
+
// class keyword
|
|
142
|
+
{ pattern: /class( )*\w+(\(( )*\w+( )*\))?( )*:/, points: 2 },
|
|
143
|
+
// if keyword
|
|
144
|
+
{ pattern: /if( )+(.+)( )*:/, points: 2 },
|
|
145
|
+
// elif keyword
|
|
146
|
+
{ pattern: /elif( )+(.+)( )*:/, points: 2 },
|
|
147
|
+
// else keyword
|
|
148
|
+
{ pattern: /else:/, points: 2 },
|
|
149
|
+
// for loop
|
|
150
|
+
{ pattern: /for (\w+|\(?\w+,( )*\w+\)?) in (.+):/, points: 2 },
|
|
151
|
+
// Python variable declaration.
|
|
152
|
+
{ pattern: /\w+( )*=( )*\w+(?!;)(\n|$)/, points: 1 },
|
|
153
|
+
// import something
|
|
154
|
+
{ pattern: /import ([[^.]\w])+/, points: 1, nearTop: true },
|
|
155
|
+
// print statement/function
|
|
156
|
+
{ pattern: /print((( )*\(.+\))|( )+.+)/, points: 1 },
|
|
157
|
+
// &&/|| operators
|
|
158
|
+
{ pattern: /(&{2}|\|{2})/, points: -1 }
|
|
159
|
+
],
|
|
160
|
+
|
|
161
|
+
Java: [
|
|
162
|
+
// System.out.println() etc.
|
|
163
|
+
{ pattern: /System\.(in|out)\.\w+/, points: 2 },
|
|
164
|
+
// Class variable declarations
|
|
165
|
+
{
|
|
166
|
+
pattern: /(private|protected|public)( )*\w+( )*\w+(( )*=( )*[\w])?/,
|
|
167
|
+
points: 2
|
|
168
|
+
},
|
|
169
|
+
// Method
|
|
170
|
+
{
|
|
171
|
+
pattern: /(private|protected|public)( )*\w+( )*[\w]+\(.+\)/,
|
|
172
|
+
points: 2
|
|
173
|
+
},
|
|
174
|
+
// String class
|
|
175
|
+
{ pattern: /(^|\s)(String)( )+[\w]+( )*=?/, points: 2 },
|
|
176
|
+
// List/ArrayList
|
|
177
|
+
{
|
|
178
|
+
pattern: /(List<\w+>|ArrayList<\w*>( )*\(.*\))(( )+[\w]+|;)/,
|
|
179
|
+
points: 2
|
|
180
|
+
},
|
|
181
|
+
// class keyword
|
|
182
|
+
{ pattern: /(public( )*)?class( )*\w+/, points: 2 },
|
|
183
|
+
// Array declaration.
|
|
184
|
+
{ pattern: /(\w+)(\[( )*\])+( )+\w+/, points: 2 },
|
|
185
|
+
// final keyword
|
|
186
|
+
{ pattern: /final( )*\w+/, points: 2 },
|
|
187
|
+
// getter & setter
|
|
188
|
+
{ pattern: /\w+\.(get|set)\(.+\)/, points: 2 },
|
|
189
|
+
// new Keyword (Java)
|
|
190
|
+
{ pattern: /new [A-Z]\w*( )*\(.+\)/, points: 2 },
|
|
191
|
+
// C style variable declaration.
|
|
192
|
+
{
|
|
193
|
+
pattern: /(^|\s)(char|long|int|float|double)( )+[\w]+( )*=?/,
|
|
194
|
+
points: 1
|
|
195
|
+
},
|
|
196
|
+
// extends/implements keywords
|
|
197
|
+
{ pattern: /(extends|implements)/, points: 2, nearTop: true },
|
|
198
|
+
// null keyword
|
|
199
|
+
{ pattern: /null/g, points: 1 },
|
|
200
|
+
// (else )if statement
|
|
201
|
+
{ pattern: /(else )?if( )*\(.+\)/, points: 1 },
|
|
202
|
+
// while loop
|
|
203
|
+
{ pattern: /while( )+\(.+\)/, points: 1 },
|
|
204
|
+
// void keyword
|
|
205
|
+
{ pattern: /void/g, points: 1 },
|
|
206
|
+
// const
|
|
207
|
+
{ pattern: /const( )*\w+/, points: -1 },
|
|
208
|
+
// pointer
|
|
209
|
+
{ pattern: /(\w+)( )*\*( )*\w+/, points: -1 },
|
|
210
|
+
// Single quote multicharacter string
|
|
211
|
+
{ pattern: /'.{2,}'/, points: -1 },
|
|
212
|
+
// C style include
|
|
213
|
+
{
|
|
214
|
+
pattern: /#include( )*(<|")\w+(\.h)?(>|")/,
|
|
215
|
+
points: -1,
|
|
216
|
+
nearTop: true
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
|
|
220
|
+
HTML: [
|
|
221
|
+
{
|
|
222
|
+
pattern: /<!DOCTYPE (html|HTML PUBLIC .+)>/,
|
|
223
|
+
points: 2,
|
|
224
|
+
nearTop: true
|
|
225
|
+
},
|
|
226
|
+
// Tags
|
|
227
|
+
{
|
|
228
|
+
pattern: /<[a-z0-9]+(( )*[\w]+=('|").+('|")( )*)?>.*<\/[a-z0-9]+>/g,
|
|
229
|
+
points: 2
|
|
230
|
+
},
|
|
231
|
+
// Properties
|
|
232
|
+
{ pattern: /[a-z-]+=("|').+("|')/g, points: 2 },
|
|
233
|
+
// PHP tag
|
|
234
|
+
{ pattern: /<\?php/, points: -50 }
|
|
235
|
+
],
|
|
236
|
+
|
|
237
|
+
CSS: [
|
|
238
|
+
// Properties
|
|
239
|
+
{ pattern: /[a-z-]+:(?!:).+;/, points: 2 },
|
|
240
|
+
// <style> tag from HTML
|
|
241
|
+
{ pattern: /<(\/)?style>/, points: -50 }
|
|
242
|
+
],
|
|
243
|
+
|
|
244
|
+
Ruby: [
|
|
245
|
+
// require/include
|
|
246
|
+
{
|
|
247
|
+
pattern: /(require|include)( )+'\w+(\.rb)?'/,
|
|
248
|
+
points: 2,
|
|
249
|
+
nearTop: true
|
|
250
|
+
},
|
|
251
|
+
// Function definition
|
|
252
|
+
{ pattern: /def( )+\w+( )*(\(.+\))?( )*\n/, points: 2 },
|
|
253
|
+
// Instance variables
|
|
254
|
+
{ pattern: /@\w+/, points: 2 },
|
|
255
|
+
// Boolean property
|
|
256
|
+
{ pattern: /\.\w+\?/, points: 2 },
|
|
257
|
+
// puts (Ruby print)
|
|
258
|
+
{ pattern: /puts( )+("|').+("|')/, points: 2 },
|
|
259
|
+
// Inheriting class
|
|
260
|
+
{ pattern: /class [A-Z]\w*( )*<( )*([A-Z]\w*(::)?)+/, points: 2 },
|
|
261
|
+
// attr_accessor
|
|
262
|
+
{ pattern: /attr_accessor( )+(:\w+(,( )*)?)+/, points: 2 },
|
|
263
|
+
// new
|
|
264
|
+
{ pattern: /\w+\.new( )+/, points: 2 },
|
|
265
|
+
// elsif keyword
|
|
266
|
+
{ pattern: /elsif/, points: 2 },
|
|
267
|
+
// do
|
|
268
|
+
{ pattern: /do( )*\|(\w+(,( )*\w+)?)+\|/, points: 2 },
|
|
269
|
+
// for loop
|
|
270
|
+
{ pattern: /for (\w+|\(?\w+,( )*\w+\)?) in (.+)/, points: 1 },
|
|
271
|
+
// nil keyword
|
|
272
|
+
{ pattern: /nil/, points: 1 },
|
|
273
|
+
// Scope operator
|
|
274
|
+
{ pattern: /[A-Z]\w*::[A-Z]\w*/, points: 1 }
|
|
275
|
+
],
|
|
276
|
+
|
|
277
|
+
Go: [
|
|
278
|
+
// package something
|
|
279
|
+
{ pattern: /package( )+[a-z]+\n/, points: 2, nearTop: true },
|
|
280
|
+
// import
|
|
281
|
+
{
|
|
282
|
+
pattern: /(import( )*\(( )*\n)|(import( )+"[a-z0-9/.]+")/,
|
|
283
|
+
points: 2,
|
|
284
|
+
nearTop: true
|
|
285
|
+
},
|
|
286
|
+
// error check
|
|
287
|
+
{ pattern: /if.+err( )*!=( )*nil.+{/, points: 2 },
|
|
288
|
+
// Go print
|
|
289
|
+
{ pattern: /fmt\.Print(f|ln)?\(.*\)/, points: 2 },
|
|
290
|
+
// function
|
|
291
|
+
{ pattern: /func(( )+\w+( )*)?\(.*\).*{/, points: 2 },
|
|
292
|
+
// variable initialisation
|
|
293
|
+
{ pattern: /\w+( )*:=( )*.+[^;\n]/, points: 2 },
|
|
294
|
+
// if/else if
|
|
295
|
+
{ pattern: /(}( )*else( )*)?if[^()]+{/, points: 2 },
|
|
296
|
+
// var/const declaration
|
|
297
|
+
{ pattern: /(var|const)( )+\w+( )+[\w*]+(\n|( )*=|$)/, points: 2 },
|
|
298
|
+
// public access on package
|
|
299
|
+
{ pattern: /[a-z]+\.[A-Z]\w*/, points: 1 },
|
|
300
|
+
// nil keyword
|
|
301
|
+
{ pattern: /nil/, points: 1 },
|
|
302
|
+
// Single quote multicharacter string
|
|
303
|
+
{ pattern: /'.{2,}'/, points: -1 }
|
|
304
|
+
],
|
|
305
|
+
|
|
306
|
+
PHP: [
|
|
307
|
+
// PHP tag
|
|
308
|
+
{ pattern: /<\?php/, points: 2 },
|
|
309
|
+
// PHP style variables.
|
|
310
|
+
{ pattern: /\$\w+/, points: 2 },
|
|
311
|
+
// use Something\Something;
|
|
312
|
+
{ pattern: /use( )+\w+(\\\w+)+( )*;/, points: 2, nearTop: true },
|
|
313
|
+
// arrow
|
|
314
|
+
{ pattern: /\$\w+->\w+/, points: 2 },
|
|
315
|
+
// require/include
|
|
316
|
+
{
|
|
317
|
+
pattern:
|
|
318
|
+
/(require|include)(_once)?( )*\(?( )*('|").+\.php('|")( )*\)?( )*;/,
|
|
319
|
+
points: 2
|
|
320
|
+
},
|
|
321
|
+
// echo 'something';
|
|
322
|
+
{ pattern: /echo( )+('|").+('|")( )*;/, points: 1 },
|
|
323
|
+
// NULL constant
|
|
324
|
+
{ pattern: /NULL/, points: 1 },
|
|
325
|
+
// new keyword
|
|
326
|
+
{ pattern: /new( )+((\\\w+)+|\w+)(\(.*\))?/, points: 1 },
|
|
327
|
+
// Function definition
|
|
328
|
+
{ pattern: /function(( )+[$\w]+\(.*\)|( )*\(.*\))/g, points: 1 },
|
|
329
|
+
// (else)if statement
|
|
330
|
+
{ pattern: /(else)?if( )+\(.+\)/, points: 1 },
|
|
331
|
+
// scope operator
|
|
332
|
+
{ pattern: /\w+::\w+/, points: 1 },
|
|
333
|
+
// === operator
|
|
334
|
+
{ pattern: /===/g, points: 1 },
|
|
335
|
+
// !== operator
|
|
336
|
+
{ pattern: /!==/g, points: 1 },
|
|
337
|
+
// C/JS style variable declaration.
|
|
338
|
+
{
|
|
339
|
+
pattern: /(^|\s)(var|char|long|int|float|double)( )+\w+( )*=?/,
|
|
340
|
+
points: -1
|
|
341
|
+
}
|
|
342
|
+
],
|
|
343
|
+
|
|
344
|
+
Unknown: []
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
function getPoints(language, lineOfCode, checkers) {
|
|
348
|
+
return checkers
|
|
349
|
+
.map((checker) => {
|
|
350
|
+
if (checker.pattern.test(lineOfCode)) {
|
|
351
|
+
return checker.points;
|
|
352
|
+
}
|
|
353
|
+
return 0;
|
|
354
|
+
})
|
|
355
|
+
.reduce((memo, num) => memo + num, 0);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function detect(snippet, options) {
|
|
359
|
+
const opts = Object.assign(
|
|
360
|
+
{
|
|
361
|
+
heuristic: true,
|
|
362
|
+
statistics: false
|
|
363
|
+
},
|
|
364
|
+
options || {}
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
let linesOfCode = snippet
|
|
368
|
+
.replace(/\r\n?/g, "\n")
|
|
369
|
+
.replace(/\n{2,}/g, "\n")
|
|
370
|
+
.split("\n");
|
|
371
|
+
|
|
372
|
+
function nearTop(index) {
|
|
373
|
+
if (linesOfCode.length <= 10) {
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
return index < linesOfCode.length / 10;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (opts.heuristic && linesOfCode.length > 500) {
|
|
380
|
+
linesOfCode = linesOfCode.filter((lineOfCode, index) => {
|
|
381
|
+
if (nearTop(index)) {
|
|
382
|
+
return true;
|
|
383
|
+
}
|
|
384
|
+
return index % Math.ceil(linesOfCode.length / 500) === 0;
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const pairs = Object.keys(LANGUAGES).map((key) => {
|
|
389
|
+
return { language: key, checkers: LANGUAGES[key] };
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const results = pairs.map((pair) => {
|
|
393
|
+
const language = pair.language;
|
|
394
|
+
const checkers = pair.checkers;
|
|
395
|
+
|
|
396
|
+
if (language === "Unknown") {
|
|
397
|
+
return { language: "Unknown", points: 1 };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const pointsList = linesOfCode.map((lineOfCode, index) => {
|
|
401
|
+
if (!nearTop(index)) {
|
|
402
|
+
return getPoints(
|
|
403
|
+
language,
|
|
404
|
+
lineOfCode,
|
|
405
|
+
checkers.filter((checker) => !checker.nearTop)
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
return getPoints(language, lineOfCode, checkers);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
const points = pointsList.reduce((memo, num) => memo + num);
|
|
412
|
+
|
|
413
|
+
return { language, points };
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
const sortedResult = results.sort(
|
|
417
|
+
(prev, next) => prev.points - next.points
|
|
418
|
+
);
|
|
419
|
+
const bestResult = sortedResult[sortedResult.length - 1];
|
|
420
|
+
|
|
421
|
+
if (opts.statistics) {
|
|
422
|
+
const statistics = {};
|
|
423
|
+
results.forEach((result) => {
|
|
424
|
+
statistics[result.language] = result.points;
|
|
425
|
+
});
|
|
426
|
+
return { detected: bestResult.language, statistics };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return bestResult.language;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const languages = Object.keys(LANGUAGES);
|
|
433
|
+
const LANG = {};
|
|
434
|
+
|
|
435
|
+
languages.forEach((language) => {
|
|
436
|
+
LANG[language] = language;
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
export { detect, languages, LANG };
|
package/LICENSE
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
Copyright (c) 2020, Salesforce.com, Inc.
|
|
2
|
-
All rights reserved.
|
|
3
|
-
|
|
4
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
5
|
-
|
|
6
|
-
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
7
|
-
|
|
8
|
-
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
9
|
-
|
|
10
|
-
* Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
11
|
-
|
|
12
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|