Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccc4cc0

Browse files
committed
remove duplicate code in script
1 parent 7d1d241 commit ccc4cc0

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

‎utils/snippetParser.ts‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@ import { isCorrectType } from "../src/utils/objectUtils";
66
import { raise } from "../src/utils/raise";
77
import { reverseSlugify, slugify } from "../src/utils/slugify";
88

9-
let errored = false;
10-
119
const crlfRegex = /\r\n/gm;
1210
const propertyRegex = /^\s+([a-zA-Z]+):\s*(.+)/;
1311
const headerEndCodeStartRegex = /^\s*---\s*```.*\n/;
1412
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 {
1625
if (crlfRegex.exec(text) !== null) {
17-
errored = true;
1826
return raise(
1927
"Found CRLF line endings instead of LF line endings",
2028
snippetPath
@@ -25,7 +33,6 @@ function parseSnippet(snippetPath, name, text) {
2533
const fromCursor = () => text.substring(cursor);
2634

2735
if (!fromCursor().trim().startsWith("---")) {
28-
errored = true;
2936
return raise("Missing header start delimiter '---'", snippetPath);
3037
}
3138
cursor += 3;
@@ -46,12 +53,10 @@ function parseSnippet(snippetPath, name, text) {
4653
"tags",
4754
])
4855
) {
49-
errored = true;
5056
return raise("Invalid properties", snippetPath);
5157
}
5258

5359
if (slugify(properties.title) !== name) {
54-
errored = true;
5560
return raise(
5661
`slugifyed 'title' property doesn't match snippet file name`,
5762
snippetPath
@@ -60,14 +65,12 @@ function parseSnippet(snippetPath, name, text) {
6065

6166
match = headerEndCodeStartRegex.exec(fromCursor());
6267
if (match === null) {
63-
errored = true;
6468
return raise("Missing header end '---' or code start '```'", snippetPath);
6569
}
6670
cursor += match[0].length;
6771

6872
match = codeRegex.exec(fromCursor());
6973
if (match === null) {
70-
errored = true;
7174
return raise("Missing code block end '```'", snippetPath);
7275
}
7376
const code: string = match[1];
@@ -103,24 +106,29 @@ export function parseAllSnippets() {
103106
}
104107

105108
const categories: CategoryType[] = [];
109+
106110
for (const category of readdirSync(languagePath)) {
107111
if (category === "icon.svg") continue;
108-
const categoryPath = join(languagePath, category);
109112

113+
const categoryPath = join(languagePath, category);
110114
const categorySnippets: SnippetType[] = [];
115+
111116
for (const snippet of readdirSync(categoryPath)) {
112117
const snippetPath = join(categoryPath, snippet);
113118
const snippetContent = readFileSync(snippetPath).toString();
114119
const snippetFileName = snippet.slice(0, -3);
115-
116-
const snippetData = parseSnippet(
120+
const snippetData = parseSnippet({
117121
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+
}
122129
categorySnippets.push(snippetData);
123130
}
131+
124132
categories.push({
125133
categoryName: reverseSlugify(category),
126134
snippets: categorySnippets,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /