From 3384a6bdab7addc8095aa0ff6fabd3cb2e276946 Mon Sep 17 00:00:00 2001 From: bitPogo <33144205+bitPogo@users.noreply.github.com> Date: Sat, 31 Oct 2020 16:42:43 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20AbstractConvert=20&=20Convert?= =?UTF-8?q?erConstructor=20(#1083)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/types/index.d.ts | 25 ++++++++++++++++++++++--- packages/core/types/tests.ts | 13 +++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/core/types/index.d.ts b/packages/core/types/index.d.ts index a57907caf..4b5ad86a4 100644 --- a/packages/core/types/index.d.ts +++ b/packages/core/types/index.d.ts @@ -2980,7 +2980,26 @@ export namespace Asciidoctor { function create(): Html5Converter; } - class Converter { + interface AbstractConverter { + /** + * Converts an {AbstractNode} using the given transform. + * This method must be implemented by a concrete converter class. + * + * @param node - The concrete instance of AbstractNode to convert. + * @param [transform] - An optional String transform that hints at which transformation should be applied to this node. + * If a transform is not given, the transform is often derived from the value of the {AbstractNode#getNodeName} property. (optional, default: undefined) + * @param [opts]- An optional JSON of options hints about how to convert the node. (optional, default: undefined) + * + * @returns the {String} result. + */ + convert(node: AbstractNode, transform?: string, opts?: unknown): string; + } + + interface ConverterConstructor { + new(backend?: string, opts?: unknown): AbstractConverter; + } + + class Converter implements AbstractConverter { /** * Converts an {AbstractNode} using the given transform. * This method must be implemented by a concrete converter class. @@ -3009,7 +3028,7 @@ export namespace Asciidoctor { * @param opts - a JSON of options to pass to the converter (default: {}) * @returns a {Converter} instance for converting nodes in an Asciidoctor AST. */ - function create(backend: string, opts?: any): Converter; + function create(backend: string, opts?: unknown): Converter; } /** @@ -3027,7 +3046,7 @@ export namespace Asciidoctor { * @param converter - The {Converter} instance to register * @param backends- A {string} {Array} of backend names that this converter should be registered to handle (optional, default: ['*']) */ - register(converter: any, backends?: string[]): void; + register(converter: AbstractConverter | ConverterConstructor, backends?: string[]): void; /** * Retrieves the singleton instance of the converter factory. diff --git a/packages/core/types/tests.ts b/packages/core/types/tests.ts index faa81a794..22dbd1344 100644 --- a/packages/core/types/tests.ts +++ b/packages/core/types/tests.ts @@ -510,7 +510,7 @@ interface Transforms { [key: string]: (node: Asciidoctor.AbstractNode) => string; } -class BlogConverter { +class BlogConverter implements Asciidoctor.AbstractConverter { private readonly baseConverter: Asciidoctor.Html5Converter; private readonly transforms: Transforms; @@ -1076,12 +1076,21 @@ assert(blockSubs5[0] === 'specialcharacters'); let converterRegistry = processor.ConverterFactory.getRegistry(); assert(typeof converterRegistry.html5 === 'function'); -class BlankConverter { +class BlankConverter implements Asciidoctor.AbstractConverter { convert() { return ''; } } +const BlankConstructor = BlankConverter as Asciidoctor.ConverterConstructor; +processor.ConverterFactory.register(BlankConstructor, ['blank']); +converterRegistry = processor.ConverterFactory.getRegistry(); +assert(typeof converterRegistry.html5 === 'function'); +assert(typeof converterRegistry.blank === 'function'); +assert(typeof processor.ConverterFactory.for('html5') === 'function'); +assert(typeof processor.ConverterFactory.for('blank') === 'function'); +assert(typeof processor.ConverterFactory.for('foo') === 'undefined'); + processor.ConverterFactory.register(new BlankConverter(), ['blank']); converterRegistry = processor.ConverterFactory.getRegistry(); assert(typeof converterRegistry.html5 === 'function');