import { ContentId, IContentStorage, IH5PConfig, ILibraryStorage, IUser } from './types'; /** * Creates standalone HTML packages that can be used to display H5P in a browser * without having to use the full H5P server backend. * * The bundle includes all JavaScript files, stylesheets, fonts of the H5P core * and all libraries used in the content. It also includes base64 encoded * resources used in the content itself. This can make the files seriously big, * if the content includes video files or lots of high-res images. * * The bundle does NOT internalize resources that are included in the content * via absolute URLs but only resources that are part of the H5P package. * * The HTML exports work with all content types on the official H5P Hub, but * there might be unexpected issues with other content types if they behave * weirdly and in any kind of non-standard way. * * The exported bundle contains license information for each file put into the * bundle in a shortened fashion (only includes author and license name and not * full license text). * * (important!) You need to install these NPM packages for the exporter to work: * postcss, postcss-clean, postcss-url, postcss-safe-parser, uglify-js */ export default class HtmlExporter { protected libraryStorage: ILibraryStorage; protected contentStorage: IContentStorage; protected config: IH5PConfig; protected coreFilePath: string; protected editorFilePath: string; /** * @param libraryStorage * @param contentStorage * @param config * @param coreFilePath the path on the local filesystem at which the H5P * core files can be found. (should contain a js and styles directory) * @param editorFilePath the path on the local filesystem at which the H5P * editor files can be found. (Should contain the scripts, styles and * ckeditor directories). */ constructor(libraryStorage: ILibraryStorage, contentStorage: IContentStorage, config: IH5PConfig, coreFilePath: string, editorFilePath: string); private contentFileScanner; private coreSuffix; private defaultAdditionalScripts; private editorSuffix; private player; /** * Creates a HTML file that contains **all** scripts, styles and library * resources (images and fonts) inline. All resources used inside the * content are only listed and must be retrieved from library storage by the * caller. * @param contentId a content id that can be found in the content repository * passed into the constructor * @param user the user who wants to create the bundle * @param contentResourcesPrefix (optional) if set, the prefix will be added * to all content files in the content's parameters; example: * contentResourcesPrefix = '123'; filename = 'images/image.jpg' => filename * in parameters: '123/images/image.jpg' (the directory separated is added * automatically) * @throws H5PError if there are access violations, missing files etc. * @returns a HTML string that can be written into a file and a list of * content files used by the content; you can use the filenames in * IContentStorage.getFileStream. Note that the returned filenames DO NOT * include the prefix, so that the caller doesn't have to remove it when * calling getFileStream. */ createBundleWithExternalContentResources(contentId: ContentId, user: IUser, contentResourcesPrefix?: string): Promise<{ contentFiles: string[]; html: string; }>; /** * Creates a single HTML file that contains **all** scripts, styles and * resources (images, videos, etc.) inline. This bundle will grow very large * if there are big videos in the content. * @param contentId a content id that can be found in the content repository * passed into the constructor * @param user the user who wants to create the bundle * @throws H5PError if there are access violations, missing files etc. * @returns a HTML string that can be written into a file */ createSingleBundle(contentId: ContentId, user: IUser): Promise; /** * Finds all files in the content's parameters and returns them. Also * appends the prefix if necessary. Note: This method has a mutating effect * on model! * @param model * @param prefix this prefix will be added to all file references as * subdirectory */ private findAndPrefixContentResources; /** * Generates JavaScript / CSS comments that includes license information * about a file. Includes: filename, author, license. Note that some H5P * libraries don't contain any license information. * @param filename * @param core * @param editor * @param library * @returns a multi-line comment with the license information. The comment * is marked as important and includes @license so that uglify-js and * postcss-clean leave it in. */ private generateLicenseText; /** * Gets the contents of a file as a string. Only works for text files, not * binary files. * @param filename the filename as generated by H5PPlayer. This can be a * path to a) a core file b) an editor file c) a library file * @returns an object giving more detailed information about the file: * - core: true if the file is a core file, undefined otherwise * - editor: true if the file is an editor file, undefined otherwise * - library: the library name if the file is a library file, undefined * otherwise * - filename: the filename if the suffix of the core/editor/library is * stripped * - text: the text in the file */ private getFileAsText; /** * Creates a big minified bundle of all script files in the model * @param model * @param additionalScripts an array of scripts (actual script code as * string, not filenames!) that should be appended at the end of the bundle * @returns all scripts in a single bundle */ private getScriptBundle; /** * Creates a big minified bundle of all style files in the model. Also * internalizes all url(...) resources in the styles. * @param model * @returns all styles in a single bundle */ private getStylesBundle; /** * Gets base64 encoded contents of library files that have not been used in * the bundle so far. Ignores files that are only used by the editor. * @param libraries the libraries for which to get files * @returns an object with the filenames of files as keys and base64 strings * as values */ private getUnusedLibraryFiles; /** * Changes the content params by internalizing all files references with * base64 data strings. Has a side effect on contents[cid-xxx]! * @param model */ private internalizeContentResources; /** * Returns true if the filename is not an absolute URL or empty. * @param filename */ private isLocalPath; /** * Creates HTML strings out of player models. * @param model the player model created by H5PPlayer * @returns a string with HTML markup */ private renderer; /** * A factory method that returns functions that can be passed to the url * option of postcss-url. The function returns the base64 encoded resource. * @param filename the filename of the css file being internalized * @param library the library name if the css file is a library file * @param editor true if the css file is a editor file * @param core true if the css file is a core file * @param asset the object received from the postcss-url plugin call */ private urlInternalizer; }