van abel
van abel

Reputation: 181

How to replace for array?

The following code (from stackedit -- a markdown editor) will replace

\begin{thm}
blabla...
\end{thm}

with

<div class="latex_thm">
blabla...
</div>

The code is:

userCustom.onEditorConfigure = function(editor) {
    var converter = editor.getConverter();
    converter.hooks.chain("preConversion", function(text) {
        return text.replace(/\\begin{thm}([\s\S]*?)\\end{thm}/g, function(wholeMatch, m1) {
            return '<thm>' + m1 + '</thm>';
        });
    });
    converter.hooks.chain("preBlockGamut", function(text, blockGamutHookCallback) {
        return text.replace(/<thm>([\s\S]*?)<\/thm>/g, function(wholeMatch, m1) {
            return '<div class="latex_thm">' + blockGamutHookCallback(m1) + '</div>';
        });
    });
};

But If I want to not only replace thm with latex_thm but also lem with latex_lem, how to do this? I think maybe solve it with array? But it sames not work for me:

userCustom.onEditorConfigure = function(editor) {
    var converter = editor.getConverter();
    converter.hooks.chain("preConversion", function(text) {
        var array = {
            "thm": "thm",
            "lem": "lem"
        };
        for (var val in array) {
            return text.replace(/\\begin{array[val]}([\s\S]*?)\\end{array[val]}/g, function(wholeMatch, m1) {
                return '<div class="latex_"' + array[val] + '>' + m1 + '</div>';
            });
        };
    });
};
};

Could you help me out?

Upvotes: 0

Views: 125

Answers (1)

georg
georg

Reputation: 214949

something like (untested):

   converter.hooks.chain("preConversion", function(text) {
        return text.replace(/\\begin{(thm|lem)}([\s\S]*?)\\end{\1}/g, function(wholeMatch, m1, m2) {
            return '<' + m1 + '>' + m2 + '</' + m1 + '>';
        });
    });
    converter.hooks.chain("preBlockGamut", function(text, blockGamutHookCallback) {
        return text.replace(/<(thm|lem)>([\s\S]*?)<\/\1>/g, function(wholeMatch, m1, m2) {
            return '<div class="latex_' + m1 + '">' + blockGamutHookCallback(m2) + '</div>';
        });
    });

Upvotes: 1

Related Questions