vitepress icon indicating copy to clipboard operation
vitepress copied to clipboard

Importing code snippet from jsdoc, tsdoc @example

Open soc221b opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

It would be better to write examples in single place.

Describe the solution you'd like

Input:

<<< @/snippets/snippet-with-example.js...?

Code file:

/**
 * Adds two numbers together.
 * 
 * @example
 * Here's a simple example:
 * ```
 * // Prints "2":
 * console.log(add(1,1));
 * ```
 * 
 * @example
 * Here's an example with negative numbers:
 * ```
 * // Prints "0":
 * console.log(add(1,-1));
 * ```
 */
export function add(x: number, y: number): number {
}

Output:

Here's a simple example:

// Prints "2":
console.log(add(1,1));

Here's an example with negative numbers:

// Prints "0":
console.log(add(1,-1));

Describe alternatives you've considered

No response

Additional context

No response

Validations

soc221b avatar Sep 10 '22 02:09 soc221b

This is sort of beyond the scope here. You'll probably have to use some third party library for extracting information from jsdocs comments. I'm still keeping the issue open for now.

brc-dd avatar Sep 10 '22 03:09 brc-dd

I would love if vitepress had jsdoc drive API documentation (+ loading readme from /src) as a core feature. It would be amazing for keeping documentation in sync with code!

thedamon avatar Nov 07 '22 22:11 thedamon

One can probably use Microsoft API Extractor for these kind of things. Including it in the core might get difficult for us to maintain as various users will want different sort of customizations on how docs are being extracted from comments.

Here are some example repos using api-extractor with VitePress/VuePress: https://github.com/kristiandupont/extract-pg-schema, https://github.com/kazupon/api-extractor-sandbox One can find some others too, these came on top in my search.

If someone wants to build a plugin, that'll be great too.

brc-dd avatar Nov 27 '22 15:11 brc-dd