-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathsnippetParser.ts
134 lines (114 loc) · 3.55 KB
/
snippetParser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { existsSync, readdirSync, readFileSync } from "fs";
import { join } from "path";
import { CategoryType, RawSnippetType, SnippetType } from "../src/types";
import { isCorrectType } from "../src/utils/objectUtils";
import { raise } from "../src/utils/raise";
import { reverseSlugify, slugify } from "../src/utils/slugify";
let errored = false;
const crlfRegex = /\r\n/gm;
const propertyRegex = /^\s+([a-zA-Z]+):\s*(.+)/;
const headerEndCodeStartRegex = /^\s*---\s*```.*\n/;
const codeRegex = /^(.+)```/s;
function parseSnippet(snippetPath, name, text) {
if (crlfRegex.exec(text) !== null) {
errored = true;
return raise(
"Found CRLF line endings instead of LF line endings",
snippetPath
);
}
let cursor = 0;
const fromCursor = () => text.substring(cursor);
if (!fromCursor().trim().startsWith("---")) {
errored = true;
return raise("Missing header start delimiter '---'", snippetPath);
}
cursor += 3;
const properties = {};
let match;
while ((match = propertyRegex.exec(fromCursor())) !== null) {
cursor += match[0].length;
properties[match[1].toLowerCase()] = match[2];
}
if (
!isCorrectType<RawSnippetType>(properties, [
"title",
"description",
"author",
"tags",
])
) {
errored = true;
return raise("Invalid properties", snippetPath);
}
if (slugify(properties.title) !== name) {
errored = true;
return raise(
`slugifyed 'title' property doesn't match snippet file name`,
snippetPath
);
}
match = headerEndCodeStartRegex.exec(fromCursor());
if (match === null) {
errored = true;
return raise("Missing header end '---' or code start '```'", snippetPath);
}
cursor += match[0].length;
match = codeRegex.exec(fromCursor());
if (match === null) {
errored = true;
return raise("Missing code block end '```'", snippetPath);
}
const code: string = match[1];
return {
title: properties.title,
description: properties.description,
author: properties.author,
code,
tags: properties.tags
.split(",")
.map((tag) => tag.trim())
.filter((tag) => tag),
contributors: (properties.contributors ?? "")
.split(",")
.map((contributor) => contributor.trim())
.filter((contributor) => contributor),
};
}
const snippetPath = "snippets/";
export function parseAllSnippets() {
const snippets = {};
for (const language of readdirSync(snippetPath)) {
const languagePath = join(snippetPath, language);
const languageIconPath = join(languagePath, "icon.svg");
if (!existsSync(languageIconPath)) {
errored = true;
raise(`icon for '${language}' is missing`);
continue;
}
const categories: CategoryType[] = [];
for (const category of readdirSync(languagePath)) {
if (category === "icon.svg") continue;
const categoryPath = join(languagePath, category);
const categorySnippets: SnippetType[] = [];
for (const snippet of readdirSync(categoryPath)) {
const snippetPath = join(categoryPath, snippet);
const snippetContent = readFileSync(snippetPath).toString();
const snippetFileName = snippet.slice(0, -3);
const snippetData = parseSnippet(
snippetPath,
snippetFileName,
snippetContent
);
if (!snippetData) continue;
categorySnippets.push(snippetData);
}
categories.push({
categoryName: reverseSlugify(category),
snippets: categorySnippets,
});
}
snippets[language] = categories;
}
return [errored, snippets];
}