All files index.js

86.95% Statements 60/69
83.33% Branches 20/24
100% Functions 4/4
87.87% Lines 58/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147    3x 3x     3x 3x 3x 3x 3x   3x 3x               3x   3x 3x   3x 3x 3x 3x                                   3x     3x   3x                     3x                 3x 3x             3x 3x 2x 2x         1x 1x 1x 1x 1x 1x     1x   1x   1x 29x 2x 2x 2x 2x   27x         27x         27x 3x   24x 11x 11x 13x 2x 2x 2x 11x 2x 2x 2x       1x 1x             3x        
/*jshint esversion: 8 */
 
const path = require('path');
const puppeteer = require('puppeteer');
 
async function processBlock(blk) {
    const book = this;
    let code = blk.body;
    const config = book.config.get('pluginsConfig.sequence', {});
    const width = blk.kwargs.width;
    const height = 'Reserved';
 
    try {
        const browser = await puppeteer.launch({
            args: [
                '--disable-dev-shm-usage',
                '--no-sandbox',
                '--allow-file-access-from-files',
                '--enable-local-file-accesses'
            ]
        });
        const page = await browser.newPage();
 
        const htmlFile = path.join(__dirname, 'renderer.html');
        await page.goto("file://" + htmlFile, { waitUntil: 'domcontentloaded' });
 
        const xCode = encodeURIComponent(code);
        const xConfig = encodeURIComponent(JSON.stringify(config));
        const xWidth = encodeURIComponent(width);
        const xHeight = encodeURIComponent(height);
 
        /* istanbul ignore next */
        const result = await page.evaluate(
            `(async () => {
                code = decodeURIComponent("${xCode}");
                config = JSON.parse(decodeURIComponent("${xConfig}"));
                width = decodeURIComponent("${xWidth}");
                height = decodeURIComponent("${xHeight}");
 
                window.renderComplete = false;
                const rendered = await render(code, config, width);
                window.renderComplete = true;  // Flag to indicate completion
                return rendered;
            })()`
        );
 
        // Poll for render completion
        await page.waitForFunction('window.renderComplete === true', {
            timeout: 50000
        });
        await browser.close();
 
        return result;
    } catch (error) {
        console.error("Error processing block:", error);
        return `<svg version="1.1" width="600" height="200" xmlns="http://www.w3.org/2000/svg">
            <text x="10" y="100" font-size="60" text-anchor="left">
                Failed to render sequence diagram. Check console for details.
            </text>
        </svg>`;
    }
}
 
module.exports = {
    blocks: {
        sequence: {
            process: processBlock
        }
    },
    hooks: {
        // Initialize plugin and read config
        "init": function () {
            Eif (!Object.keys(this.book.config.get('pluginsConfig.sequence', {})).length) {
                this.book.config.set('pluginsConfig.sequence', {
                    'theme': 'simple'
                });
            }
        },
        // Before parsing markdown
        "page:before": function (page) {
            let flows = page.content.match(/```(\x20|\t)*(sequence)((.*[\r\n]+)+?)?```/igm);
            if (Array.isArray(flows)) {
                for (let i = 0; i < flows.length; i++) {
                    page.content = page.content.replace(
                        flows[i],
                        flows[i]
                            .replace(/```(\x20|\t)*(sequence)[ \t]+{(.*)}/i,
                                function (matchedStr) {
                                    Iif (!matchedStr) return "";
                                    let newStr = "";
                                    let modeQuote = false;
                                    let modeArray = false;
                                    let modeChar = false;
                                    let modeEqual = false;
 
                                    // Trim left and right space
                                    let str = matchedStr.replace(/^\s+|\s+$/g, "");
                                    // Remove ```sequence header
                                    str = str.replace(/```(\x20|\t)*(sequence)/i, "");
 
                                    for (let j = 0; j < str.length; j++) {
                                        if (str.charAt(j) === "\"") {
                                            modeQuote = !modeQuote;
                                            modeChar = true;
                                            newStr += str.charAt(j);
                                            continue;
                                        }
                                        Iif (str.charAt(j) === "[") {
                                            modeArray = true;
                                            newStr += str.charAt(j);
                                            continue;
                                        }
                                        Iif (str.charAt(j) === "]") {
                                            modeArray = false;
                                            newStr += str.charAt(j);
                                            continue;
                                        }
                                        if (modeQuote || modeArray) {
                                            newStr += str.charAt(j);
                                        } else {
                                            if (/[A-Za-z0-9_]/.test(str.charAt(j))) {
                                                modeChar = true;
                                                newStr += str.charAt(j);
                                            } else if (str.charAt(j) === "=") {
                                                modeEqual = true;
                                                modeChar = false;
                                                newStr += str.charAt(j);
                                            } else if (modeChar && modeEqual) {
                                                modeChar = false;
                                                modeEqual = false;
                                                newStr += ",";
                                            }
                                        }
                                    }
                                    newStr = newStr.replace(/,$/, "");
                                    return `{% sequence ${newStr} %}`;
                                })
                            .replace(/```(\x20|\t)*(sequence)/i, '{% sequence %}')
                            .replace(/```/, '{% endsequence %}')
                    );
                }
            }
            return page;
        }
    }
};