Skip to content

Commit

Permalink
Add basic support for mathjax tea3#42
Browse files Browse the repository at this point in the history
  • Loading branch information
VicaYang committed Jul 17, 2018
1 parent 51da393 commit 19d7391
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
64 changes: 64 additions & 0 deletions lib/filter/formula.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

'use strict';

var Promise = require('bluebird');
var assign = require('object-assign');

//------------------------------------
// formula to <amp-mathml>
//------------------------------------
module.exports.filter_formula = function(result){
if(result.tempData.isCacheUse)return Promise.resolve(result);
return new Promise(function(resolve , reject){

var updateObj;
var replaceStr = result.data;
var config = result.config;
var isFormulaContain = result.tempData.isFormulaContain;

// protect $ in code block
replaceStr = replaceStr.replace(/<pre>.+?<\/pre>/g, function($1) {
return arguments[0].replace(/\$/g, 'PROTECTED_DOLLAR')
});
// protect $ in case like title="$a$"
replaceStr = replaceStr.replace(/=\".+?\"/g, function($1) {
return arguments[0].replace(/\$/g, 'PROTECTED_DOLLAR')
});

// replace formula
replaceStr = replaceStr.replace(/\$\$(.+?)\$\$/g, function ($1,$2){
isFormulaContain = true;
return '<amp-mathml layout="container" data-formula="\\[' + arguments[1] +'\\]"></amp-mathml>';
});
replaceStr = replaceStr.replace(/\$(.+?)\$/g, function ($1,$2){
isFormulaContain = true;
return '<amp-mathml layout="container" inline data-formula="\\[' + arguments[1] +'\\]"></amp-mathml>';
});

//recover the dollar in code block
replaceStr = replaceStr.replace(/PROTECTED_DOLLAR/g, '$')

if(isFormulaContain){
updateObj = assign(
result ,
{
data : replaceStr
} ,
{
tempData : assign(
result.tempData ,
{
isFormulaContain : isFormulaContain
}
)
}
);
} else {
updateObj = result;
}

resolve( updateObj );

// process.stdout.write('[hexo-generator-amp] Plugin is currently replacing Formula ... \r');
});
};
1 change: 1 addition & 0 deletions lib/filter/rendering-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports.rendering_html = function(result){
isSoundCloudContain: result.tempData.isSoundCloudContain ,
isIframeContain : result.tempData.isIframeContain ,
isElseAdContain : result.tempData.isElseAdContain ,
isFormulaContain : result.tempData.isFormulaContain,
avatarPath : result.tempData.avatarPath_template ,
logoPath : result.tempData.logoPath_template ,
logoPath_width : result.tempData.logoPath_template_width ,
Expand Down
5 changes: 4 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var af_mn = require("./filter/minify-html.js");
var af_ch = require("./filter/write-cache.js");
var af_av = require("./filter/amp-validate.js");
var af_sc = require("./filter/soundcloud.js");
var af_fm = require("./filter/formula.js")



Expand Down Expand Up @@ -56,7 +57,8 @@ module.exports = function(locals , hexo ) {
isTwitterContain : false ,
isSoundCloudContain : false ,
isIframeContain : false ,
isElseAdContain : false
isElseAdContain : false ,
isFormulaContain : false
};

return {
Expand Down Expand Up @@ -111,6 +113,7 @@ function postProcess(result){
.then(af_oi.filter_otherIframe)
.then(af_vd.filter_video)
.then(af_sn.filter_sanitize)
.then(af_fm.filter_formula)
.then(af_rd.rendering_html)
.then(af_mn.html_minify)
.then(af_ch.write_cache)
Expand Down
6 changes: 6 additions & 0 deletions template/sample-amp.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@
<% } %>
<% if (isIframeContain){ %>
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
<% } %>
<% if (isFormulaContain){ %>
<script async custom-element="amp-mathml" src="https://cdn.ampproject.org/v0/amp-mathml-0.1.js"></script>
<% } %>
<% if (config.generator_amp.substituteGoogle_adsense || isElseAdContain){ %>
<script async custom-element="amp-ad" src="https://cdn.ampproject.org/v0/amp-ad-0.1.js"></script>
<% } %>
<% if (config.generator_amp.google_analytics){ %>
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<% } %>
<% if (config.generator_amp.mathjax){ %>
<script async custom-element="amp-mathml" src="https://cdn.ampproject.org/v0/amp-mathml-0.1.js"></script>
<% } %>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script type="application/ld+json">
{
Expand Down

0 comments on commit 19d7391

Please sign in to comment.