Skip to content

TeX Math rendering #26

Closed
Closed
@bordaigorl

Description

@bordaigorl

I am proposing to add TeX math rendering via Google Charts or the like.

The only change needed is in marked.tex that needs to be patched as follows

--- marked.js   2012-08-06 16:26:22.155518000 +0100
+++ marked-tex.js   2012-08-07 17:59:24.654090000 +0100
@@ -306,3 +306,3 @@
 var inline = {
-  escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
+  escape: /^\\([\\`*{}\[\]()#+\-.!_>\$])/,
   autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
@@ -312,2 +312,3 @@
   reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
+  math: /^\$([^ \t\n\$][^\$]*[^ \t\n\$])\$/,
   nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
@@ -317,3 +318,3 @@
   br: /^ {2,}\n(?!\s*$)/,
-  text: /^[^\0]+?(?=[\\<!\[_*`]| {2,}\n|$)/
+  text: /^[^\0]+?(?=[\$\\<!\[_*`]| {2,}\n|$)/
 };
@@ -341,3 +342,4 @@
   strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/,
-  em: /^_(?=\S)([^\0]*?\S)_(?!_)|^\*(?=\S)([^\0]*?\S)\*(?!\*)/
+  em: /^_(?=\S)([^\0]*?\S)_(?!_)|^\*(?=\S)([^\0]*?\S)\*(?!\*)/,
+  math: noop
 };
@@ -346,3 +348,3 @@
   url: /^(https?:\/\/[^\s]+[^.,:;"')\]\s])/,
-  text: /^[^\0]+?(?=[\\<!\[_*`]|https?:\/\/| {2,}\n|$)/
+  text: /^[^\0]+?(?=[\$\\<!\[_*`]|https?:\/\/| {2,}\n|$)/
 };
@@ -369,2 +371,9 @@

+    // math
+    if (cap = inline.math.exec(src)) {
+      src = src.substring(cap[0].length);
+      out += '<img alt="'+cap[1]+'" src="https://chart.googleapis.com/chart?cht=tx&chl='+encodeURIComponent(cap[1])+'">';
+      continue;
+    }
+
     // autolink

or along the same lines.

I am aware of one big downside of this approach: the image gets rendered on the fly by google chart so your TeX formula flies around the net every time you (or the receiver) view it.
This conflicts with the general idea of Mardown-here rendering the content completely in-browser.

A fully fledged solution could present an option to disable this feature if "no info leak" is a strong requirement.

The added functionality is in my opinion a huge gain when working on scientific projects with collaborators.
A warning that this is not rendered in browser would in my opinion be enough.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions