Skip to content

Commit bcd0f84

Browse files
committed
format docs
1 parent 9af0a18 commit bcd0f84

File tree

14 files changed

+323
-58
lines changed

14 files changed

+323
-58
lines changed

src/format/fixtures/generateFixtures.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const output_path = path.join(
1313
// const input = path.join(dir, "api-docs-markdown.js");
1414

1515
async function run() {
16-
const { contents } = await format()(
17-
"./api-docs.md",
16+
const { contents } = await format({
17+
file: "./api-docs.md",
1818
markdown,
19-
"svelte",
20-
"docs",
21-
"docs"
22-
);
19+
project: "svelte",
20+
docs_type: "docs",
21+
dir: "docs",
22+
});
2323
await fs.writeFile(output_path, `export default ${JSON.stringify(contents)}`);
2424
}
2525

src/format/format_api.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const _format = suite("transform docs");
2626
// });
2727

2828
_format("formats", async () => {
29-
const formatter = format();
3029
const md = `### hello
3130
3231
This is some paragraph text
@@ -42,7 +41,13 @@ console.log('boo')
4241
\`\`\`
4342
4443
`;
45-
const x = await formatter("boo.md", md, "svelte", "docs", "docs/boo");
44+
const x = await format({
45+
file: "boo.md",
46+
markdown: md,
47+
project: "svelte",
48+
docs_type: "docs",
49+
dir: "docs/boo",
50+
});
4651

4752
// console.log(x);
4853
});

src/format/format_api.ts

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@ import { set_link_attributes } from "./links";
2626
import { highight_code_block } from "./code";
2727
import { parse_frontmatter } from "./frontmatter";
2828
import { split_view } from "./split_view";
29-
import { custom_vfile, section } from "./types";
29+
import { custom_vfile } from "./types";
30+
import { section } from "../types";
3031

3132
import u from "unist-builder";
3233

34+
interface Format {
35+
file: string;
36+
markdown: string;
37+
project: string;
38+
docs_type: docs_type;
39+
dir: string;
40+
seen_slugs?: Map<string, number>;
41+
}
42+
3343
// MDAST == Markdown AST
3444
// HAST == HTML AST
3545

@@ -65,38 +75,40 @@ const { process } = unified()
6575
// HAST -> string
6676
.use(stringify, { allowDangerousCharacters: true, allowDangerousHtml: true });
6777

68-
export function format() {
69-
return async function (
70-
file: string,
71-
markdown: string,
72-
project: string,
73-
docs_type: docs_type,
74-
dir: string,
75-
seen_slugs: Map<string, number> = new Map()
76-
) {
77-
const sections: section[] = [];
78-
const section_title = file.toLowerCase().endsWith("readme.md")
79-
? make_slug(project, seen_slugs)
80-
: false;
81-
const vfile = vFile<custom_vfile>({
82-
contents: markdown,
83-
data: {
84-
seen_slugs,
85-
sections,
86-
section_stack: [sections],
87-
section_title,
88-
dir,
89-
file_type: file.toLowerCase().endsWith("readme.md")
90-
? "readme"
91-
: "other",
92-
docs_type,
93-
prev_level: 3,
94-
slugs: [],
95-
},
96-
});
78+
export async function format({
79+
file,
80+
markdown,
81+
project,
82+
docs_type,
83+
dir,
84+
seen_slugs = new Map(),
85+
}: Format): Promise<custom_vfile> {
86+
const sections: section[] = [];
87+
const section_title = file.toLowerCase().endsWith("readme.md")
88+
? project
89+
: false;
90+
91+
const section_slug = file.toLowerCase().endsWith("readme.md")
92+
? make_slug(project, seen_slugs)
93+
: false;
94+
95+
const vfile = vFile<custom_vfile>({
96+
contents: markdown,
97+
data: {
98+
seen_slugs,
99+
sections,
100+
section_stack: [sections],
101+
section_title,
102+
section_slug,
103+
dir,
104+
file_type: file.toLowerCase().endsWith("readme.md") ? "readme" : "other",
105+
docs_type,
106+
prev_level: 3,
107+
slugs: [],
108+
},
109+
});
97110

98-
const docs = await process(vfile);
111+
const docs = (await process(vfile)) as custom_vfile;
99112

100-
return docs;
101-
};
113+
return docs;
102114
}

src/format/frontmatter.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ export function parse_frontmatter(): Transformer {
2525
// @ts-ignore
2626
vFile.data.frontmatter = data;
2727

28-
if (!vFile.data.section_title) {
29-
vFile.data.section_title = make_slug(
28+
if (!vFile.data.section_slug) {
29+
vFile.data.section_slug = make_slug(
3030
//@ts-ignore
3131

3232
vFile.data.frontmatter.title,
3333
vFile.data.seen_slugs
3434
);
3535
}
36+
37+
if (!vFile.data.section_title) {
38+
//@ts-ignore
39+
vFile.data.section_title = vFile.data.frontmatter.title;
40+
}
41+
3642
}
3743
} catch (e) {
3844
vFile.messages.push(new Message("YAML failed to parse", e));

src/format/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { format_api, format_docs } from "./format_api";
1+
export { format } from "./format_api";

src/format/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import type { VFile } from "vfile";
22

3-
export type section = { slug: string; title: string; sections: section[] };
3+
import { section } from "../types";
4+
45
export type docs_type =
56
| "docs"
67
| "faq"
78
| "migrating"
89
| "examples"
910
| "tutorials"
1011
| "blog";
12+
1113
export type custom_vfile = VFile & {
1214
data: {
1315
sections: section[];
1416
section_stack: section[][];
1517
prev_level: number;
1618
section_title: string;
19+
section_slug: string;
1720
dir: string;
1821
slugs: string[];
1922
seen_slugs: Map<string, number>;

src/fs/get_content.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { promises as fs } from "fs";
22
import * as path from "path";
33

4-
interface SimpleFile {
4+
type SimpleFile = {
55
name: string;
66
content: SimpleFile[] | string;
7-
}
7+
};
88

99
type File = SimpleFile & {
1010
is_dir: boolean;
@@ -18,16 +18,16 @@ type doc_types =
1818
| "tutorials"
1919
| "examples";
2020

21-
export interface Docs {
21+
export type DocFiles = {
2222
docs?: unknown;
2323
faq?: unknown;
2424
migrating?: unknown;
2525
blog?: unknown;
2626
tutorials?: unknown;
2727
examples?: unknown;
28-
}
28+
};
2929

30-
type transformed_docs = [string, Docs][];
30+
export type transformed_docs = [string, DocFiles][];
3131

3232
export async function rc_read_file(file_path: string): Promise<File> {
3333
let file_or_dir: File = {
@@ -78,7 +78,7 @@ export function transform_files(
7878
): transformed_docs {
7979
let is_docs = false;
8080

81-
const base_docs: Docs = {};
81+
const base_docs: DocFiles = {};
8282
const pkgs: transformed_docs = [];
8383

8484
if (file.is_dir && Array.isArray(file.content)) {

src/fs/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { get_docs, Docs } from "./get_content";
1+
export { get_docs, DocFiles } from "./get_content";

src/main.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ async function get_repo(
4747
await exec.exec("git", ["switch", target_branch]);
4848

4949
const x = fs.readdirSync(process.cwd());
50-
console.log(x);
5150
}
5251

5352
async function run() {
@@ -87,10 +86,10 @@ async function run() {
8786

8887
if (docs.length) {
8988
docs.forEach(([project, docs]) => {
90-
for (const type in docs) {
91-
const _docs = format_docs[type](docs[type]);
92-
}
93-
})
89+
for (const type in docs) {
90+
const _docs = format_docs[type](docs[type]);
91+
}
92+
});
9493
const formatted_base_docs = base_docs.docs.map(([name, content]) =>
9594
format_api(name, content, "docs", name)
9695
);

src/transform/cloudflare.fixture.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { FormattedFile } from "../format/format_api";
1+
type FormattedFile = {
2+
content: string;
3+
title: string;
4+
slug: string;
5+
file: string;
6+
sections: section[];
7+
};
28

39
const one: FormattedFile = {
410
content: "test",

0 commit comments

Comments
 (0)