|
| 1 | +import { existsSync, readdirSync, readFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | + |
| 4 | +export function reverseSlugify(string, separator = "-") { |
| 5 | + return string |
| 6 | + .split(separator) |
| 7 | + .map((segment) => segment.charAt(0).toUpperCase() + segment.slice(1)) |
| 8 | + .join(' ') |
| 9 | + .trim(); |
| 10 | +} |
| 11 | + |
| 12 | +let errored = false; |
| 13 | +function raise(issue, snippet = '') { |
| 14 | + console.error(`${issue}${snippet ? ` in '${snippet}'` : ''}`); |
| 15 | + errored = true; |
| 16 | + return null; |
| 17 | +} |
| 18 | + |
| 19 | +const propertyRegex = /^\s+([a-zA-Z]+): (.+)/; |
| 20 | +const headerEndCodeStartRegex = /^\s+---\s+```.*\n/; |
| 21 | +const codeRegex = /^(.+)```/s |
| 22 | +function parseSnippet(snippetPath, text) { |
| 23 | + let cursor = 0; |
| 24 | + |
| 25 | + const fromCursor = () => text.substring(cursor); |
| 26 | + |
| 27 | + if(!fromCursor().trim().startsWith('---')) return raise('Missing header start delimiter \'---\'', snippetPath); |
| 28 | + cursor += 3; |
| 29 | + |
| 30 | + const properties = {}; |
| 31 | + let match; |
| 32 | + while((match = propertyRegex.exec(fromCursor())) !== null) { |
| 33 | + cursor += match[0].length; |
| 34 | + properties[match[1].toLowerCase()] = match[2]; |
| 35 | + } |
| 36 | + |
| 37 | + if(!('title' in properties)) return raise(`Missing 'title' property`, snippetPath); |
| 38 | + if(!('description' in properties)) return raise(`Missing 'description' property`, snippetPath); |
| 39 | + if(!('author' in properties)) return raise(`Missing 'author' property`, snippetPath); |
| 40 | + if(!('tags' in properties)) return raise(`Missing 'tags' property`, snippetPath); |
| 41 | + |
| 42 | + match = headerEndCodeStartRegex.exec(fromCursor()); |
| 43 | + if(match === null) return raise('Missing header end \'---\' or code start \'```\'', snippetPath); |
| 44 | + cursor += match[0].length; |
| 45 | + |
| 46 | + match = codeRegex.exec(fromCursor()); |
| 47 | + if(match === null) return raise('Missing code block end \'```\'', snippetPath); |
| 48 | + const code = match[1]; |
| 49 | + |
| 50 | + return { |
| 51 | + title: properties.title, |
| 52 | + description: properties.description, |
| 53 | + author: properties.author, |
| 54 | + tags: properties.tags.split(',').map((tag) => tag.trim()).filter((tag) => tag), |
| 55 | + code: code, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const snippetPath = "snippets/"; |
| 60 | +export function parseAllSnippets() { |
| 61 | + const snippets = {}; |
| 62 | + |
| 63 | + for(const language of readdirSync(snippetPath)) { |
| 64 | + const languagePath = join(snippetPath, language); |
| 65 | + |
| 66 | + const languageIconPath = join(languagePath, 'icon.svg'); |
| 67 | + |
| 68 | + if(!existsSync(languageIconPath)) { |
| 69 | + raise(`icon for '${language}' is missing`); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + const categories = []; |
| 74 | + for(const category of readdirSync(languagePath)) { |
| 75 | + if(category === 'icon.svg') continue; |
| 76 | + const categoryPath = join(languagePath, category); |
| 77 | + |
| 78 | + const categorySnippets = []; |
| 79 | + for(const snippet of readdirSync(categoryPath)) { |
| 80 | + const snippetPath = join(categoryPath, snippet); |
| 81 | + const snippetContent = readFileSync(snippetPath).toString(); |
| 82 | + |
| 83 | + const snippetData = parseSnippet(snippetPath, snippetContent); |
| 84 | + if(!snippetData) continue; |
| 85 | + categorySnippets.push(snippetData); |
| 86 | + } |
| 87 | + categories.push({ |
| 88 | + categoryName: reverseSlugify(category), |
| 89 | + snippets: categorySnippets, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + snippets[language] = categories; |
| 94 | + } |
| 95 | + |
| 96 | + return [ errored, snippets ]; |
| 97 | +} |
0 commit comments