From fbb400353f4ff1be3e979118e85288fa727af819 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 24 Feb 2024 13:02:47 +0100 Subject: [PATCH] feat(file): allow rendering in code block --- docs/2.generators/file.md | 54 ++++++++++++++++++++++++++------- docs/2.generators/pm-install.md | 2 +- src/generators/file.ts | 11 ++++++- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/docs/2.generators/file.md b/docs/2.generators/file.md index 1ebf015..64dcf29 100644 --- a/docs/2.generators/file.md +++ b/docs/2.generators/file.md @@ -4,24 +4,44 @@ The `file` generator reads a file and inlines it's contents. ## Example - + ### Input - + ### Output - - - ## 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. + + + ```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", + }, + }; + + ``` @@ -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. +:: + :: diff --git a/docs/2.generators/pm-install.md b/docs/2.generators/pm-install.md index 43a4a8a..3cab834 100644 --- a/docs/2.generators/pm-install.md +++ b/docs/2.generators/pm-install.md @@ -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 diff --git a/src/generators/file.ts b/src/generators/file.ts index 93f0309..6ea35d6 100644 --- a/src/generators/file.ts +++ b/src/generators/file.ts @@ -1,4 +1,6 @@ import { readFile } from "node:fs/promises"; +import { basename, extname } from "pathe"; +import { codeBlock } from "omark"; import { defineGenerator } from "../generator"; import { resolvePath } from "../_utils"; @@ -6,7 +8,14 @@ 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,