Skip to content

Commit fec2278

Browse files
committed
Populate generated docs into readme
1 parent 464a3e1 commit fec2278

File tree

8 files changed

+63
-39
lines changed

8 files changed

+63
-39
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,13 @@ Will be sorted into appropriate categories in the future.
354354
# ensure Deno is installed
355355
# https://deno.land/manual@v1.29.1/getting_started/installation
356356

357-
# generate .code-snippets
357+
# generate .code-snippets and documentation
358358
npm run generate
359359
```
360360

361361
---
362362

363-
## 🚧 Experimental documentation
364-
363+
<!-- START:docs-gen -->
365364
## Snippets
366365

367366

@@ -3589,3 +3588,5 @@ type ${1:Model} = ${2:first} & ${3:second}
35893588
</td>
35903589
</tr>
35913590
</table>
3591+
3592+
<!-- END:docs-gen -->

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@
3838
},
3939
"scripts": {
4040
"publish": "vsce package && vsce publish",
41-
"generate": "deno run --allow-write --allow-read src/app.ts",
42-
"generate:table": "deno run --allow-write --allow-read src/app.ts --table --snippets=false",
43-
"generate:all": "deno run --allow-write --allow-read src/app.ts --table --snippets",
44-
"test": "deno run --allow-write --allow-read src/utils/table.ts"
41+
"generate": "deno run -A src/app.ts --snippets --docs",
42+
"generate:snippets": "deno run -A src/app.ts --snippets"
4543
},
4644
"contributes": {
4745
"snippets": [

src/app.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
import { parse } from "./deps.ts";
2-
import {
3-
generateSnippetDocs,
4-
generateVariantSections,
5-
} from "./docs-gen/snippets.ts";
6-
// import { logMdTables } from "./docs-gen/table-md.ts";
2+
import { generateDocs, populateDocsBlock } from "./docs-gen/snippets.ts";
73
import { languages } from "./snippets/app.ts";
84
import {
95
convertToVscSnippet,
10-
generateSnippetsFile,
6+
generateSnippets,
117
groupSnippets,
128
} from "./utils/snippets.ts";
139

1410
const flags = parse(Deno.args, {
15-
boolean: ["table", "snippets"],
16-
default: { snippets: true },
11+
boolean: ["snippets", "docs"],
12+
default: { snippets: false, docs: false },
1713
});
1814

19-
if (!flags.table && !flags.snippets) {
20-
console.log("Please specify at least one flag: --table or --snippets");
15+
if (!flags.snippets && !flags.docs) {
16+
console.log("Please specify at least one flag: --snippets or --docs");
2117
} else {
2218
if (flags.snippets) {
23-
// Snippets generation
2419
languages.forEach((language) => {
2520
const categorizedVscSnippets = language
2621
.snippetDefinitions.map(
27-
(item) => ({
28-
...item,
29-
snippets: convertToVscSnippet(item.snippets),
30-
}),
22+
(item) => {
23+
const snippets = convertToVscSnippet(item.snippets);
24+
return { ...item, snippets };
25+
},
3126
);
3227

3328
const variantVscSnippet = groupSnippets(
3429
categorizedVscSnippets.map((item) => item.snippets),
3530
);
36-
generateSnippetsFile(language.fileExtension, variantVscSnippet);
31+
generateSnippets(language.fileExtension, variantVscSnippet);
3732
});
3833
}
3934

40-
if (flags.table) {
41-
const sections = generateVariantSections(languages);
42-
generateSnippetDocs(sections);
35+
// TODO: probably better to make it generate from vsc json
36+
// pass in meta, and snippets converted to vsc format
37+
if (flags.docs) {
38+
const docs = generateDocs(languages);
39+
populateDocsBlock(docs);
4340
}
4441
}

src/deps.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
export { parse } from "https://deno.land/std@0.168.0/flags/mod.ts";
21
export { ensureDirSync } from "https://deno.land/std@0.141.0/fs/ensure_dir.ts";
2+
export { parse } from "https://deno.land/std@0.168.0/flags/mod.ts";
33
export { markdownTable } from "https://esm.sh/markdown-table@3";
4+
5+
import replace, * as _replace from "npm:replace-in-file";
6+
7+
// Fix types
8+
export const replaceInFile = replace as unknown as (
9+
config: _replace.ReplaceInFileConfig,
10+
) => Promise<_replace.ReplaceResult[]>;

src/docs-gen/snippets.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { ensureDirSync } from "../deps.ts";
1+
import { replaceInFile } from "../deps.ts";
22
import { XSnippetDefinition, XSnippetVariant } from "../models/app.ts";
33
import {
44
$col,
55
$colCode,
66
$colCodeBlock,
77
$row,
88
$table,
9+
htmlComment,
910
joinByDoubleNewLine,
1011
joinByNewLine,
1112
} from "./table-html.ts";
@@ -57,17 +58,36 @@ const generateVariantSection = (variant: XSnippetVariant) => {
5758
return joinByNewLine([title, description, "", ...sections]);
5859
};
5960

60-
export const generateVariantSections = (variants: XSnippetVariant[]) => {
61+
export const generateDocs = (variants: XSnippetVariant[]) => {
6162
return joinByDoubleNewLine(variants.map(generateVariantSection));
6263
};
6364

64-
export const generateSnippetDocs = (table: string) => {
65-
const path = "./dist";
66-
ensureDirSync(path);
67-
const file = `${path}/docs.md`;
65+
const docsGenId = "docs-gen";
66+
const docsGen = {
67+
start: htmlComment(`START:${docsGenId}`),
68+
end: htmlComment(`END:${docsGenId}`),
69+
};
70+
71+
const docsBlock = (s: string) => {
72+
return joinByNewLine([docsGen.start, s, docsGen.end]);
73+
};
6874

69-
Deno.writeFileSync(
70-
file,
71-
new TextEncoder().encode(table),
75+
export const populateDocsBlock = async (input: string) => {
76+
const regex = new RegExp(
77+
`${docsGen.start}[\\s\\S]*?${docsGen.end}`,
78+
"g",
7279
);
80+
81+
const options = {
82+
files: "./README.md",
83+
from: regex,
84+
to: docsBlock(input),
85+
};
86+
87+
try {
88+
const results = await replaceInFile(options);
89+
console.log("Replacement results:", results);
90+
} catch (error) {
91+
console.error("Error occurred:", error);
92+
}
7393
};

src/docs-gen/table-html.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const joinByDoubleNewLine = (s: string[]) => s.join("\n\n");
44
export const indent = (s: string, size = 2) => `${" ".repeat(size)}${s}`;
55
export const escapeBackticks = (s: string) => s.replace(/`/g, "\`");
66

7+
export const htmlComment = (s: string) => `<!-- ${s} -->`;
78
export const code = (s: string) => {
89
return escapeBackticks("`" + s + "`");
910
};

src/docs-gen/table-md.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export const serializeForMarkdown = (str: string) => {
1313
.replace(/\t/g, "&nbsp;&nbsp;")
1414
.replace(/\|/g, "\\|");
1515
}
16-
// TODO: dont remove | when it is in ``
17-
// but it's different for every .md implementation
16+
// TODO: don't remove | when it is in code block
17+
// but it differs for every .md implementation
1818
return str.replace(/\|/g, "\\|");
1919
};
2020

src/utils/snippets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const groupSnippets = (dicts: VscSnippetDict[]) => {
1717
}));
1818
};
1919

20-
export const generateSnippetsFile = (name: string, data: VscSnippetDict) => {
20+
export const generateSnippets = (name: string, data: VscSnippetDict) => {
2121
const path = "./dist";
2222
ensureDirSync(path);
2323
const file = `${path}/${name}.code-snippets`;

0 commit comments

Comments
 (0)