Skip to content

Commit fbb4003

Browse files
committed
feat(file): allow rendering in code block
1 parent 3ec3668 commit fbb4003

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

docs/2.generators/file.md

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,44 @@ The `file` generator reads a file and inlines it's contents.
44

55
## Example
66

7-
<!-- automd:example generator=file src="../../test/fixture/TEST.md" -->
7+
<!-- automd:example generator=file src="../../test/fixture/src/example.ts" code -->
88

99
### Input
1010

11-
<!-- automd:file src="../../test/fixture/TEST.md" -->
11+
<!-- automd:file src="../../test/fixture/src/example.ts" code -->
1212
<!-- /automd -->
1313

1414
### Output
1515

16-
<!-- automd:file src="../../test/fixture/TEST.md" -->
17-
18-
## The Lazy Coder's Guide to Programming
19-
20-
Programming can be hard. But fear not! With the power of copy-paste, you can conquer any coding challenge without breaking a sweat. Just remember: if it works once, it'll work a thousand times. Who needs original code anyway?
21-
22-
When your code doesn't work, don't blame yourself. It's clearly the compiler's fault for not understanding your genius. Remember, the more error messages you get, the closer you are to becoming a programming master.
23-
24-
Why waste time solving problems when someone else has already done it for you? Stack Overflow is your best friend, your mentor, and your savior. Just make sure to upvote the answers that save your bacon.
16+
<!-- automd:file src="../../test/fixture/src/example.ts" code -->
17+
18+
```ts [example.ts]
19+
/**
20+
* Adds two numbers together.
21+
*
22+
* @example
23+
*
24+
* ```js
25+
* add(1, 2); // 3
26+
* ```
27+
*/
28+
export function add(a: number, b: number) {
29+
return a + b;
30+
}
31+
32+
export const object = {
33+
/**
34+
* An object key
35+
*/
36+
key: {
37+
/**
38+
* A subkey
39+
*/
40+
subkey: "value",
41+
},
42+
};
43+
44+
```
2545

2646
<!-- /automd -->
2747

@@ -35,4 +55,16 @@ The `file` generator reads a file and inlines it's contents.
3555
Relative path to the file.
3656
::
3757

58+
::field{name="code" type="boolean"}
59+
Render file as code.
60+
::
61+
62+
::field{name="lang" type="string"}
63+
Code lang.
64+
::
65+
66+
::field{name="name" type="string|boolean"}
67+
File name in code. Use `no-name` to disable name in code.
68+
::
69+
3870
::

docs/2.generators/pm-install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The `pm-install` or `pm-i` generator generates installation commands for several
1717

1818
```sh
1919
# ✨ Auto-detect
20-
npx nypm i -D package-name
20+
npx nypm install -D package-name
2121

2222
# npm
2323
npm install -D package-name

src/generators/file.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { readFile } from "node:fs/promises";
2+
import { basename, extname } from "pathe";
3+
import { codeBlock } from "omark";
24
import { defineGenerator } from "../generator";
35
import { resolvePath } from "../_utils";
46

57
export const file = defineGenerator({
68
name: "file",
79
async generate({ args, config, url }) {
810
const fullPath = resolvePath(args.src, { url, dir: config.dir });
9-
const contents = await readFile(fullPath, "utf8");
11+
let contents = await readFile(fullPath, "utf8");
12+
13+
if (args.code) {
14+
contents = codeBlock(contents, args.lang || extname(fullPath).slice(1), {
15+
// prettier-ignore
16+
ext: args.name === false ? undefined : (typeof args.name === 'string' ? args.name : `[${basename(fullPath)}]`),
17+
});
18+
}
1019

1120
return {
1221
contents,

0 commit comments

Comments
 (0)