Skip to content

Commit 8b967af

Browse files
committed
transform all docs
1 parent f68d76d commit 8b967af

File tree

5 files changed

+1756
-8
lines changed

5 files changed

+1756
-8
lines changed

src/main.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import exec from "@actions/exec";
33
import fs from "fs";
44
import path from "path";
55

6-
import { get_docs, Docs } from "./fs";
7-
import { format_api, format_docs } from "./format";
8-
import { transform_cloudflare } from "./transform";
9-
import { FormattedFile } from "./format/format_api";
6+
import { get_docs } from "./fs";
7+
import { transform_cloudflare, transform_docs } from "./transform";
108

119
async function get_repo(
1210
target_repo: string,

src/transform/docs.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ import {
2020
transform_examples,
2121
transform_faq,
2222
transform_tutorials,
23+
transform,
2324
} from "./docs";
2425
import { faq_in, faq_out_full, faq_out_list } from "./fixtures/faq";
2526
import {
2627
migrating_in,
2728
migrating_out_full,
2829
migrating_out_list,
2930
} from "./fixtures/migrating";
31+
import { everything } from "./fixtures/everything";
3032

3133
const _docs = suite("transform_docs");
3234

@@ -40,7 +42,7 @@ _docs("transforms examples", async () => {
4042
assert.equal(output, { list: examples_out_list, full: examples_out_full });
4143
});
4244

43-
_docs("transforms examples", async () => {
45+
_docs("transforms tutorials", async () => {
4446
const output = await transform_tutorials(tutorials_in, "svelte");
4547
assert.equal(output, { list: tutorials_out_list, full: tutorials_out_full });
4648
});
@@ -60,4 +62,18 @@ _docs("transforms migrating", async () => {
6062
assert.equal(output, { list: migrating_out_list, full: migrating_out_full });
6163
});
6264

65+
_docs("transforms everything", async () => {
66+
const docs = {
67+
docs: docs_in,
68+
tutorials: tutorials_in,
69+
blog: blog_in,
70+
examples: examples_in,
71+
faq: faq_in,
72+
migrating: migrating_in,
73+
};
74+
const output = await transform(docs, "svelte", "docs");
75+
// console.log(JSON.stringify(output, null, 2));
76+
assert.equal(output, everything);
77+
});
78+
6379
_docs.run();

src/transform/docs.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ function extract_meta(files: SimpleFile[]): [SimpleFile[], { title: string }] {
148148

149149
if (index < 0) throw new Error("Examples must have a meta.json file.");
150150

151-
const meta = JSON.parse(files.splice(index, 1)[0].content as string);
151+
const meta = JSON.parse(files.slice(index, index + 1)[0].content as string);
152152

153-
return [files, meta];
153+
return [files.filter((_, i) => i !== index), meta];
154154
}
155155

156156
function process_example(
@@ -324,3 +324,48 @@ export async function transform_faq(
324324
full: final_faq,
325325
};
326326
}
327+
328+
const transform_map = {
329+
docs: transform_docs,
330+
migrating: transform_docs,
331+
faq: transform_faq,
332+
blog: transform_blog,
333+
examples: transform_examples,
334+
tutorials: transform_tutorials,
335+
};
336+
337+
interface Docs {
338+
docs?: DocsSource[];
339+
migrating?: DocsSource[];
340+
faq?: DocsSource[];
341+
blog?: DocsSource[];
342+
examples?: ExamplesCatSource[];
343+
tutorials?: TutorialSource[];
344+
}
345+
346+
interface TransformedDocs {
347+
content: unknown;
348+
project: string;
349+
type: string;
350+
}
351+
352+
export async function transform(files: Docs, project: string, dir: string) {
353+
const docs: TransformedDocs[] = [];
354+
355+
for (const key in files) {
356+
docs.push({
357+
content: await transform_map[key as keyof Docs](
358+
//@ts-ignore
359+
files[key],
360+
project,
361+
dir
362+
),
363+
project,
364+
type: key,
365+
});
366+
}
367+
368+
return docs;
369+
}
370+
371+
// docs: Array<Record<string, unknown>>, { project, type, keyby, version }

0 commit comments

Comments
 (0)