Skip to content

Commit

Permalink
feat(file): allow rendering in code block
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 24, 2024
1 parent 3ec3668 commit fbb4003
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
54 changes: 43 additions & 11 deletions docs/2.generators/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,44 @@ The `file` generator reads a file and inlines it's contents.

## Example

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

### Input

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

### Output

<!-- automd:file src="../../test/fixture/TEST.md" -->

## The Lazy Coder's Guide to Programming

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?

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.

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.
<!-- automd:file src="../../test/fixture/src/example.ts" code -->

```ts [example.ts]
/**
* Adds two numbers together.
*
* @example
*
* ```js
* add(1, 2); // 3
* ```
*/
export function add(a: number, b: number) {
return a + b;
}

export const object = {
/**
* An object key
*/
key: {
/**
* A subkey
*/
subkey: "value",
},
};

```

<!-- /automd -->

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

::field{name="code" type="boolean"}
Render file as code.
::

::field{name="lang" type="string"}
Code lang.
::

::field{name="name" type="string|boolean"}
File name in code. Use `no-name` to disable name in code.
::

::
2 changes: 1 addition & 1 deletion docs/2.generators/pm-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The `pm-install` or `pm-i` generator generates installation commands for several

```sh
# ✨ Auto-detect
npx nypm i -D package-name
npx nypm install -D package-name

# npm
npm install -D package-name
Expand Down
11 changes: 10 additions & 1 deletion src/generators/file.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { readFile } from "node:fs/promises";
import { basename, extname } from "pathe";
import { codeBlock } from "omark";
import { defineGenerator } from "../generator";
import { resolvePath } from "../_utils";

export const file = defineGenerator({
name: "file",
async generate({ args, config, url }) {
const fullPath = resolvePath(args.src, { url, dir: config.dir });
const contents = await readFile(fullPath, "utf8");
let contents = await readFile(fullPath, "utf8");

if (args.code) {
contents = codeBlock(contents, args.lang || extname(fullPath).slice(1), {
// prettier-ignore
ext: args.name === false ? undefined : (typeof args.name === 'string' ? args.name : `[${basename(fullPath)}]`),
});
}

return {
contents,
Expand Down

0 comments on commit fbb4003

Please sign in to comment.