Comments (5)
How about if I just ignore that one line causing the whole lot this trouble.
// @ts-ignore
They're both band-aids over a gaping wound. If that's the band-aid you prefer, then that's fine. As long as you can remember that the TypeScript function signature is a lie.
from gray-matter.
I have been at it since hours and still couldn't fix it. I believe the signature for the excerpt
function is incorrect.
It should be something like
excerpt?: boolean | ((file: GrayMatterFile<I>, options: O) => void)
Should I make a PR?
from gray-matter.
It should be something like
excerpt?: boolean | ((file: GrayMatterFile<I>, options: O) => void)
@DarhkVoyd That would likely be the best change to make that's consistent with the real function signature. However there are a couple of flaws with the real function signature:
- It would be more intuitive for
excerpt
(optional field) to be a union of:boolean
— iftrue
, the default "extract excerpt" implementation would be used- Function returning a
string
— overrides default "extract excerpt" implementation string
— for backwards compatibility (until a major version is released) since the current implementation interprets astring
excerpt
likeexcerpt_separator
- The name
GrayMatterFile
is confusing since it implies some strong correlation with a file-system object but there is no correlation to speak of. It's really just an ad hoc global state dictionary being passed around between functions. And most of its fields aren't even necessary for findingexcerpt
.- In the first place,
input
(i.e. the first argument of theexcerpt
function type) should probably be something like astring
orBuffer
. (This would be a backwards incompatible change which should only be made on a major release).
- In the first place,
- The idea of relying on argument mutability as a public interface is dubious in the majority of cases.
To be clear, the existing TypeScript annotations are fine on their own; it's the implementation that needs to match.
Should I make a PR?
Probably not. Despite numerous issues and PRs, there hasn't been a significant change to this repository in over 5 years, so you'd probably be wasting your time. I don't blame the author since lack of monetizability in open source is a serious issue and they probably have better things to do with their time.
from gray-matter.
Here's a (relatively) sane wrapper for graymatter
taken straight from the source code for my website:
import type { GrayMatterFile, GrayMatterOption } from 'gray-matter'
import graymatter_ from 'gray-matter'
type GrayMatterOptions = GrayMatterOption<
string | Buffer,
GrayMatterOption<string | Buffer, any>
>
export type ExtractExcerptFn = (
input: string | Buffer,
options: GrayMatterOptions,
) => string
function graymatterInputToStringOrBuffer(
input: GrayMatterFile<string | Buffer>,
): string | Buffer {
if (typeof input !== 'object' || input === null) {
throw new Error(
`Expected input to be an object; received ${JSON.stringify(input)}`,
)
}
if (!('content' in input)) {
throw new Error('Expected "content" property in input object')
}
return input.content
}
function saneExcerptWrapper(
excerpt: boolean | ExtractExcerptFn | undefined,
): NonNullable<GrayMatterOptions>['excerpt'] {
if (typeof excerpt !== 'function') {
return excerpt
}
return ((input, options) => {
;(input as any).excerpt = (excerpt as ExtractExcerptFn)(
graymatterInputToStringOrBuffer(input as any),
options,
)
}) as typeof excerpt
}
export default function graymatter<
I extends string | Buffer,
O extends GrayMatterOption<I, O>,
>(input: I, options?: O): GrayMatterFile<I> {
return graymatter_(input, {
...options,
excerpt: saneExcerptWrapper(options?.excerpt),
})
}
from gray-matter.
How about if I just ignore that one line causing the whole lot this trouble.
// @ts-ignore
from gray-matter.
Related Issues (20)
- Can't parse datetime format
- ReferenceError: Buffer is not defined HOT 7
- typescript issues - is this package deprecated?
- Can't get values of double-nested entries
- Passing environment variable to the yaml file header HOT 2
- (Request) Any way to generate "square bracket" arrays rather than lists? HOT 1
- Add support for async excerpt functions? HOT 2
- Some kind of unwanted caching behaviour?
- `file.isEmpty` is not documented in typings
- gray-matter seems to be broken in Next 13 HOT 2
- Add Superscript Support
- Image for sponsorships
- android app issues
- Invalid front-matter should not be cached
- adding colon inside string breaks gray-matter parsing HOT 1
- Add Generic Data Typing for matter Frontmatter Parsing HOT 4
- Expose `YAMLException` from `js-yaml`
- grey-matter does not import as a function when using import statements HOT 2
- Reference issue
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gray-matter.