Skip to content

Instantly share code, notes, and snippets.

@pkra
pkra / cdnjsredirect.js
Last active April 12, 2017 14:51
cdnjs redirect example
(function () {
var newMathJax = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js';
var oldMathJax = 'https://path.to/MathJax.j';
var n = oldMathJax.length;
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
var src = script.getAttribute('src') || '';
if (src.substr(0, n) === oldMathJax) {
@pkra
pkra / deep_labels.html
Last active March 30, 2017 09:36
Deep Labels, cf. codepen.io/pkra/pen/xRBZjq
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<link rel="shortcut icon" type="image/x-icon" href="https://production-assets.codepen.io/assets/favicon/favicon-8ea04875e70c4b0bb41da869e81236e54394d63638a1ef12fa558a4a835f1164.ico" />
<link rel="mask-icon" type="" href="https://production-assets.codepen.io/assets/favicon/logo-pin-f2d2b6d2c61838f7e76325261b7195c27224080bc099486ddd6dccb469b8e8e6.svg" color="#111" />
<title>CodePen - Deep labels</title>
@pkra
pkra / phantomJS-PDF-mathjax.js
Created May 25, 2015 07:46
PhantomJS MathJax PDF example
// via https://github.com/mathjax/MathJax/issues/1029#issuecomment-72348069 and http://c.utz.cc/mathjax/render.js
var webpage = require('webpage');
var capture = function (page, pageUrl, callback) {
page.open(pageUrl, function (status) {
var interval, allDone;
if (status !== 'success') {
callback(new Error('Error rendering page'));
@pkra
pkra / wikipedia2mj.js
Last active March 7, 2017 02:10
Bookmarklet for Wikipedia, replacing PNGs on the fly
javascript:(function(){ var s=document.createElement('script');s.setAttribute('src','ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"');document.getElementsByTagName('body')[0].appendChild(s); $('img.tex').wrap('<span class="MathJax_Preview" />');$('.MathJax_Preview').after(function() { tex = $(this).find('img').attr("alt"); return " <script type='math/tex'>" + tex + "</" + "script>" ;});$.getScript('https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML');})()
@pkra
pkra / child.js
Created February 20, 2017 08:14
mathjax-node experiment with forking child processes
const mathjax = require('mathjax-node');
const typeset = mathjax.typeset;
process.on('message', function(m) {
if (m === 'exit') {
// empty typeset to push exit on MathJax's queue (else we'd terminate too soon)
typeset({"math": "", "format": "TeX", "mml":true}, function(result) {
process.send('exit');
})
} else {
@pkra
pkra / wp2mj.js
Last active February 10, 2017 16:07
A snippet for wordpress's math PNGs
(function () {
var images = document.querySelectorAll('img.latex');
for (var i = 0; i < images.length; i++){
var image = images[i];
tex = image.getAttribute('alt');
var preview = document.createElement('span');
preview.setAttribute('class','MathJax_Preview');
image.parentNode.insertBefore(preview, image);
preview.appendChild(image);
var script = document.createElement('script');
@pkra
pkra / img-to-mj.js
Created August 1, 2016 11:31
Use TeX in alt-text of images for rendering with MathJax
(function () {
var images = document.getElementsByTagName('img');
var hasMath = false;
for (var i = 0; i < images.length; i++){
var image = images[i];
tex = image.getAttribute('alt');
if (tex && tex[0] === '$'){ // TODO a class for TeX images would be cleaner, also to differentiate inline and display math
hasMath = true;
var preview = document.createElement('span');
preview.setAttribute('class','MathJax_Preview');
@pkra
pkra / index.html
Last active July 19, 2016 12:39
mathjax pre-processing on node
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MathJax even more basic pen</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
@pkra
pkra / page-jsdom-mml.js
Created June 24, 2016 11:14
Example for dropping mj-page from mathjax-node: processing mathml in HTML.
"use strict";
var mjAPI = require("../lib/main.js");
mjAPI.start();
var jsdom = require('jsdom').jsdom;
var serializeDocument = require("jsdom").serializeDocument;
var input = "<!DOCTYPE html><p> When <math> <mi>a</mi><mo>&#x2260;</mo><mn>0</mn> </math>, there are two solutions to <math> <mi>a</mi><msup><mi>x</mi><mn>2</mn></msup> <mo>+</mo> <mi>b</mi><mi>x</mi> <mo>+</mo> <mi>c</mi> <mo>=</mo> <mn>0</mn> </math></p>";
var document = jsdom(input);
var window = document.defaultView;
@pkra
pkra / mj-a11y-grunt.js
Last active May 30, 2016 11:20
grunt file for mathjax-a11y
module.exports = function(grunt) {
grunt.initConfig({
'json-minify': {
build: {
files: 'dist/mathmaps/**/*.json'
}
},
gitclone: {
'speech-rule-engine': {