Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chris-dura opened this issue May 5, 2022 · 1 comment
Closed

Comments

@chris-dura
Copy link

chris-dura commented May 5, 2022

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
Copy link
Owner

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants