Skip to content

Commit

Permalink
Update snippets util
Browse files Browse the repository at this point in the history
  • Loading branch information
itsbrunodev committed Jan 5, 2025
1 parent 45137f2 commit f41b8d8
Showing 1 changed file with 74 additions and 66 deletions.
140 changes: 74 additions & 66 deletions lib/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,91 @@ export interface Snippet {

export type GroupedSnippets = Record<string, Record<string, Snippet[]>>;

const basePath = "snippets";
const basePath = join(process.cwd(), "snippets");

export async function getSnippet(
language: string,
category: string,
name: string,
): Promise<(Omit<Snippet, "path"> & { snippet: string }) | null> {
const path = join(basePath, language, category, `${name}.mdx`);

const exists = existsSync(path);

if (!exists) {
try {
const path = join(basePath, language, category, `${name}.mdx`);

const exists = existsSync(path);

if (!exists) {
return null;
}

const content = readFileSync(path, "utf-8");

if (!content) {
return null;
}

const snippet = content.replace(
/export\s+const\s+metadata\s*=\s*\{[^}]*\};(\r\n){2}|```\w*\r\n/g,
"",
);

return {
language,
category,
name,
metadata: parseMetadata(content),
snippet,
};
} catch {
return null;
}

const content = readFileSync(path, "utf-8");

if (!content) {
return null;
}

const snippet = content.replace(
/export\s+const\s+metadata\s*=\s*\{[^}]*\};(\r\n){2}|```\w*\r\n/g,
"",
);

return {
language,
category,
name,
metadata: parseMetadata(content),
snippet,
};
}

export const getGroupedSnippets: () => Promise<GroupedSnippets> = cache(
async () => {
const files = readdirSync(basePath, { recursive: true }) as string[];

const snippets = files
.filter((file) => file.endsWith(".mdx"))
.map((file) => {
const relativePath = file.replace(`${basePath}/`, "");
const path = join("./", basePath, file);

const parts = relativePath.split("\\");
const language = parts[0];
const category = parts[1];
const name = parts[2].replace(".mdx", "");

const metadata = parseMetadata(readFileSync(path, "utf-8"));

return { language, category, name, metadata, path };
});

const groupedSnippets = snippets.reduce((acc, snippet) => {
const { language, category, name, metadata, path } = snippet;

if (!acc[language]) {
acc[language] = {};
}

if (!acc[language][category]) {
acc[language][category] = [];
}

acc[language][category].push({
language,
category,
name,
metadata,
path,
});

return acc;
}, {} as GroupedSnippets);

return groupedSnippets;
try {
const files = readdirSync(basePath, { recursive: true }) as string[];

const snippets = files
.filter((file) => file.endsWith(".mdx"))
.map((file) => {
const relativePath = file.replace(`${basePath}/`, "");
const path = join("./", basePath, file);

const parts = relativePath.split("\\");
const language = parts[0];
const category = parts[1];
const name = parts[2].replace(".mdx", "");

const metadata = parseMetadata(readFileSync(path, "utf-8"));

return { language, category, name, metadata, path };
});

const groupedSnippets = snippets.reduce((acc, snippet) => {
const { language, category, name, metadata, path } = snippet;

if (!acc[language]) {
acc[language] = {};
}

if (!acc[language][category]) {
acc[language][category] = [];
}

acc[language][category].push({
language,
category,
name,
metadata,
path,
});

return acc;
}, {} as GroupedSnippets);

return groupedSnippets;
} catch (e) {
return {};
}
},
);

0 comments on commit f41b8d8

Please sign in to comment.