-
Notifications
You must be signed in to change notification settings - Fork 615
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
Add typescript definition #667
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Decides whether or not the document is reader-able without parsing the whole thing. | ||
* @return {boolean} Whether or not we suspect Readability.parse() will suceeed at returning an article object. | ||
*/ | ||
export function isProbablyReaderable( | ||
document: Document, | ||
options?: { | ||
/** The minimum node content length used to decide if the document is readerable. */ | ||
minContentLength?: number; | ||
/** The minumum cumulated 'score' used to determine if the document is readerable. */ | ||
minScore?: number; | ||
/** The function used to determine if a node is visible. */ | ||
visibilityChecker?: (node: Node) => boolean; | ||
} | ||
): boolean; | ||
|
||
export class Readability<T = string> { | ||
constructor( | ||
document: Document, | ||
options?: { | ||
debug?: boolean; | ||
maxElemsToParse?: number; | ||
nbTopCandidates?: number; | ||
charThreshold?: number; | ||
classesToPreserve?: string[]; | ||
keepClasses?: boolean; | ||
serializer?: (node: Node) => T; | ||
disableJSONLD?: boolean; | ||
} | ||
); | ||
|
||
parse(): null | { | ||
/** article title */ | ||
title: string; | ||
/** author metadata */ | ||
byline: string; | ||
/** content direction */ | ||
dir: string; | ||
/** HTML of processed article content */ | ||
content: T; | ||
/** text content of the article (all HTML removed) */ | ||
textContent: string; | ||
/** length of an article, in characters */ | ||
length: number; | ||
/** article description, or short excerpt from the content */ | ||
excerpt: string; | ||
siteName: string; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused - what other T is supported here? Why not just not have this template-like construction and use
string
literally below?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by "literally below". This makes sure that the type of
content
will beT
(which is the return type ofserializer
and defaults tostring
if there is no serializer) (see #667 (comment))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, hm. That's... interesting. @danburzo is there a particular reason we shouldn't require it to be a string (even if it can be a non-string right now)? Feels odd to have something called "serializer" that then does other transforms and produces something other than a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We introduced
serializer
to support other types of DOM serialization (e.g. XHTML/XML for EPUB, #605). In my particular case, where Readability is part of a larger processing pipeline, I found it's actually more efficient to skip an entire serialization/parse pair altogether:In my previous comment, I had suggested the change in the TypeScript definition for the sake of accuracy, but if this detail of the
serializer
option obscures the class as a whole, I think we can safely pretend it always returns a string here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. Sorry for not doing a good job at reading before. No-op seems more in keeping (like saying somehow "don't serialize this at all"). We could make it return
Node | string
, assuming the typescript definition file allows for that? Then it won't require parametrization/templatization of the class. We should probably clarify this in the jsdoc and/or docs though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it's a bit hard to have the return type of
parse
depends on the return type ofserializer
without having it in a template. IMO it's more important to have an exact return type forparse
rather than forserializer
.The template is transparent for the user, they won't ever need to use it so I don't think it's obscuring the class too much?
But I agree it should be clarified in the docs (none of the options are documented at the moment)