Skip to content

Commit 10b7f40

Browse files
authored
Add typescript definitions for Article and ReadabilityOptions objects. (mozilla#971)
* Add typescript definitions for Article and ReadabilityOptions objects. * fix: improve TypeScript type definition * removed non-existing attributes in ReadabilityOptions * use ReadabilityOptions in the Readability definition * add missing attributes * add explanatory commentary for all attributes of ReadabilityOptions * fix: remove unused `url` attribute of the ReadabilityOptions type * fix: callbacks doesn’t exist either
1 parent a07e62c commit 10b7f40

1 file changed

Lines changed: 68 additions & 12 deletions

File tree

index.d.ts

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Decides whether or not the document is reader-able without parsing the whole thing.
3-
* @return {boolean} Whether or not we suspect Readability.parse() will suceeed at returning an article object.
3+
* @return {boolean} Whether or not we suspect Readability.parse() will succeed at returning an article object.
44
*/
55
export function isProbablyReaderable(
66
document: Document,
@@ -14,20 +14,72 @@ export function isProbablyReaderable(
1414
}
1515
): boolean;
1616

17+
export interface ReadabilityOptions {
18+
/**
19+
* Whether to output debug messages. Defaults to `false`.
20+
*/
21+
debug?: boolean;
22+
/**
23+
* The maximum number of elements to parse. If the document exceeds this,
24+
* Readability will stop processing. Useful for performance on very large documents.
25+
* Defaults to 0 (no limit).
26+
*/
27+
maxElemsToParse?: number;
28+
/**
29+
* The number of top candidate nodes to consider when determining the main article content.
30+
* A higher number might lead to better results but could increase processing time.
31+
* Defaults to 5.
32+
*/
33+
nbTopCandidates?: number;
34+
/**
35+
* The minimum number of characters required for a text node to be considered
36+
* significant and included in the article content.
37+
* Defaults to 500.
38+
*/
39+
charThreshold?: number;
40+
/**
41+
* An array of class names to preserve. If `keepClasses` is `true`,
42+
* only classes in this array will be kept. Defaults to an empty array.
43+
*/
44+
classesToPreserve?: string[];
45+
/**
46+
* If `true`, Readability will retain the original class names of elements
47+
* in the parsed article content. If `classesToPreserve` is also set,
48+
* only those specified classes will be kept. Defaults to `false`.
49+
*/
50+
keepClasses?: boolean;
51+
/**
52+
* A function that serializes an HTML element into a string.
53+
* Defaults to `el => el.innerHTML`. This is used to get the content
54+
* of the parsed article.
55+
* @param el The Node to serialize.
56+
* @returns The HTML string representation of the element's content.
57+
*/
58+
serializer?: (el: Node) => string;
59+
/**
60+
* If `true`, Readability will not attempt to parse or extract
61+
* JSON-LD structured data from the document. Defaults to `false`.
62+
*/
63+
disableJSONLD?: boolean;
64+
/**
65+
* A regular expression used to validate video URLs. Only videos
66+
* matching this regex will be included in the parsed content.
67+
* Defaults to a regex that allows common video embedding platforms.
68+
*/
69+
allowedVideoRegex?: RegExp;
70+
/**
71+
* A modifier applied to the link density score of an element.
72+
* This influences how Readability judges the main content area,
73+
* potentially helping with documents that have many or few links.
74+
* Defaults to 1.
75+
*/
76+
linkDensityModifier?: number;
77+
}
78+
1779
export class Readability<T = string> {
1880
constructor(
1981
document: Document,
20-
options?: {
21-
debug?: boolean;
22-
maxElemsToParse?: number;
23-
nbTopCandidates?: number;
24-
charThreshold?: number;
25-
classesToPreserve?: string[];
26-
keepClasses?: boolean;
27-
serializer?: (node: Node) => T;
28-
disableJSONLD?: boolean;
29-
allowedVideoRegex?: RegExp;
30-
}
82+
options?: ReadabilityOptions
3183
);
3284

3385
parse(): null | {
@@ -62,3 +114,7 @@ export class Readability<T = string> {
62114
publishedTime: string | null | undefined;
63115
};
64116
}
117+
118+
// Assuming Article is the return type of Readability.prototype.parse()
119+
export type Article = ReturnType<Readability['parse']>;
120+

0 commit comments

Comments
 (0)