import schema from '@stencila/schema'; import * as vfile from '../util/vfile'; /** * Encoding options that are common to all codecs. * * Codecs are encouraged to respect these options but * are not forced to. Indeed, some options do not make sense for * some codecs. For example, for the PDF codec `isStandalone` * is always `true` so if `isStandalone: false` is supplied * as an option it will be ignored. Futhermore, some combinations * of options are pointless e.g. a `theme` when `isStandalone: false` */ export interface CommonEncodeOptions { format?: string; filePath?: string; isStandalone?: boolean; isBundle?: boolean; theme?: string; } /** * Default values for encoding options. * * This set of defaults provide a way of promoting consistency amongst * codecs. Instead of, for example, one codec defaulting * to `isStandalone: true` and another to `false`. It does not * enforce consistency however. */ type CommonEncodeDefaults = Required>; export declare const commonEncodeDefaults: CommonEncodeDefaults; /** * Decoding options that are common to all codecs. * * See notes for `CommonEncodeOptions` for how these are used. */ export interface CommonDecodeOptions { /** * The format to decode content from. * * Most codecs only decode from one format. However, * for those codecs that support multiple formats, * this options lets the user specify which one. */ format?: string; /** * Should the content be treated as a standalone * document or as a fragment? * * This option affects whether codecs will attempt * to extract metadata etc from the content. If this * option is `false`, some codecs will default to returning * an array of content nodes, instead of for example an * `Article` node. */ isStandalone?: boolean; /** * The node type to decode content to. * * Many codecs decode to a single node type. * e.g. the `docx` codec always decodes to an `Article`. * However, some `codecs` can decode to multiple node types. * e.g. the `yaml` and `xlsx` codecs. * This option allows the user to be explicit about which node type * they expect content to be decode to. */ asType?: keyof schema.Types; /** * Should the decoded node have the `reshape` function applied to it? */ shouldReshape?: boolean; } /** * Default values for encoding options. * * See notes for `commonEncodeDefaults` for why these exist. */ type CommonDecodeDefaults = Required>; export declare const commonDecodeDefaults: CommonDecodeDefaults; /** * The interface for a codec. * * Note that our use of the term "codec", is consistent with our usage elsewhere in Stencila * as something that creates or modifies executable document, and * differs from the usage of [`unified`](https://github.com/unifiedjs/unified#processorcodec). */ export declare abstract class Codec { /** * An array of [IANA Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml) * that the codec can decode/encode. */ readonly mediaTypes?: string[]; /** * Any array of file names to use to match the codec. * This can be useful for differentiating between * "flavors" of formats e.g. `datapackage.json` versus any old `.json` file. */ readonly fileNames?: string[]; /** * Any array of file name extensions to register for the codec. * This can be useful for specifying conversion to less well known media types * e.g. `--to tdp` for outputting `datapackage.json` to the console. */ readonly extNames?: string[]; /** * A function that does [content sniffing](https://en.wikipedia.org/wiki/Content_sniffing) * to determine if the codec is able to decode the content. As well as raw content, the content * string could be a file system path and the codec could do "sniffing" of the file system * (e.g. testing if certain files are present in a directory). */ readonly sniff?: (content: string) => Promise; /** * The default encode options for this codec */ protected commonEncodeDefaults: CommonEncodeDefaults; /** * The default decode options for this codec */ protected commonDecodeDefaults: CommonDecodeDefaults; /** * Decode a `VFile` to a `stencila.Node`. * * @param file The `VFile` to decode * @param options Decoding options * @returns A promise that resolves to a `stencila.Node` */ abstract readonly decode: (file: vfile.VFile, options?: DecodeOptions) => Promise; /** * Encode a `stencila.Node` to a `VFile`. * * @param node The `stencila.Node` to encode * @param options Encoding options * @returns A promise that resolves to a `VFile` */ abstract readonly encode: (node: schema.Node, options?: EncodeOptions) => Promise; /** * Decode a `stencila.Node` from a `string`. * * This is a convenience method which simply * reads the content of the `VFile` and passes * it to the `decode` method. * * @param content The `string` to decode * @param options Decoding options * @returns A promise that resolves to a `stencila.Node` */ load(content: string, options?: DecodeOptions): Promise; /** * Transform a node prior to dumping it. * * Defaults to "bundling" the file i.e reading * media content from files into data URIs. * This default can be overridden using the `isBundle: false` option * but is usually appropriate for `dump`ing to a string (cf `write` which * writes to files). * Derived classes may override this method to perform alternative * transformations to the node, prior to dumping it. * * @param node The `stencila.Node` to write * @param filePath The path of the file * @param options Encoding options */ preDump(node: schema.Node, options?: EncodeOptions): Promise; /** * Encode a `stencila.Node` to a `string`. * * This is a convenience method which simply * dumps the content of the `VFile` created by * the `encode` method. * * @param node The `stencila.Node` to dump * @returns A promise that resolves to a `string` */ dump(node: schema.Node, options?: EncodeOptions): Promise; /** * Read a `stencila.Node` from a file. * * This is a convenience method which reads the file into a `VFile`, * makes any references to local files absolute, and passes * it to the `decode` method. * * @param filePath The path of the file * @param options Decoding options * @returns A promise that resolves to a `stencila.Node` */ read(filePath: string, options?: DecodeOptions): Promise; /** * Transform a node prior to writing it. * * Writes data URI or file system media content to a sibling folder. * This default behavior can be overridden using the `isBundle: true` * option, but is usually appropriate for `write`ing files (cf `dump` * which bundles them. * * Derived classes may override this method to perform alternative * transformations to the node, prior to writing it. * * @param node The `stencila.Node` to write * @param filePath The path of the file * @param options Encoding options */ preWrite(node: schema.Node, filePath: string, options?: EncodeOptions): Promise; /** * Encode a `stencila.Node` to a file * * This is a convenience method which writes the content of the `VFile` created by * the `encode` method. * * @param node The `stencila.Node` to write * @param filePath The path of the file * @param options Encoding options */ write(node: schema.Node, filePath: string, options?: EncodeOptions): Promise; } export {};