<commit_before>"use strict";

import * as vscode from "vscode";
import * as path from "path";

import * as beautifyJS from "js-beautify";
import fileUrl = require("file-url");
import mjml2html = require("mjml");

export default class Helper {

    static renderMJML(cb: (content: string) => any, fixLinks?: boolean, minify?: boolean, beautify?: boolean): any {
        if (!(this.isMJMLFile(vscode.window.activeTextEditor.document))) {
            vscode.window.showWarningMessage("This is not a MJML document!");
            return;
        }

        let content: string = this.mjml2html(
            vscode.window.activeTextEditor.document.getText(),
            minify != undefined ? minify : vscode.workspace.getConfiguration("mjml").minifyHtmlOutput,
            beautify != undefined ? beautify : vscode.workspace.getConfiguration("mjml").beautifyHtmlOutput,
        ).html;

        if (content) {
            if (fixLinks != undefined && fixLinks) {
                content = this.fixLinks(content);
            }

            return cb(content);
        }
        else {
            vscode.window.showErrorMessage(`MJMLError: Failed to parse file ${path.basename(vscode.window.activeTextEditor.document.uri.fsPath)}`);
        }
    }

    static isMJMLFile(document: vscode.TextDocument): boolean {
        return document.languageId === "mjml" && document.uri.scheme !== "mjml-preview";
    }

    static mjml2html(mjml: string, minify: boolean, beautify: boolean, mjmlPath?: string, level: 'skip' | 'strict' | 'ignore' = 'skip' ): { html: string, errors: any[] } {
        try {
            if (!mjmlPath) {
                mjmlPath = this.getPath();
            }

            return mjml2html(mjml, {
                level: level,
                minify: minify,
                beautify: beautify,
                filePath: mjmlPath,
                configPath: this.getCWD(mjmlPath)
            });

        }
        catch (err) {
            return;
        }
    }

    static getCWD(mjmlPath?: string): string {
        if (vscode.workspace.rootPath) {
            return vscode.workspace.rootPath;
        }
        else {
            return (mjmlPath) ? path.parse(mjmlPath).dir : "";
        }
    }

    static fixLinks(text: string, mjmlPath?: string): string {
        return text.replace(
            new RegExp(/((?:src|href|url)(?:=|\()(?:[\'\"]|))((?!http|\\|"|#).+?)([\'\"]|\))/, "gmi"),
            (subString: string, p1: string, p2: string, p3: string): string => {
                return [p1, fileUrl(path.join(path.dirname(((mjmlPath) ? mjmlPath : this.getPath())), p2)), p3].join("");<commit_msg>Return empty html & errors when mjml2html throws<commit_after>"use strict";

import * as vscode from "vscode";
import * as path from "path";

import * as beautifyJS from "js-beautify";
import fileUrl = require("file-url");
import mjml2html = require("mjml");

export default class Helper {

    static renderMJML(cb: (content: string) => any, fixLinks?: boolean, minify?: boolean, beautify?: boolean): any {
        if (!(this.isMJMLFile(vscode.window.activeTextEditor.document))) {
            vscode.window.showWarningMessage("This is not a MJML document!");
            return;
        }

        let content: string = this.mjml2html(
            vscode.window.activeTextEditor.document.getText(),
            minify != undefined ? minify : vscode.workspace.getConfiguration("mjml").minifyHtmlOutput,
            beautify != undefined ? beautify : vscode.workspace.getConfiguration("mjml").beautifyHtmlOutput,
        ).html;

        if (content) {
            if (fixLinks != undefined && fixLinks) {
                content = this.fixLinks(content);
            }

            return cb(content);
        }
        else {
            vscode.window.showErrorMessage(`MJMLError: Failed to parse file ${path.basename(vscode.window.activeTextEditor.document.uri.fsPath)}`);
        }
    }

    static isMJMLFile(document: vscode.TextDocument): boolean {
        return document.languageId === "mjml" && document.uri.scheme !== "mjml-preview";
    }

    static mjml2html(mjml: string, minify: boolean, beautify: boolean, mjmlPath?: string, level: 'skip' | 'strict' | 'ignore' = 'skip' ): { html: string, errors: any[] } {
        try {
            if (!mjmlPath) {
                mjmlPath = this.getPath();
            }

            return mjml2html(mjml, {
                level: level,
                minify: minify,
                beautify: beautify,
                filePath: mjmlPath,
                configPath: this.getCWD(mjmlPath)
            });

        }
        catch (err) {
            return { html: '', errors: [err] };
        }
    }

    static getCWD(mjmlPath?: string): string {
        if (vscode.workspace.rootPath) {
            return vscode.workspace.rootPath;
        }
        else {
            return (mjmlPath) ? path.parse(mjmlPath).dir : "";
        }
    }

    static fixLinks(text: string, mjmlPath?: string): string {
        return text.replace(
            new RegExp(/((?:src|href|url)(?:=|\()(?:[\'\"]|))((?!http|\\|"|#).+?)([\'\"]|\))/, "gmi"),
            (subString: string, p1: string, p2: string, p3: string): string => {
                return [p1, fileUrl(path.join(path.dirname(((mjmlPath) ? mjmlPath : this.getPath())), p2)), p3].join("");
{'fraction_non_alphanumeric': 0.14743950478334272, 'fraction_numerical': 0.0039392234102419805, 'mean_word_length': 2.215922798552473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608003', 'n_tokens_mistral': 1681, 'n_tokens_neox': 1622, 'n_words': 338}