valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
291 stars 72 forks source link

Should return type for `PermalinkGenerator` be `void` instead of `string`? #115

Closed chris-dura closed 2 years ago

chris-dura commented 2 years ago

Hello!

TL;DR --

The types say that my custom PermalinkGenerator should return a string: https://github.com/valeriangalliat/markdown-it-anchor/blob/master/types/index.d.ts#L39

But, the docs for custom permalinks don't show a return type: https://github.com/valeriangalliat/markdown-it-anchor#custom-permalink

And, it doesn't look like the built-in Permalink functions (headerLink, linkInsideHeader, linkAfterHeader) return strings either?

So, what string am I supposed to return in my custom function renderPermalink (slug, opts, state, idx) {}?


More Details...

I'm using VuePress, and I want to configure my own custom permalink, using the permalink option for markdown-it-anchor.

The types for markdown-it-anchor show that a PermalinkGenerator should return a string

However, the docs for custom permalinks don't show a return type, so I'm getting an ESLint error because I don't return a string in my custom function...

// .vuepress/config.ts
...
markdown: {
  anchor: {
    permalink: (slug, opts, state, idx) => {
      state.tokens.splice(idx, 0, Object.assign(new state.Token('html_block', '', 0), {
        content: `<div>test</div>`
      }));
    }
  }
},
...
(property) anchor.AnchorOptions.permalink?: anchor.PermalinkGenerator
Type '(slug: string, opts: PermalinkOptions, state: StateCore, idx: number) => void' is not assignable to type 'PermalinkGenerator'.
  Type 'void' is not assignable to type 'string'.ts(2322)

If I return a random string, that satisfies ESLint and the permalink still renders <div>test</div>, so should I just return an empty string?

// .vuepress/config.ts
...
markdown: {
  anchor: {
    permalink: (slug, opts, state, idx) => {
      state.tokens.splice(idx, 0, Object.assign(new state.Token('html_block', '', 0), {
        content: `<div>test</div>`
      }));

      return 'what should go here?';
    }
  }
},
...
valeriangalliat commented 2 years ago

Thanks for the detailed issue!

You're right this is a mistake with the return type for PermalinkGenerator and it should indeed be void. It's fixed with v8.6.3. Cheers :)