Skip to content

Ghost + LaTex #24

@rainyear

Description

@rainyear

Ghost v0.7.0

LaTex - MathJax v2.6.1

1. Code Injection

Add following codes to {{ghost_foot}} tag:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX", "output/SVG"],
  tex2jax: {inlineMath: [['$$', '$$']],displayMath : [['$$$$','$$$$']]},
});
</script>
<script src="//cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-AMS_HTML"></script>

2. Escape a_i, b_i

$a_i, b_i$ like LaTex equations might be parsed into $a<em>i, b</em>i. We need a custom extension of Showdown to escape this transition:

cd $BLOG/core/server/models

# create new file
vim ghostLatex.js
(function () {
    var ghostLatex = function(){
        return [
            {
                type    : 'lang',
                filter : function (text) {
                    return text.replace(/([^\\\\])~D([^$]+[^\\])~D/g,  function(match, g1, g2) {
                        res = g1 + '~D' + g2.replace(/\\([^\d\w\s])/g, function (match, g) {
                            if (g == '\\')
                                g = '\\\\'
                            return '\\\\' + g;
                        }).replace(/_/g, '\\_').replace(/\*/g, '\\*') + '~D';
                        return res;
                    });
                }
            }
        ]
    };
    // Server-side export
    if (typeof module !== 'undefined') {
        module.exports = ghostLatex;
    }
}());

Edit $BLOG/core/server/models/post.js:

/* Add to the top */
var ghostLatex = require('./ghostLatex');
/* 
Update 
    converter      = new Showdown.converter({extensions: ['ghostgfm', 'footnotes', 'highlight']}),
to
*/
converter      = new Showdown.converter({extensions: [ghostLatex, 'ghostgfm', 'footnotes', 'highlight']}),

Only new posts will be affected after Ghost server is restarted, if you want to update all your published posts, you might need to edit and update them.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions