From 02be5edae640c655e86ae06d80b6961086378bb5 Mon Sep 17 00:00:00 2001 From: Charles Vandevoorde Date: Thu, 18 Jan 2018 21:50:13 +0100 Subject: [PATCH] add mozilla/readability types (#23038) --- types/mozilla-readability/index.d.ts | 43 +++++++++++++++++ .../mozilla-readability-tests.ts | 46 +++++++++++++++++++ types/mozilla-readability/tsconfig.json | 24 ++++++++++ types/mozilla-readability/tslint.json | 1 + 4 files changed, 114 insertions(+) create mode 100644 types/mozilla-readability/index.d.ts create mode 100644 types/mozilla-readability/mozilla-readability-tests.ts create mode 100644 types/mozilla-readability/tsconfig.json create mode 100644 types/mozilla-readability/tslint.json diff --git a/types/mozilla-readability/index.d.ts b/types/mozilla-readability/index.d.ts new file mode 100644 index 0000000000..a30925fb0b --- /dev/null +++ b/types/mozilla-readability/index.d.ts @@ -0,0 +1,43 @@ +// Type definitions for mozilla-readability 0.1 +// Project: https://github.com/mozilla/readability +// Definitions by: Charles Vandevoorde +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +export = Readability; + +declare class Readability { + constructor(uri: Readability.Uri, doc: Document, options?: Readability.Options); + + parse(): Readability.ParseResult; + isProbablyReaderable(helperIsVisible?: (node: any) => boolean): boolean; +} + +declare namespace Readability { + interface Uri { + spec: string; + host: string; + prePath: string; + scheme: string; + pathBase: string; + } + + interface Options { + debug?: boolean; + maxElemsToParse?: number; + nbTopCandidates?: number; + wordThreshold?: number; + classesToPreserve?: string[]; + } + + interface ParseResult { + uri: Uri; + title: string; + byline: string; + dir: string; + content: string; + textContent: string; + length: number; + excerpt: string; + } +} diff --git a/types/mozilla-readability/mozilla-readability-tests.ts b/types/mozilla-readability/mozilla-readability-tests.ts new file mode 100644 index 0000000000..bcb17f4bf5 --- /dev/null +++ b/types/mozilla-readability/mozilla-readability-tests.ts @@ -0,0 +1,46 @@ +import * as Readability from 'mozilla-readability'; +import { JSDOM } from 'jsdom'; + +// Compiling requires `--noImplicitUseStrict` +// because issue https://github.com/mozilla/readability/issues/346 +// requires global variable `Node` when using nodejs. + +const fakeUri: Readability.Uri = { + spec: "http://fakehost/test/page.html", + host: "fakehost", + prePath: "http://fakehost", + scheme: "http", + pathBase: "http://fakehost/test/" +}; + +function test_basic_usage() { + const dom = new JSDOM(`

Hello

Hi!`); + // Required until https://github.com/mozilla/readability/issues/346 + // is fixed. + Node = dom.window.Node; + + const reader = new Readability(fakeUri, dom.window.document); + const article = reader.parse(); +} + +function test_readability_with_options() { + const dom = new JSDOM(`

Hello

Hi!`); + // Required until https://github.com/mozilla/readability/issues/346 + // is fixed. + Node = dom.window.Node; + + const options: Readability.Options = { + debug: true, + maxElemsToParse: 100, + }; + const article = new Readability(fakeUri, dom.window.document, options).parse(); +} + +function test_is_probably_readerable() { + const dom = new JSDOM(`

Hello

Hi!`); + // Required until https://github.com/mozilla/readability/issues/346 + // is fixed. + Node = dom.window.Node; + + const isReadable = new Readability(fakeUri, dom.window.document).isProbablyReaderable(); +} diff --git a/types/mozilla-readability/tsconfig.json b/types/mozilla-readability/tsconfig.json new file mode 100644 index 0000000000..007aac8b63 --- /dev/null +++ b/types/mozilla-readability/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "mozilla-readability-tests.ts" + ] +} diff --git a/types/mozilla-readability/tslint.json b/types/mozilla-readability/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/mozilla-readability/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }