Skip to content
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 AbstractConvert & ConverterConstructor #1083

Merged
merged 1 commit into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions packages/core/types/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
Expand Down