@@ -6,15 +6,23 @@ import { isCorrectType } from "../src/utils/objectUtils";
6
6
import { raise } from "../src/utils/raise" ;
7
7
import { reverseSlugify , slugify } from "../src/utils/slugify" ;
8
8
9
- let errored = false ;
10
-
11
9
const crlfRegex = / \r \n / gm;
12
10
const propertyRegex = / ^ \s + ( [ a - z A - Z ] + ) : \s * ( .+ ) / ;
13
11
const headerEndCodeStartRegex = / ^ \s * - - - \s * ` ` ` .* \n / ;
14
12
const codeRegex = / ^ ( .+ ) ` ` ` / s;
15
- function parseSnippet ( snippetPath , name , text ) {
13
+
14
+ let errored = false ;
15
+
16
+ function parseSnippet ( {
17
+ snippetPath,
18
+ name,
19
+ text,
20
+ } : {
21
+ snippetPath : string ;
22
+ name : string ;
23
+ text : string ;
24
+ } ) : SnippetType | null {
16
25
if ( crlfRegex . exec ( text ) !== null ) {
17
- errored = true ;
18
26
return raise (
19
27
"Found CRLF line endings instead of LF line endings" ,
20
28
snippetPath
@@ -25,7 +33,6 @@ function parseSnippet(snippetPath, name, text) {
25
33
const fromCursor = ( ) => text . substring ( cursor ) ;
26
34
27
35
if ( ! fromCursor ( ) . trim ( ) . startsWith ( "---" ) ) {
28
- errored = true ;
29
36
return raise ( "Missing header start delimiter '---'" , snippetPath ) ;
30
37
}
31
38
cursor += 3 ;
@@ -46,12 +53,10 @@ function parseSnippet(snippetPath, name, text) {
46
53
"tags" ,
47
54
] )
48
55
) {
49
- errored = true ;
50
56
return raise ( "Invalid properties" , snippetPath ) ;
51
57
}
52
58
53
59
if ( slugify ( properties . title ) !== name ) {
54
- errored = true ;
55
60
return raise (
56
61
`slugifyed 'title' property doesn't match snippet file name` ,
57
62
snippetPath
@@ -60,14 +65,12 @@ function parseSnippet(snippetPath, name, text) {
60
65
61
66
match = headerEndCodeStartRegex . exec ( fromCursor ( ) ) ;
62
67
if ( match === null ) {
63
- errored = true ;
64
68
return raise ( "Missing header end '---' or code start '```'" , snippetPath ) ;
65
69
}
66
70
cursor += match [ 0 ] . length ;
67
71
68
72
match = codeRegex . exec ( fromCursor ( ) ) ;
69
73
if ( match === null ) {
70
- errored = true ;
71
74
return raise ( "Missing code block end '```'" , snippetPath ) ;
72
75
}
73
76
const code : string = match [ 1 ] ;
@@ -103,24 +106,29 @@ export function parseAllSnippets() {
103
106
}
104
107
105
108
const categories : CategoryType [ ] = [ ] ;
109
+
106
110
for ( const category of readdirSync ( languagePath ) ) {
107
111
if ( category === "icon.svg" ) continue ;
108
- const categoryPath = join ( languagePath , category ) ;
109
112
113
+ const categoryPath = join ( languagePath , category ) ;
110
114
const categorySnippets : SnippetType [ ] = [ ] ;
115
+
111
116
for ( const snippet of readdirSync ( categoryPath ) ) {
112
117
const snippetPath = join ( categoryPath , snippet ) ;
113
118
const snippetContent = readFileSync ( snippetPath ) . toString ( ) ;
114
119
const snippetFileName = snippet . slice ( 0 , - 3 ) ;
115
-
116
- const snippetData = parseSnippet (
120
+ const snippetData = parseSnippet ( {
117
121
snippetPath,
118
- snippetFileName ,
119
- snippetContent
120
- ) ;
121
- if ( ! snippetData ) continue ;
122
+ name : snippetFileName ,
123
+ text : snippetContent ,
124
+ } ) ;
125
+ if ( snippetData === null ) {
126
+ errored = true ;
127
+ continue ;
128
+ }
122
129
categorySnippets . push ( snippetData ) ;
123
130
}
131
+
124
132
categories . push ( {
125
133
categoryName : reverseSlugify ( category ) ,
126
134
snippets : categorySnippets ,
0 commit comments