Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ghost + LaTex #24

Open
rainyear opened this issue Feb 26, 2016 · 1 comment
Open

Ghost + LaTex #24

rainyear opened this issue Feb 26, 2016 · 1 comment
Assignees
Labels

Comments

@rainyear
Copy link
Owner

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.

@rainyear rainyear added the Note label Feb 26, 2016
@rainyear
Copy link
Owner Author

rainyear commented Mar 4, 2016

Update:

This change could cause:

export PATH=$PATH:$JAVA_HOME

rendered as:

export PATH=$PATH:$JAVA\_HOME

So we should change code block back, add another filter:

{
        type: 'lang',
        filter: function(text) {
                return text.replace(/```([\s\S]*?\n)```/gim, function(match, g){
                        return "```" + g.replace(/\\_/g, '_') + "```";
                })
        }
}

@rainyear rainyear self-assigned this Sep 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant