|
| 1 | +const joinByNewLine = (s: string[]) => s.join("\n"); |
| 2 | +const joinByDoubleNewLine = (s: string[]) => s.join("\n\n"); |
| 3 | +const indent = (s: string) => ` ${s}`; |
| 4 | +const escape = (s: string) => s.replace(/`/g, "\`"); |
| 5 | + |
| 6 | +const code = (s: string) => { |
| 7 | + return escape("`" + s + "`"); |
| 8 | +}; |
| 9 | + |
| 10 | +const codeBlock = (s: string, lang = "javascript") => { |
| 11 | + return joinByNewLine([ |
| 12 | + `${indent(escape("```" + lang))}`, |
| 13 | + s, |
| 14 | + `${indent(escape("```"))}`, |
| 15 | + ]); |
| 16 | +}; |
| 17 | + |
| 18 | +const $row = (s: string) => { |
| 19 | + return joinByNewLine(["", "<!-- ROW -->", "<tr>", s, "</tr>"]); |
| 20 | +}; |
| 21 | + |
| 22 | +const $colDoubleNewLine = (s: string, cb?: (input: string) => string) => { |
| 23 | + return joinByDoubleNewLine(["<td>", cb?.(s) ?? s, "</td>"]); |
| 24 | +}; |
| 25 | + |
| 26 | +const $col = (s: string) => { |
| 27 | + return `<td>${s}</td>`; |
| 28 | +}; |
| 29 | +const $colCode = (s: string) => { |
| 30 | + return $colDoubleNewLine(s, code); |
| 31 | +}; |
| 32 | +const $colCodeBlock = (s: string) => { |
| 33 | + return $colDoubleNewLine(s, codeBlock); |
| 34 | +}; |
| 35 | + |
| 36 | +const snippetRow = ( |
| 37 | + { prefix, name, body }: { |
| 38 | + prefix: string; |
| 39 | + name: string; |
| 40 | + body: string | string[]; |
| 41 | + }, |
| 42 | +) => { |
| 43 | + const cols = joinByNewLine([ |
| 44 | + $colCode(prefix), |
| 45 | + $col(name), |
| 46 | + $colCodeBlock(Array.isArray(body) ? body.join("\n") : body), |
| 47 | + ]); |
| 48 | + |
| 49 | + return $row(cols); |
| 50 | +}; |
| 51 | + |
| 52 | +const $table = (header: string, rows: string[]) => { |
| 53 | + return joinByNewLine([ |
| 54 | + "<table>", |
| 55 | + header, |
| 56 | + joinByNewLine(rows), |
| 57 | + "</table>", |
| 58 | + ]); |
| 59 | +}; |
| 60 | + |
| 61 | +const headerRow = (headers: string[]) => { |
| 62 | + const cols = joinByNewLine(headers.map((header) => $col(header))); |
| 63 | + |
| 64 | + return $row(cols); |
| 65 | +}; |
| 66 | + |
| 67 | +// Custom for single snippet |
| 68 | +const testcase = [ |
| 69 | + { |
| 70 | + prefix: "c", |
| 71 | + name: "const", |
| 72 | + body: "const $0", |
| 73 | + }, |
| 74 | + { |
| 75 | + prefix: "l", |
| 76 | + name: "let", |
| 77 | + body: "let $0", |
| 78 | + }, |
| 79 | + { |
| 80 | + prefix: "ifei", |
| 81 | + name: "if/else-if statement", |
| 82 | + body: "if ($1) {\n\t$2\n} else if ($3) {\n\t$4\n}", |
| 83 | + }, |
| 84 | + { |
| 85 | + prefix: "csc", |
| 86 | + name: "class with constructor", |
| 87 | + body: [ |
| 88 | + "class $1 {", |
| 89 | + "\tconstructor($2) {", |
| 90 | + "\t\t$0", |
| 91 | + "\t}", |
| 92 | + "}", |
| 93 | + ], |
| 94 | + }, |
| 95 | +]; |
| 96 | +const snippetTableHeader = headerRow(["Prefix", "Name", "Body"]); |
| 97 | +const snippetRows = testcase.map(snippetRow); |
| 98 | + |
| 99 | +const singleSnippetTable = $table(snippetTableHeader, snippetRows); |
| 100 | + |
| 101 | +Deno.writeFileSync( |
| 102 | + "./dist/test.md", |
| 103 | + new TextEncoder().encode(singleSnippetTable), |
| 104 | +); |
0 commit comments