Skip to content

Commit 3ca4924

Browse files
committed
feat: file generator
1 parent 1b39861 commit 3ca4924

File tree

7 files changed

+99
-43
lines changed

7 files changed

+99
-43
lines changed

docs/2.generators/fetch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# fetch
22

3-
The `fetch` generator fetches a URL (using [unjs/ofetch](https://ofetch.unjs.io)) and renders downloads content inside.
3+
The `fetch` generator fetches a URL (using [unjs/ofetch](https://ofetch.unjs.io)) and inlines response body.
44

55
<!-- automd:example generator=fetch url="https://gist.github.com/pi0/c176defbba5568b6d06ea619a75f6104/raw" -->
66

docs/2.generators/file.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# file
2+
3+
The `file` generator reads a file and inlines it's contents.
4+
5+
<!-- automd:example generator=file src="../test/fixture/TEST.md" -->
6+
7+
## Example
8+
9+
### Input
10+
11+
<!-- automd:file src=../test/fixture/TEST.md -->
12+
<!-- /automd -->
13+
14+
### Output
15+
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.
25+
26+
<!-- /automd -->
27+
28+
<!-- /automd -->
29+
30+
## Arguments
31+
32+
- `src`: Relative path to the file.

src/generators/file.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { readFile } from "node:fs/promises";
2+
import { resolve } from "node:path";
3+
import { defineGenerator } from "../generator";
4+
5+
export const file = defineGenerator({
6+
name: "file",
7+
async generate({ args, config }) {
8+
const fullPath = resolve(config.dir, args.src);
9+
const contents = await readFile(fullPath, "utf8");
10+
11+
return {
12+
contents,
13+
};
14+
},
15+
});

src/generators/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { pmX, pmInstall } from "./pm";
55
import { fetch as _fetch } from "./fetch";
66
import { jsimport } from "./jsimport";
77
import { withAutomd } from "./with-automd";
8+
import { file } from "./file";
89

910
export default {
1011
jsdocs,
@@ -13,6 +14,7 @@ export default {
1314
"pm-install": pmInstall,
1415
"pm-x": pmX,
1516
fetch: _fetch,
17+
file,
1618
jsimport,
1719
"with-automd": withAutomd,
1820
} as Record<string, Generator>;

test/fixture/INPUT.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
<!-- automd:jsdocs -->
2121
<!-- /automd -->
2222

23-
## `fetch`
24-
25-
<!-- automd:fetch url="https://gist.github.com/pi0/c176defbba5568b6d06ea619a75f6104/raw" -->
26-
<!-- /automd -->
27-
2823
## `jsimport`
2924

3025
<!-- automd:jsimport cjs=true cdn=true name=pkg imports=foo,bar -->
@@ -34,3 +29,13 @@
3429

3530
<!-- automd:with-automd -->
3631
<!-- /automd -->
32+
33+
## `fetch`
34+
35+
<!-- automd:fetch url="https://gist.github.com/pi0/c176defbba5568b6d06ea619a75f6104/raw" -->
36+
<!-- /automd -->
37+
38+
## `file`
39+
40+
<!-- automd:file src="./TEST.md" -->
41+
<!-- /automd -->

test/fixture/OUTPUT.md

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,38 @@ add(1, 2); // 3
7575

7676
<!-- /automd -->
7777

78+
## `jsimport`
79+
80+
<!-- automd:jsimport cjs=true cdn=true name=pkg imports=foo,bar -->
81+
82+
**ESM** (Node.js, Bun)
83+
84+
```js
85+
import { foo, bar } from "pkg";
86+
```
87+
88+
**CommonJS** (Legacy Node.js)
89+
90+
```js
91+
const { foo, bar } = require("pkg");
92+
```
93+
94+
**CDN** (Deno, Bun and Browsers)
95+
96+
```js
97+
import { foo, bar } from "https://esm.sh/pkg";
98+
```
99+
100+
<!-- /automd -->
101+
102+
## `with-automd`
103+
104+
<!-- automd:with-automd -->
105+
106+
_🤖 docs are auto updated with [automd](https:/automd.unjs.io) (last updated: Tue Feb 20 2024)_
107+
108+
<!-- /automd -->
109+
78110
## `fetch`
79111

80112
<!-- automd:fetch url="https://gist.github.com/pi0/c176defbba5568b6d06ea619a75f6104/raw" -->
@@ -101,34 +133,16 @@ Why waste time solving problems when someone else has already done it for you? S
101133

102134
<!-- /automd -->
103135

104-
## `jsimport`
105-
106-
<!-- automd:jsimport cjs=true cdn=true name=pkg imports=foo,bar -->
136+
## `file`
107137

108-
**ESM** (Node.js, Bun)
138+
<!-- automd:file src="./TEST.md" -->
109139

110-
```js
111-
import { foo, bar } from "pkg";
112-
```
113-
114-
**CommonJS** (Legacy Node.js)
115-
116-
```js
117-
const { foo, bar } = require("pkg");
118-
```
140+
## The Lazy Coder's Guide to Programming
119141

120-
**CDN** (Deno, Bun and Browsers)
121-
122-
```js
123-
import { foo, bar } from "https://esm.sh/pkg";
124-
```
125-
126-
<!-- /automd -->
127-
128-
## `with-automd`
142+
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?
129143

130-
<!-- automd:with-automd -->
144+
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.
131145

132-
_🤖 docs are auto updated with [automd](https:/automd.unjs.io) (last updated: Tue Feb 20 2024)_
146+
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.
133147

134148
<!-- /automd -->
Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
# The Lazy Coder's Guide to Programming
2-
3-
## Chapter 1: The Art of Copy-Pasting
4-
5-
### Section 1.1: Ctrl+C, Ctrl+V, Repeat
1+
## The Lazy Coder's Guide to Programming
62

73
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?
84

9-
## Chapter 2: Debugging 101: Blame the Compiler
10-
11-
### Section 2.1: It's Not You, It's the Computer
12-
135
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.
146

15-
## Chapter 3: Stack Overflow: The Holy Grail
16-
17-
### Section 3.1: Why Figure It Out Yourself?
18-
197
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.

0 commit comments

Comments
 (0)