question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFC: Syntax for @example tag

See original GitHub issue

RFC: Core set of tags for TSDoc brought up the question of the @example tag that could be used to identify examples. I’m forking it into a separate issue since @c69 pointed out that the design is unclear.

Can anyone find samples of its usage in existing documentation systems?

Since TSDoc will support Markdown, how would it interact with that? Maybe something like this?

/**
 * 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 {
}

How would an API reference web site leverage these directives?

@aciccarello

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:14 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
octogonzcommented, Dec 9, 2019

That’s a good suggestion. Currently API Documenter simply generates names like Example 1, Example 2, etc. Not as informative as it could be.

Maybe in @example Name - ... format, or take the text on the same line as the tag to be the name.

There’s already some precedent, where the @throws tag has a convention that the exception name can be specified using text on the same line as the tag.

   /**
    * Retrieves metadata about a book from the catalog.
    *    
    * @param isbnCode - the ISBN number for the book
    * @returns the retrieved book object
    *
    * @throws {@link IsbnSyntaxError}
    * This exception is thrown if the input is not a valid ISBN number.
    *
    * @throws {@link book-lib#BookNotFoundError}
    * Thrown if the ISBN number is valid, but no such book exists in the catalog.
    *
    * @public
    */
    function fetchBookByIsbn(isbnCode: string): Book;

So maybe a similar convention could be used for @example as in your sample. (BTW markdown headers should probably start with #, to avoid making assumptions about the documenter’s heading structure.)

So it could be like this:

/**
 * Parses a JSON file.
 *
 * @param path - Full path to the file.
 * @returns An object containing the JSON data.
 *
 * @example Parsing a basic JSON file
 *
 * # Contents of `file.json`
 * ```json
 * {
 *   "exampleItem": "text"
 * }
 * ```
 *
 * # Usage
 * ```ts
 * const result = parseFile("file.json");
 * ```
 *
 * # Result
 * ```ts
 * {
 *   exampleItem: 'text',
 * }
 * ```
 */

…and might get rendered like this:


Example: Parsing a basic JSON file

Contents of file.json

{
  "exampleItem": "text"
}

Usage

const result = parseFile("file.json");

Result

{
  exampleItem: 'text',
}
2reactions
vogloblinskycommented, Dec 6, 2018

@pgonzal I can have for Angular documentation tool https://github.com/compodoc/compodoc this kind of @example usage which use TypeScript decorator.

 * @example
 * Usage example
 * ```typescript
 * @Component({selector: 'something'}) export class AppComponent {};
 * ```
Read more comments on GitHub >

github_iconTop Results From Across the Web

RFC 4151:The 'tag' URI Scheme - IETF
This section first specifies the syntax of tag URIs and gives examples. It then describes a set of rules for minting tags that...
Read more >
Language tags in HTML and XML - W3C
Language tag syntax is defined by the IETF 's BCP 47. BCP stands for 'Best Current Practice', and is a persistent name for...
Read more >
IETF language tag - Wikipedia
History · Syntax of language tags · List of common primary language subtags · Relation to other standards · Extensions · See also...
Read more >
Tag URI
Compatibility with RFC 2822 email addresses: there was discussion on uri@w3.org about how to upgrade the tag scheme's syntax for better compatibility with ......
Read more >
doc: RFC 5646: Tags for Identifying Languages - hjp
For example, the user's language preferences in a Web browser can be used to select Web pages appropriately. ... The language tag syntax...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found