Skip to content

Commit

Permalink
impletement supply an option for user to switch for the inline MathJa…
Browse files Browse the repository at this point in the history
…x expression #283
  • Loading branch information
rhiokim committed Feb 9, 2014
1 parent e79c9a8 commit d7c6373
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 21 deletions.
80 changes: 80 additions & 0 deletions src/js/app/core/InlineLexer.js
@@ -0,0 +1,80 @@
define([
],
function() {
var marked = require("marked");
var options = store.get('Markdown') || {};

var InlineLexer = marked.InlineLexer;

var customRules = {
stronghighlight: /^==([^=]+)==/,
underline: /^_([^_]+)_/
};

var inlineMathRegEx = /^ *(split)([\s\S]*?[^\$])\1(?!\$)/;
if (options.dollarSign) {
customRules.math = replace(inlineMathRegEx)
('split', /\${3,3}|\${1,1}/)
();
}


function replace(regex, opt) {
regex = regex.source;
opt = opt || '';
return function self(name, val) {
if (!name) return new RegExp(regex, opt);
val = val.source || val;
val = val.replace(/(^|[^\[])\^/g, '$1');
regex = regex.replace(name, val);
return self;
};
}

function merge(obj) {
var i = 1
, target
, key;

for (; i < arguments.length; i++) {
target = arguments[i];
for (key in target) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
obj[key] = target[key];
}
}
}

return obj;
}

InlineLexer.rules.normal = merge({}, InlineLexer.rules.normal, customRules);
InlineLexer.rules.gfm = merge({}, InlineLexer.rules.gfm, customRules);
InlineLexer.rules.breaks = merge({}, InlineLexer.rules.breaks, customRules);

window.ee.on('preferences.markdown.change', function(options) {
var rules = InlineLexer.rules;

if (options.dollarSign) {
customRules.math = replace(inlineMathRegEx)
('split', /\${3,3}|\${1,1}/)
();
} else {
customRules.math = replace(inlineMathRegEx)
('split', /\${3,3}/)
();
}

if (options.gfm) {
InlineLexer.rules.gfm = merge({}, rules.gfm, customRules);
}

if (options.breaks) {
InlineLexer.rules.breaks = merge({}, rules.breaks, customRules);
}

window.ee.emit('preferences.markdown.change.after');
});

return InlineLexer;
});
18 changes: 13 additions & 5 deletions src/js/app/core/Lexer.js
Expand Up @@ -12,18 +12,14 @@ define([
var lexer = new marked.Lexer(defaults);

var customRules = {
math: /^ *(\${2,2}) *([\s\S]+?)\s*\1 *(?:\n+|$)/,
oembed: /^@\[(inside)\]\(href\)/,
toc: /^\[\s*(TOC|toc)(?:\s+['"]([\s\S]*?)['"])?\s*\] *(?:\n+|$)/
}

var _inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
var _href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;

customRules.oembed = replace(customRules.oembed)
('inside', _inside)
('href', _href)
();

function replace(regex, opt) {
regex = regex.source;
opt = opt || '';
Expand Down Expand Up @@ -53,7 +49,19 @@ define([
return obj;
}

customRules.oembed = replace(customRules.oembed)
('inside', _inside)
('href', _href)
();

lexer.rules.paragraph = replace(lexer.rules.paragraph)
('math', customRules.math)
();

lexer.rules = merge({}, lexer.rules, customRules);
lexer.rules.pedantic = merge({}, lexer.rules.pedantic, customRules);
lexer.rules.gfm = merge({}, lexer.rules.gfm, customRules);
lexer.rules.tables = merge({}, lexer.rules.tables, customRules);

window.ee.on('preferences.markdown.change', function(options) {
var blocks = marked.Lexer.rules;
Expand Down
6 changes: 4 additions & 2 deletions src/js/app/core/Parser.js
@@ -1,12 +1,14 @@
define([
'core/Lexer'
'core/Lexer',
'core/InlineLexer'
],
function(Lexer) {
function(Lexer, InlineLexer) {

var gui = require('nw.gui'),
win = gui.Window.get();

var marked = require("marked");
marked.InlineLexer = InlineLexer;

var parse = function(src) {
var tokens = Lexer.lex(src);
Expand Down
8 changes: 5 additions & 3 deletions src/js/app/core/Renderer.js
Expand Up @@ -40,14 +40,16 @@ define([
}

renderer.math = function(text, block) {
if (block) {
if (block === '$$') {
return '<p class="mathjax">$$'
+ text
+ '$$</p>';
} else {
return '<span class="mathjax">$$$'
return '<span class="mathjax">'
+ block
+ text
+ '$$$</span>';
+ block
+ '</span>';
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/app/math/Math.js
Expand Up @@ -18,9 +18,9 @@ define([], function() {

loadJs(global.PATHS.js + '/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML', function() {
MathJax.Hub.Config({
showProcessingMessages: true,
showProcessingMessages: false,
tex2jax: {
inlineMath: [ ['$$$','$$$'], ["\\(","\\)"] ],
inlineMath: [ ['$$$','$$$'], ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true
}
Expand Down
1 change: 1 addition & 0 deletions src/js/preferences/tabs/Backup.js
Expand Up @@ -62,6 +62,7 @@ define([
delete config.Window;
delete config.Helper;
delete config.Markdown.gfm;
delete config.Markdown.emoji;
delete config.Markdown.highlight;
delete config.Markdown.langPrefix;
delete config.Markdown.headerPrefix;
Expand Down
9 changes: 9 additions & 0 deletions src/js/preferences/tabs/Markdown.js
Expand Up @@ -30,6 +30,7 @@ define([
'click input[name=breaks]': 'enableBreaks',
'click input[name=smartLists]': 'enableSmartLists',
'click input[name=smartypants]': 'enableSmartyPants',
'click input[name=dollarSign]': 'enableDollarSign',
'click input[name=mathjax]': 'enableMathjax',
'click button[name=apply]': 'clickApply',
'click button[name=default]': 'clickDefault'
Expand All @@ -51,6 +52,7 @@ define([
this.$('input[name=smartLists]').prop('checked', opt.smartLists);
this.$('input[name=smartypants]').prop('checked', opt.smartypants);
this.$('input[name=mathjax]').prop('checked', opt.mathjax);
this.$('input[name=dollarSign]').prop('checked', opt.dollarSign);
},

reset: function() {
Expand Down Expand Up @@ -121,6 +123,13 @@ define([
options.set('mathjax', bool);

_gaq.push('haroopad.preferences', 'markdown option', 'mathjax:'+bool);
},

enableDollarSign: function(e) {
var bool = $(e.target).is(':checked');
options.set('dollarSign', bool);

_gaq.push('haroopad.preferences', 'markdown option', 'use single dollar sign:'+bool);
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/js/preferences/tabs/Markdown.opt.js
Expand Up @@ -14,7 +14,8 @@ define([
highlight: null,
langPrefix: '',
headerPrefix: '',
mathjax: false
mathjax: false,
dollarSign: false
},

initialize: function() {
Expand Down
18 changes: 10 additions & 8 deletions src/node_modules/marked/lib/marked.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/preferences.html
Expand Up @@ -480,6 +480,17 @@ <h5 data-i18n="markdown.etc">ETC</h5>
<input type="checkbox" value="" name="mathjax">
<span data-i18n="markdown.enable-math">Use Mathematics Expression</span> [<a href="http://mathjax.org">?</a>]
</label>


<h5 data-i18n="markdown.math">Math Expression options</h5>
<label class="checkbox">
<input type="checkbox" value="" name="mathjax">
<span data-i18n="markdown.enable-math">Use Mathematics Expression</span> [<a href="http://mathjax.org">?</a>]
</label>
<label class="checkbox">
<input type="checkbox" value="" name="dollarSign">
<span data-i18n="markdown.enable-dollar-sign">Use $ Sign for Inline Expression</span>
</label>
</p>
</div>

Expand Down

0 comments on commit d7c6373

Please sign in to comment.