Skip to content

Commit 1a8e746

Browse files
committed
Create futher extractions of snippet generation table code
1 parent ada8055 commit 1a8e746

File tree

1 file changed

+95
-54
lines changed

1 file changed

+95
-54
lines changed

src/utils/table.ts

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { VscSnippetDefinition } from "../models/app.ts";
2+
13
const joinByNewLine = (s: string[]) => s.join("\n");
24
const joinByDoubleNewLine = (s: string[]) => s.join("\n\n");
35
const indent = (s: string) => ` ${s}`;
@@ -16,7 +18,7 @@ const codeBlock = (s: string, lang = "javascript") => {
1618
};
1719

1820
const $row = (s: string) => {
19-
return joinByNewLine(["", "<!-- ROW -->", "<tr>", s, "</tr>"]);
21+
return joinByNewLine(["", "<tr>", s, "</tr>"]);
2022
};
2123

2224
const $colDoubleNewLine = (s: string, cb?: (input: string) => string) => {
@@ -33,72 +35,111 @@ const $colCodeBlock = (s: string) => {
3335
return $colDoubleNewLine(s, codeBlock);
3436
};
3537

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-
38+
const $headerRow = (headers: string[]) => {
39+
const cols = joinByNewLine(headers.map($col));
4940
return $row(cols);
5041
};
5142

52-
const $table = (header: string, rows: string[]) => {
43+
const $table = (headings: string[], rows: string[]) => {
5344
return joinByNewLine([
5445
"<table>",
55-
header,
46+
$headerRow(headings),
5647
joinByNewLine(rows),
5748
"</table>",
5849
]);
5950
};
6051

61-
const headerRow = (headers: string[]) => {
62-
const cols = joinByNewLine(headers.map((header) => $col(header)));
52+
// Snippet specific login
53+
54+
type SnippetRow = {
55+
prefix: string;
56+
name: string;
57+
body: string | string[];
58+
};
59+
60+
const snippetRow = ({ prefix, name, body }: SnippetRow) => {
61+
const parsedBody = Array.isArray(body) ? body.join("\n") : body;
62+
const cols = joinByNewLine([
63+
$colCode(prefix),
64+
$col(name),
65+
$colCodeBlock(parsedBody),
66+
]);
6367

6468
return $row(cols);
6569
};
6670

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);
71+
export const generateSnippetTable = (items: SnippetRow[]) => {
72+
const headings = ["Prefix", "Name", "Body"];
73+
const rows = items.map(snippetRow);
74+
75+
return $table(headings, rows);
76+
};
9877

99-
const singleSnippetTable = $table(snippetTableHeader, snippetRows);
78+
export const generateSnippetSection = (
79+
{ meta, snippets }: VscSnippetDefinition,
80+
) => {
81+
const title = `### ${meta.title}`;
82+
const description = meta.description ? meta.description : "";
83+
const table = generateSnippetTable(
84+
Object.entries(snippets).map(([name, { prefix, body }]) => ({
85+
name,
86+
prefix: prefix as string,
87+
body,
88+
})),
89+
);
90+
91+
return joinByNewLine([title, description, table]);
92+
};
10093

101-
Deno.writeFileSync(
102-
"./dist/test.md",
103-
new TextEncoder().encode(singleSnippetTable),
104-
);
94+
export const writeSectionToFile = (table: string) => {
95+
Deno.writeFileSync(
96+
"./dist/test.md",
97+
new TextEncoder().encode(table),
98+
);
99+
};
100+
101+
// Testcase
102+
const section = generateSnippetSection({
103+
meta: {
104+
title: "Test",
105+
},
106+
snippets: {
107+
"const": {
108+
"prefix": "c",
109+
"body": "const $0",
110+
},
111+
"let": {
112+
"prefix": "l",
113+
"body": "let $0",
114+
},
115+
"const assignment": {
116+
"prefix": "ca",
117+
"body": "const $1 = $0",
118+
},
119+
"let assignment": {
120+
"prefix": "la",
121+
"body": "let $1 = $0",
122+
},
123+
"const string assignment": {
124+
"prefix": "cas",
125+
"body": "const $1 = '$0'",
126+
},
127+
"const array assignment": {
128+
"prefix": "car",
129+
"body": "const $1 = [$0]",
130+
},
131+
"const object assignment": {
132+
"prefix": "cao",
133+
"body": "const $1 = { $0 }",
134+
},
135+
"object destructuring": {
136+
"prefix": "dob",
137+
"body": "const { $0 } = ${1:object}",
138+
},
139+
"array destructuring": {
140+
"prefix": "dar",
141+
"body": "const [$0] = ${1:array}",
142+
},
143+
},
144+
});
145+
writeSectionToFile(section);

0 commit comments

Comments
 (0)