typedoc2md / typedoc-plugin-markdown

A plugin for TypeDoc that enables TypeScript API documentation to be generated in Markdown.
https://typedoc-plugin-markdown.org
MIT License
725 stars 177 forks source link

`<` and `>` character are prefixed with `\` in method definitions #564

Closed nicfv closed 4 months ago

nicfv commented 10 months ago

I brought up an issue in #557 but it was overlooked and then the issue was closed.

When using generic types in class methods, this will add \ characters before the < and >. See example:

export class Example<T> {
    constructor() { }
    /**
     * Get the first item in `arr`
     */
    public first(arr: T[]): T {
        return arr[0];
    }
}

Which yields this markdown file:

[min-example](../README.md) / [Exports](../modules.md) / Example

# Class: Example\<T\>

## Type parameters

| Name |
| :------ |
| `T` |

## Table of contents

### Constructors

- [constructor](Example.md#constructor)

### Methods

- [first](Example.md#first)

## Constructors

### constructor

• **new Example**\<`T`\>(): [`Example`](Example.md)\<`T`\>

#### Type parameters

| Name |
| :------ |
| `T` |

#### Returns

[`Example`](Example.md)\<`T`\>

#### Defined in

index.ts:2

## Methods

### first

▸ **first**(`arr`): `T`

Get the first item in `arr`

#### Parameters

| Name | Type |
| :------ | :------ |
| `arr` | `T`[] |

#### Returns

`T`

#### Defined in

index.ts:6

Which mostly makes sense, but when combining with mkdocs, I get these weird artifacts:

image

Is it possible to create an option on how < and > are escaped? Or better yet, turn < into &lt; and > into &gt;. I tried replacing those characters with the HTML entities, and it worked perfectly.

nicfv commented 10 months ago

I just installed 4.0.0-next.47 by the way. It is amazing, great job, and thank you!

Unfortunately, I'm still experiencing this bug. :(

If not fixed, and someone comes across this post in the future, I added these lines to my workflow which seem to fix the problem. (These lines search and replace all instances of \< with &lt; and \> with &gt; in all markdown files in the docs/ directory.)

sed -i 's|\\<|\&lt;|g' docs/**/*.md
sed -i 's|\\>|\&gt;|g' docs/**/*.md

I could potentially see issues arising by using my code above, IF for example you have a URL that contains \< or \>. Since it will convert that to the HTML entity as well, some links might break. You could buff up that script to check to see if it's a markdown link, or the line contains http for example, and not run the replacement.

OR, if you have a comment that contains a code block with escaped square brackets, like this:

<hello>
\<hello>
<hello\>
\<hello\>

Then you'll end up with documentation looking like this:

<hello>
&lt;hello>
<hello&gt;
&lt;hello&gt;
tgreyuk commented 10 months ago

thanks - i'll have a think about the best solution is for this.

nicfv commented 9 months ago

Thank you for taking a look. I also created issue https://github.com/mkdocs/mkdocs/issues/3563 to see if they have anything to add to it.

tgreyuk commented 4 months ago

"useHTMLEncodedBrackets" option will html encode angle brackets (rather than escape backslash). Available in typedoc-plugin-markdown@4.2.0.