jsdoc2md / dmd

The default output template for jsdoc2md
MIT License
39 stars 49 forks source link

Support both caption and lang sub tag for examples #58

Closed ManBearTM closed 6 years ago

ManBearTM commented 6 years ago

When trying to specify both a caption and a language for jsdoc examples the language is simply ignored.

This pull request should make it possible to include both by using one of below methods:

/**
 * Cleanest method IMO
 * @example <caption>index.html</caption> @lang html
 * <div></div>
 */

/**
 * I think the lang tag at the end is cleaner, but this still works
 * @example @lang html <caption>index.html</caption>
 * <div></div>
 */

/**
 * Still works with caption on a new line
 * @example
 * <caption>index.html</caption> @lang html
 * <div></div>
 */

This won't work:

/**
 * Both lang and caption must be on the same line to work
 * @example @lang html
 * <caption>index.html</caption> 
 * <div></div>
 */

/**
 * Using lang first will cause the example to close and thus be empty
 * @example
 * @lang html <caption>index.html</caption>
 * <div></div>
 */

To explain the commit, instead of deleting the first line if a caption is present, I extract both caption and lang from the first line (if available). Then if either of them is found, I delete the first line.

winterhotlatte commented 6 years ago

@ManBearTM What is the advantage of this over #57 ?

With your commit 2b40b9c689041aee9248805ea6854e9989a5d584 , no matter where you put the @lang, it will be processed, such as in this case:

 /**
 * @example <caption>funny text</caption>
 * var0: "awesome var"
 * var1: "very funny" @lang yaml
 */

The output:

**Example** *(funny text)*  
```yaml
var0: "awesome var"
var1: "very funny" @lang yaml

I am not sure it is supposed to work this way. What if the user actually wants to use `@lang` literally somewhere in example. Not only will it be processed, but also left within. Same goes for:

```javascript
 /**
 * @example
 * <caption>funny @lang yaml text</caption>
 * var0: "awesome var"
 * var1: "very funny"
 */

Output:

**Example** *(funny @lang yaml text)*  
```yaml
var0: "awesome var"
var1: "very funny"
ManBearTM commented 6 years ago

@foamy-latte Should've totally checked the existing pull requests beforehand šŸ˜‘I don't think there is any advantage over yours. Mine might be slightly simpler, but you seem to cover more edge cases. I think I'll just leave it here for @75lb to decide.

Good catch with the @lang. The current behavior is actually to search the entire example, but I changed it to search lines[0] instead of example, since it just makes sense. It will still match @lang anywhere on the first line, which might not be ideal, but I tried not to break existing behavior.

75lb commented 6 years ago

duplicate of #57, which was merged. Thanks anyway! šŸ‘