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

Fix the type of excerpt function #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sunnyone
Copy link

The "excerpt" option does not expect a function that returns a string but expects a function that mutates a file object itself.
https://github.com/jonschlinkert/gray-matter#optionsexcerpt

@petejohanson
Copy link

I hit this just now too working on a next.js project using this.

That being said, I'm really not sure the point of O, and that seems to trip things up more than it helps. Locally, I tried:

  interface GrayMatterOption<
    I extends Input,
  > {
    parser?: () => void
    eval?: boolean
    excerpt?: boolean | ((input: GrayMatterFile<I>, options: GrayMatterOption<I>) => string)
    excerpt_separator?: string
    engines?: {
      [index: string]:
        | ((input: string) => object)
        | { parse: (input: string) => object; stringify?: (data: object) => string }
    }
    language?: string
    delimiters?: string | [string, string]
  }

And this seems to make everything happy. With the semi-recursive O type, the TS compiler seems to trip up. With the change, I can do:

        const { data, content, excerpt } = matter(fileContents, {
            excerpt: (file: matter.GrayMatterFile<string>, opts: matter.GrayMatterOption<string>) => {
                file.excerpt = file.content.split('\n').slice(0, 5).join('\n')
            },
        })

And TS is happy.

Happy to submit an updated PR if folks like that.

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

Successfully merging this pull request may close these issues.

2 participants