From 19d7391309fa5b501bca8e3c41237214f4536f87 Mon Sep 17 00:00:00 2001 From: VicaYang Date: Tue, 17 Jul 2018 14:15:21 +0800 Subject: [PATCH] Add basic support for mathjax #42 --- lib/filter/formula.js | 64 ++++++++++++++++++++++++++++++++++++ lib/filter/rendering-html.js | 1 + lib/generator.js | 5 ++- template/sample-amp.ejs | 6 ++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 lib/filter/formula.js diff --git a/lib/filter/formula.js b/lib/filter/formula.js new file mode 100644 index 0000000..e4bed88 --- /dev/null +++ b/lib/filter/formula.js @@ -0,0 +1,64 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + +//------------------------------------ +// formula to +//------------------------------------ +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>/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 '';
+    });
+    replaceStr = replaceStr.replace(/\$(.+?)\$/g, function ($1,$2){
+      isFormulaContain = true;
+      return '';
+    });
+    
+    //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');
+  });
+};
\ No newline at end of file
diff --git a/lib/filter/rendering-html.js b/lib/filter/rendering-html.js
index 1ec6bb2..2a40580 100644
--- a/lib/filter/rendering-html.js
+++ b/lib/filter/rendering-html.js
@@ -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 ,
diff --git a/lib/generator.js b/lib/generator.js
index 7bd1353..f94a2f1 100644
--- a/lib/generator.js
+++ b/lib/generator.js
@@ -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")
 
 
 
@@ -56,7 +57,8 @@ module.exports = function(locals , hexo ) {
         isTwitterContain        : false ,
         isSoundCloudContain     : false ,
         isIframeContain         : false ,
-        isElseAdContain         : false
+        isElseAdContain         : false ,
+        isFormulaContain        : false
       };
       
       return {
@@ -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)
diff --git a/template/sample-amp.ejs b/template/sample-amp.ejs
index 11f467e..7b7685c 100644
--- a/template/sample-amp.ejs
+++ b/template/sample-amp.ejs
@@ -63,6 +63,9 @@
     <% } %>
     <% if (isIframeContain){ %>
     
+	<% } %>
+    <% if (isFormulaContain){ %>
+      
     <% } %>
     <% if (config.generator_amp.substituteGoogle_adsense || isElseAdContain){ %>
     
@@ -70,6 +73,9 @@
     <% if (config.generator_amp.google_analytics){ %>
     
     <% } %>
+    <% if (config.generator_amp.mathjax){ %>
+      
+    <% } %>