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

Customizable description extensions for better / more reliable descriptions. #159

Open
taylorhadden opened this issue Dec 27, 2024 · 1 comment

Comments

@taylorhadden
Copy link

taylorhadden commented Dec 27, 2024

I've noticed that the "description" field defaults to nothing when there are no defined values in <head>. This means that websites like Wikipedia have empty descriptions, whereas user expectation would likely be that the description is a snippet of the first paragraph of the article (for example).

It would be very useful to be able to add a custom description handler to the options that could handle this case. e.g.

function parseTextResponse(
  ...,
  options: ILinkPreviewOptions = {},
  ...
) {
  const doc = cheerio.load(body);

  return {
    ...
    description: getDescription(doc) || options.descriptionFallback ? options.descriptionFallback(doc) : null
    ....
  };
}

I could certainly do something like write my own fetch & processing behavior, only passing to link-preview-js when I need a generic fallback, but I'd rather have an integrated solution.

Another alternative could be some post-process callback that gives you the result and the cheerio doc so that you can make changes. e.g. used like:

getLinkPreview('http://foo.bar', {
   postProcessText: (result, doc) => {
      if (!result.description) {
         result.description = doc('article p').text()
         // Or some site-specific customization
      }
   }
}).then(...)

Would you be interested in a push request along these lines?

@ospfranco
Copy link
Collaborator

I think the post process callback is the most flexible. Feel free to open a PR. Don't forget to update the README please.

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