From 61a3446458ea16af90228a5c6531b43a26f97c82 Mon Sep 17 00:00:00 2001 From: Titus Date: Wed, 14 Oct 2020 09:14:39 +0200 Subject: [PATCH] Update types Closes GH-539. Reviewed-by: Christian Murphy --- packages/remark-parse/types/index.d.ts | 109 +-------------------- packages/remark-parse/types/test.ts | 17 +--- packages/remark-stringify/types/index.d.ts | 47 +-------- packages/remark-stringify/types/test.ts | 22 +---- packages/remark/types/test.ts | 2 +- 5 files changed, 12 insertions(+), 185 deletions(-) diff --git a/packages/remark-parse/types/index.d.ts b/packages/remark-parse/types/index.d.ts index 6b774d10b..5460e2075 100644 --- a/packages/remark-parse/types/index.d.ts +++ b/packages/remark-parse/types/index.d.ts @@ -1,113 +1,14 @@ // TypeScript Version: 3.0 -import {Node, Parent, Position} from 'unist' -import {Parser, Plugin} from 'unified' - -declare class RemarkParser implements Parser { - parse(): Node - blockMethods: string[] - inlineTokenizers: { - [key: string]: remarkParse.Tokenizer - } - blockTokenizers: { - [key: string]: remarkParse.Tokenizer - } - - inlineMethods: string[] -} +import {Plugin} from 'unified' +import {Options} from 'mdast-util-from-markdown' declare namespace remarkParse { - interface Parse extends Plugin<[RemarkParseOptions?]> { - (options: RemarkParseOptions): void - Parser: typeof RemarkParser - } - - type Parser = RemarkParser - - interface RemarkParseOptions { - /** - * GFM mode - * - * Turns on: - * * Fenced code blocks - * * Autolinking of URLs - * * Deletions (strikethrough) - * * Task lists - * * Tables - * - * @defaultValue `true` - */ - gfm?: boolean - - /** - * CommonMark mode - * - * Allows: - * * Empty lines to split blockquotes - * * Parentheses (`(` and `)`) around link and image titles - * * Any escaped ASCII punctuation character - * * Closing parenthesis (`)`) as an ordered list marker - * * URL definitions in blockquotes - * - * Disallows: - * * Indented code blocks directly following a paragraph - * * ATX headings (# Hash headings) without spacing after opening hashes or and before closing hashes - * * Setext headings (`Underline headings\n---`) when following a paragraph - * * Newlines in link and image titles - * * White space in link and image URLs in auto-links (links in brackets, `<` and `>`) - * * Lazy blockquote continuation, lines not preceded by a greater than character (`>`), for lists, code, and thematic breaks - * - * @defaultValue `false` - */ - commonmark?: boolean - - /** - * Defines which HTML elements are seen as block level. - * - * @defaultValue blocks listed in - */ - blocks?: string[] + interface Parse extends Plugin<[RemarkParseOptions?]> {} - /** - * Pedantic mode - * - * Turns on: - * * Emphasis (`_alpha_`) and importance (`__bravo__`) with underscores in words - * * Unordered lists with different markers (`*`, `-`, `+`) - * * If commonmark is also turned on, ordered lists with different markers (`.`, `)`) - * * And removes less spaces in list items (at most four, instead of the whole indent) - * - * @defaultValue `false` - * @deprecated pedantic mode is buggy. It won’t be in micromark, which will be the basis of a future version of remark. - */ - pedantic?: boolean - } - - /** - * @deprecated Use `RemarkParseOptions` instead. - */ - type PartialRemarkParseOptions = RemarkParseOptions - - interface Add { - (node: Node, parent?: Parent): Node - test(): Position - reset(node: Node, parent?: Node): Node - } - - type Eat = (value: string) => Add - - type Locator = (value: string, fromIndex: number) => number - - interface Tokenizer { - (eat: Eat, value: string, silent: true): boolean | void - (eat: Eat, value: string): Node | void - locator?: Locator - onlyAtStart?: boolean - notInBlock?: boolean - notInList?: boolean - notInLink?: boolean - } + type RemarkParseOptions = Options } + declare const remarkParse: remarkParse.Parse export = remarkParse diff --git a/packages/remark-parse/types/test.ts b/packages/remark-parse/types/test.ts index 967e84608..d3b2dfda9 100644 --- a/packages/remark-parse/types/test.ts +++ b/packages/remark-parse/types/test.ts @@ -1,22 +1,7 @@ import unified = require('unified') import remarkParse = require('remark-parse') -const partialParseOptions: remarkParse.RemarkParseOptions = { - gfm: true, - pedantic: true -} - unified().use(remarkParse) -unified().use(remarkParse, partialParseOptions) - -const badParseOptions = { - gfm: 'true' -} // $ExpectError -unified().use(remarkParse, badParseOptions) - -const badParseOptionsInterface: remarkParse.RemarkParseOptions = { - // $ExpectError - gfm: 'true' -} +unified().use(remarkParse, {unknown: 1}) diff --git a/packages/remark-stringify/types/index.d.ts b/packages/remark-stringify/types/index.d.ts index d2c0dae28..467e8c14e 100644 --- a/packages/remark-stringify/types/index.d.ts +++ b/packages/remark-stringify/types/index.d.ts @@ -1,51 +1,12 @@ // TypeScript Version: 3.0 -import {Compiler, Processor, Plugin} from 'unified' -import {Node, Parent} from 'unist' - -declare class RemarkCompiler implements Compiler { - compile(): string - visitors: { - [key: string]: remarkStringify.Visitor - } -} +import {Plugin} from 'unified' +import {Options} from 'mdast-util-to-markdown' declare namespace remarkStringify { - interface Stringify extends Plugin<[RemarkStringifyOptions?]> { - Compiler: typeof RemarkCompiler - (this: Processor, options?: RemarkStringifyOptions): void - } - - type Compiler = RemarkCompiler - - interface RemarkStringifyOptions { - gfm?: boolean - commonmark?: boolean - entities?: boolean | 'numbers' | 'escape' - setext?: boolean - closeAtx?: boolean - tableCellPadding?: boolean - tablePipeAlign?: boolean - stringLength?: (s: string) => number - fence?: '~' | '`' - fences?: boolean - bullet?: '-' | '*' | '+' - listItemIndent?: 'tab' | '1' | 'mixed' - incrementListMarker?: boolean - tightDefinitions?: boolean - rule?: '-' | '_' | '*' - ruleRepetition?: number - ruleSpaces?: boolean - strong?: '_' | '*' - emphasis?: '_' | '*' - } - - /** - * @deprecated Use `RemarkStringifyOptions` instead. - */ - type PartialRemarkStringifyOptions = RemarkStringifyOptions + interface Stringify extends Plugin<[RemarkStringifyOptions?]> {} - type Visitor = (node: Node, parent?: Parent) => string + type RemarkStringifyOptions = Options } declare const remarkStringify: remarkStringify.Stringify diff --git a/packages/remark-stringify/types/test.ts b/packages/remark-stringify/types/test.ts index ed8b9c051..8807396cc 100644 --- a/packages/remark-stringify/types/test.ts +++ b/packages/remark-stringify/types/test.ts @@ -13,7 +13,7 @@ unified().use(remarkStringify) unified().use(remarkStringify, inferredStringifyOptions) // These cannot be automatically inferred by TypeScript -const nonInferredStringifyOptions: remarkStringify.PartialRemarkStringifyOptions = { +const nonInferredStringifyOptions: remarkStringify.RemarkStringifyOptions = { fence: '~', bullet: '+', listItemIndent: 'tab', @@ -31,27 +31,7 @@ const badStringifyOptions = { // $ExpectError unified().use(remarkStringify, badStringifyOptions) -function gap(this: unified.Processor) { - const Compiler = this.Compiler as typeof remarkStringify.Compiler - const visitors = Compiler.prototype.visitors - const original = visitors.heading - - visitors.heading = heading - - function heading(this: unified.Processor, node: Node, parent?: Parent) { - // FIXME: remove need for explicit 'as' casting - const headingNode = node as Node & {depth: number} - return ( - (headingNode.depth === 2 ? '\n' : '') + - original.apply(this, [node, parent]) - ) - } -} - -const plugin: unified.Plugin = gap - const incompleteStringifyOptions: remarkStringify.RemarkStringifyOptions = { - gfm: true, fences: true, incrementListMarker: false } diff --git a/packages/remark/types/test.ts b/packages/remark/types/test.ts index 92a4c10d3..74a60ced5 100644 --- a/packages/remark/types/test.ts +++ b/packages/remark/types/test.ts @@ -9,7 +9,7 @@ const plugin = (options?: PluginOptions) => {} remark.parse('# Hello world!') remark().use(plugin) remark().use(plugin, {example: true}) -remark().use({settings: {commonmark: true}}) +remark().use({settings: {bullet: '+'}}) // $ExpectError remark().use({settings: {doesNotExist: true}}) // $ExpectError