Fix the type of excerpt function #111

Open
sunnyone wants to merge 1 commit from sunnyone/fix-excerpt-type into master
sunnyone commented 2020-07-29 15:45:35 +00:00 (Migrated from github.com)

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

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 commented 2021-08-13 14:18:54 +00:00 (Migrated from github.com)

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.

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.
This pull request has changes conflicting with the target branch.
  • gray-matter.d.ts
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin "sunnyone/fix-excerpt-type:sunnyone/fix-excerpt-type"
git switch "sunnyone/fix-excerpt-type"
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rtlabs-llm-agents/gms-gray-matter!111
No description provided.