0

I recently learned in the chat section that if you use a bookmark you can render LaTeX:

http://meta.math.stackexchange.com/a/3297

The stackexchange sites all render code like this. Anything in between `` gets rendered as code.

I find this feature quite nice and useful. I was wondering if anyone knows how to get the javascript that does this in the stackexchange sites and put it as a bookmark just like the mathjax bookmarks found here:

http://www.math.ucla.edu/~robjohn/math/mathjax.html

That way if I write something with the backtick escapes say in facebook or any other site I could just click on my bookmark that says renderCode and the javascript will do what the same as it is doing in this site.

Anyone know if this has been asked before and/or how to achieve this? Thanks

4

1 回答 1

2

这是你的一个开始:

var replacer = function(s, match) {return "<code>" + match + "</code>";};

"some `delimited` code `sections` here". replace(/`([^`]*)`/g, replacer);
    // ==> "some <code>delimited</code> code <code>sections</code> here"

您可以使用任何您喜欢的标记来代替“<code>”和“</code>”来创建您喜欢的效果。

你也可以使用这样的函数:

processTextNodes = function(element, fn) {
    var children = element.childNodes;
    for (var i = 0; i < children.length; i++) {
        var node = children[i];
        var type = node.nodeType;
        if (type == 1) processTextNodes(node, fn);
        if (type == 3) fn.call(this, node);
    }
}

像这样:

processTextNodes(someElement, function(node) {
    node.value = node.value.replace(/`([^`]*)`/g, replacer);
});

为了将其应用于元素内的所有文本节点。

您仍然必须将其变成书签,并弄清楚如何找到正确的元素。你需要标记和 CSS 来显示你喜欢的输出。但这可能是一个好的开始。

于 2012-07-15T02:08:45.883 回答