From 0e9f9be4a6008bbc55a9fbbb00d20fec73eb8515 Mon Sep 17 00:00:00 2001 From: twpl Date: Mon, 6 Feb 2017 18:48:51 +0900 Subject: [PATCH] generator.js Replaced by Promise() . --- lib/filter/amp-validate.js | 71 ++++ lib/filter/check-cache.js | 28 ++ lib/filter/figure-escape.js | 52 +++ lib/filter/figure-restoration.js | 45 +++ lib/filter/google-adsense.js | 55 +++ lib/filter/img.js | 109 ++++++ lib/filter/instagram.js | 57 +++ lib/filter/lazy-youtube-vimeo.js | 69 ++++ lib/filter/minify-html.js | 74 ++++ lib/filter/other-iframe.js | 47 +++ lib/filter/rendering-html.js | 55 +++ lib/filter/sanitize.js | 75 ++++ lib/filter/table-align.js | 34 ++ lib/filter/twitter.js | 62 ++++ lib/filter/video.js | 80 +++++ lib/filter/vimeo.js | 68 ++++ lib/filter/write-cashe.js | 20 ++ lib/filter/youtube.js | 69 ++++ lib/generator.js | 585 +++++-------------------------- lib/log.js | 2 + lib/util.js | 16 + package.json | 6 +- src/img/ampvalid.png | Bin 19965 -> 18240 bytes src/img/hexo2amp.png | Bin 4503 -> 14669 bytes template/sample-amp.ejs | 3 + 25 files changed, 1188 insertions(+), 494 deletions(-) create mode 100644 lib/filter/amp-validate.js create mode 100644 lib/filter/check-cache.js create mode 100644 lib/filter/figure-escape.js create mode 100644 lib/filter/figure-restoration.js create mode 100644 lib/filter/google-adsense.js create mode 100644 lib/filter/img.js create mode 100644 lib/filter/instagram.js create mode 100644 lib/filter/lazy-youtube-vimeo.js create mode 100644 lib/filter/minify-html.js create mode 100644 lib/filter/other-iframe.js create mode 100644 lib/filter/rendering-html.js create mode 100644 lib/filter/sanitize.js create mode 100644 lib/filter/table-align.js create mode 100644 lib/filter/twitter.js create mode 100644 lib/filter/video.js create mode 100644 lib/filter/vimeo.js create mode 100644 lib/filter/write-cashe.js create mode 100644 lib/filter/youtube.js create mode 100644 lib/util.js diff --git a/lib/filter/amp-validate.js b/lib/filter/amp-validate.js new file mode 100644 index 0000000..56200b8 --- /dev/null +++ b/lib/filter/amp-validate.js @@ -0,0 +1,71 @@ + +'use strict'; + +var Promise = require('bluebird'); +var pathFn = require('path'); +var assign = require('object-assign'); +var amphtmlValidator = require('amphtml-validator').getInstance(); +var lg = require('../log.js'); +var util = require('../util.js'); +var validPassCnt = 0; +var validErrorCnt = 0; + +//------------------------------------ +// AMP HTML Validate +//------------------------------------ +module.exports.amp_validate = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + + lg.setConfig(result.config); + + return new Promise(function(resolve , reject){ + + if(result.config.generator_amp.warningLog){ + amphtmlValidator.then(function (validator) { + var validate_result = validator.validateString(result.data); + if( validate_result.status === 'PASS'){ + validPassCnt++; + process.stdout.write('[hexo-generator-amp] Plugin is currently AMP validating now ... '+validPassCnt+' pages newly PASSED \u001b[32m✓\u001b[0m . \r'); + }else{ + validErrorCnt++; + if(validErrorCnt == 1){ + //(validate_result.errors.length <= 1 ? "an error." : validate_result.errors.length + " errors.") + // result.post.path + "amp/#development=1 (source: "+ result.post.source + lg.log("amp error", "\u001b[7;49;31mThe AMP Validator found error.\u001b[0m" + "\u001b[31m Please check the template files ( ./"+ result.config.generator_amp.templateDir +"/ ) , and the post's file.\u001b[0m \n"); + + if(validate_result.errors.length > 0){ + console.log("\u001b[31m" + util.spacePadding(validate_result.errors.length, 3) + " AMP Validation Error" + (validate_result.errors.length == 1 ? "" : "s") + "\u001b[31m : " + result.post.path + "amp/#development=1 (source: "+ result.post.source +")\u001b[0m"); + console.log(" \n \u001b[31mThe following is error message about AMP validation. \n ----------------------------------------"); + } + for (var ii = 0; ii < validate_result.errors.length; ii++) { + var error = validate_result.errors[ii]; + var msg = ''; + msg += ' line ' + error.line + ', col ' + error.col + ': ' + error.message; + if (error.specUrl !== null && error.specUrl != "") { + msg += ' (see ' + error.specUrl + ')'; + } + ((error.severity === 'ERROR') ? console.error : console.warn)(msg); + } + if(validate_result.errors.length > 0) + console.log(" ----------------------------------------\u001b[0m\n"); + }else{ + if(validErrorCnt == 2){ + lg.log("amp error", "\u001b[7;49;31mThe AMP Validator found error.\u001b[0m \u001b[31mFor the other pages , check the error message displayed in Chrome Devtools . \u001b[0m\n"); + } + console.log("\u001b[31m" + util.spacePadding(validate_result.errors.length, 3) + " AMP Validation Error" + (validate_result.errors.length == 1 ? " " : "s") + "\u001b[31m : " + result.post.path + "amp/#development=1 (source: "+ result.post.source +")\u001b[0m"); + } + + + + } + resolve( result ); + + }); + }else{ + + resolve( result ); + + } + + }); +}; \ No newline at end of file diff --git a/lib/filter/check-cache.js b/lib/filter/check-cache.js new file mode 100644 index 0000000..57408f6 --- /dev/null +++ b/lib/filter/check-cache.js @@ -0,0 +1,28 @@ + +'use strict'; + +var Promise = require('bluebird'); +var pathFn = require('path'); +var assign = require('object-assign'); +var cache = require('../cache.js'); + +//------------------------------------ +// check the Cache file +//------------------------------------ +module.exports.checkCache = function(result){ + return new Promise(function(resolve , reject){ + var cachedData = cache.getCache( result.post , result.config ); + process.stdout.write('[hexo-generator-amp] Plugin is currently checking the cache now ... \r'); + if(cachedData){ + resolve(assign( result , { + path : pathFn.join( result.post.path , "amp/index.html" ), + data : cachedData.xml, + tempData: assign( result.tempData , { isCacheUse : true } ) + })); + }else{ + resolve(assign( result , { + path : pathFn.join( result.post.path , "amp/index.html" ) + })); + } + }); +}; diff --git a/lib/filter/figure-escape.js b/lib/filter/figure-escape.js new file mode 100644 index 0000000..e6ccc76 --- /dev/null +++ b/lib/filter/figure-escape.js @@ -0,0 +1,52 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + +//------------------------------------ +// figure escape +//------------------------------------ +module.exports.filter_figureEscape = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + // figure tag + var updateObj; + var replaceStr = result.post.content; + var figureEscArr = []; + var figureMatch = replaceStr.match(/\/g); + if(figureMatch){ + for(var i = 0; i < figureMatch.length; i++){ + replaceStr = replaceStr.replace(figureMatch[i],""); + figureEscArr.push(figureMatch[i]); + } + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } , + { + tempData : assign( + result.tempData , + { + figureEscArr : figureEscArr + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently escaping
now ... \r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; \ No newline at end of file diff --git a/lib/filter/figure-restoration.js b/lib/filter/figure-restoration.js new file mode 100644 index 0000000..00296a7 --- /dev/null +++ b/lib/filter/figure-restoration.js @@ -0,0 +1,45 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + + +//------------------------------------ +// figure restoration +//------------------------------------ +module.exports.filter_figureRestoration = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var updateObj; + var replaceStr = result.post.content; + var figureEscArr = result.tempData.figureEscArr; + var figureMatch = replaceStr.match(/\<\!\-\-figure[0-9]+\-\-\>/g); + if(figureMatch){ + for(var i=0; i now ... \r'); + + }else{ + updateObj = result; + } + + resolve(result); + }); +}; diff --git a/lib/filter/google-adsense.js b/lib/filter/google-adsense.js new file mode 100644 index 0000000..b91614f --- /dev/null +++ b/lib/filter/google-adsense.js @@ -0,0 +1,55 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + +//------------------------------------ +// google Adsense to +//------------------------------------ +module.exports.filter_googleAdsense = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var replaceStr = result.post.content; + var config = result.config; + + replaceStr = replaceStr.replace(/\<[^\s\>]*ins[^\S\>]*class="adsbygoogle"[^\S\>]*style="([^"\\]*(?:\\.[^"\\]*)*)"[^\>]*data-ad-client="([^"\\]*(?:\\.[^"\\]*)*)"[^\>]*data-ad-slot="([^"\\]*(?:\\.[^"\\]*)*)"[^\<]*\<\/ins\>/g, function ($1, $2, $3){ + + var adWidth; + var adHeight; + if(arguments[1]){ + var matches = /width:([0-9]*)/g.exec(arguments[1]); + adWidth = matches ? matches[1] : config.generator_amp.substituteGoogle_adsense.width; + + matches = /height:([0-9]*)/g.exec(arguments[1]); + adHeight = matches ? matches[1] : config.generator_amp.substituteGoogle_adsense.height; + }else{ + adWidth = config.generator_amp.substituteGoogle_adsense.width; + adHeight = config.generator_amp.substituteGoogle_adsense.height; + } + + var adClient = arguments[2] ? arguments[2] : config.generator_amp.substituteGoogle_adsense.data_ad_client; + var adSlot = arguments[3] ? arguments[3] : config.generator_amp.substituteGoogle_adsense.data_ad_slot; + + return ""; + }); + + var updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing Google Ad ... \r'); + + resolve(result); + + }); +}; \ No newline at end of file diff --git a/lib/filter/img.js b/lib/filter/img.js new file mode 100644 index 0000000..38c5759 --- /dev/null +++ b/lib/filter/img.js @@ -0,0 +1,109 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); +var gs = require('../imageSize.js'); +var lg = require('../log.js'); + +//------------------------------------ +// to +//------------------------------------ +module.exports.filter_img = function(result){ + + if(result.tempData.isCacheUse)return Promise.resolve(result); + + lg.setConfig(result.config); + gs.setConfig(result.config); + + return new Promise(function(resolve , reject){ + + var updateObj; + var absolutePathReg = /^[a-zA-Z0-9]*?\:\/\//; + var replaceStr = result.post.content; + var post = result.post; + var imgMatch = replaceStr.match(/\/g); + var imgSrc; + var imgWidth; + var imgHeight; + + if(imgMatch){ + for(var i=0; i 0){ + imgSrc = imgDataMatch[0].replace("data-original=","src="); + }else{ + imgDataMatch = imgMatch[i].match(/src\=\".*?\"/g); + if(imgDataMatch){ + imgSrc = imgDataMatch[0]; + } + } + + imgDataMatch = imgMatch[i].match(/width\=\".*?\"/g); + if(imgDataMatch){ + imgWidth = imgDataMatch[0]; + } + + imgDataMatch = imgMatch[i].match(/(height|data\-height)\=\".*?\"/g); + if(imgDataMatch){ + imgHeight = imgDataMatch[0].replace("data-height","height"); + } + + if(imgSrc == "" || imgSrc =='src=""'){ + lg.log("error", " should contain image src attribute." , post.source); + }else{ + if(imgWidth == "" || imgHeight == ""){ + var imgPath = imgSrc.replace(/^src\=\"/,"").replace(/\"$/,""); + if(absolutePathReg.test(imgPath)){ + //External image file + lg.log("warn", " should contain image file and width height attribute. img path: "+imgPath , post.source); + }else{ + //Local image files + var gsSizeInfo = gs.getSizeInfo(imgPath , post); + if(gsSizeInfo){ + imgWidth = 'width="' + gsSizeInfo.w + '"'; + imgHeight = 'height="' + gsSizeInfo.h + '"'; + }else{ + reject(); + } + + } + } + } + + // console.log("imgSrc:"+imgSrc + " imgWidth:"+imgWidth + " imgHeight:"+imgHeight); + if(imgSrc != "" && imgWidth != "" && imgHeight != ""){ + replaceStr = replaceStr.replace(imgMatch[i], '

'); + }else{ + //coment out img + replaceStr = replaceStr.replace(imgMatch[i], ""); + } + + } + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing now ... \r'); + + }else{ + updateObj = result; + } + + resolve(result); + }); +}; \ No newline at end of file diff --git a/lib/filter/instagram.js b/lib/filter/instagram.js new file mode 100644 index 0000000..2e393e8 --- /dev/null +++ b/lib/filter/instagram.js @@ -0,0 +1,57 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + + +//------------------------------------ +// instagram to (1080 x 1080) +//------------------------------------ +module.exports.filter_instagram = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var updateObj; + var replaceStr = result.post.content; + var isInstagramContain = result.tempData.isInstagramContain; + + var igMatch = replaceStr.match(/\/g); + if(igMatch){ + for(var i=0; i= 2){ + replaceStr = replaceStr.replace(igMatch[i],''); + isInstagramContain = true; + } + } + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } , + { + tempData : assign( + result.tempData , + { + isInstagramContain : isInstagramContain + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing instagram now ... \r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; \ No newline at end of file diff --git a/lib/filter/lazy-youtube-vimeo.js b/lib/filter/lazy-youtube-vimeo.js new file mode 100644 index 0000000..a00012d --- /dev/null +++ b/lib/filter/lazy-youtube-vimeo.js @@ -0,0 +1,69 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); +var cheerio = require('cheerio'); + +//------------------------------------ +// lazy load for youtube & vimeo +//------------------------------------ +// This replacement corresponding to a special case. +// +// (e.g.)
Lazy load for Youtube
+// (e.g.)
Lazy load for Vimeo
+module.exports.filter_lazyYoutubeAndVimeo = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var updateObj; + var replaceStr = result.post.content; + var isVimeoContain = result.tempData.isVimeoContain; + var isYoutubeContain = result.tempData.isYoutubeContain; + var $ = cheerio.load(replaceStr); + + $(".lazy-video").each(function(i){ + if($(this).attr("data-ampvideoid") && $(this).attr("data-ampvideotype")){ + if($(this).attr("data-ampvideotype") == "youtube"){ + $(this).replaceWith(''); + isYoutubeContain = true; + }else if($(this).attr("data-ampvideotype") == "vimeo"){ + $(this).replaceWith(''); + isVimeoContain = true; + } + } + }); + + if(isVimeoContain || isYoutubeContain){ + replaceStr = $.html(); + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } , + { + tempData : assign( + result.tempData , + { + isVimeoContain : isVimeoContain, + isYoutubeContain : isYoutubeContain + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing lazy video now ... \r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; diff --git a/lib/filter/minify-html.js b/lib/filter/minify-html.js new file mode 100644 index 0000000..62599c4 --- /dev/null +++ b/lib/filter/minify-html.js @@ -0,0 +1,74 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); +var minify = require('html-minifier').minify; +var minifyCnt = 0; + +//------------------------------------ +// html minify (option) +//------------------------------------ +module.exports.html_minify = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var updateObj; + var htmlData = result.data; + + if(typeof result.config.generator_amp.html_minifier !== "undefined"){ + var minified_option = { + "caseSensitive" : false, + "collapseBooleanAttributes" : true , + "collapseInlineTagWhitespace" : false, + "collapseWhitespace" : true , + "conservativeCollapse" : true , + "decodeEntities" : true, + "html5" : true, + "includeAutoGeneratedTags" : false, + "keepClosingSlash" : false, + "minifyCSS" : true, + "minifyJS" : true, + "preserveLineBreaks" : false, + "preventAttributesEscaping" : false, + "processConditionalComments" : true, + "processScripts" : ["text/html" ,"application/ld+json" ,"application/json"], + "removeAttributeQuotes" : false, + "removeComments" : true, + "removeEmptyAttributes" : true, + "removeEmptyElements" : false, + "removeOptionalTags" : true, + "removeRedundantAttributes" : true, + "removeScriptTypeAttributes" : true, + "removeStyleLinkTypeAttributes": true, + "removeTagWhitespace" : false, + "sortAttributes" : true, + "sortClassName" : true, + "useShortDoctype" : true + }; + + // orverride html_minifier option + if( result.config.generator_amp.html_minifier && typeof result.config.generator_amp.html_minifier === "object"){ + minified_option = assign( {}, minified_option, result.config.generator_amp.html_minifier); + } + var minified_HTML = minify( htmlData, minified_option); + htmlData = minified_HTML; + + minifyCnt++; + + updateObj = assign( + result , + { + data : htmlData + } + ); + + process.stdout.write('[hexo-generator-amp] HTML Minifying now ... '+minifyCnt+' pages newly minified .\r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; diff --git a/lib/filter/other-iframe.js b/lib/filter/other-iframe.js new file mode 100644 index 0000000..d2baf72 --- /dev/null +++ b/lib/filter/other-iframe.js @@ -0,0 +1,47 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + + +//------------------------------------ +// other iframe +//------------------------------------ +module.exports.filter_otherIframe = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var updateObj; + var replaceStr = result.post.content; + var isElseMatch = replaceStr.match(/\\<\/iframe\>/g); + if(isElseMatch){ + for(var i=0; i= 2){ + replaceStr = replaceStr.replace(isElseMatch[i],'' + iframeSrc[1] + ''); + } + + } + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing iframe now ... \r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; diff --git a/lib/filter/rendering-html.js b/lib/filter/rendering-html.js new file mode 100644 index 0000000..bf8711c --- /dev/null +++ b/lib/filter/rendering-html.js @@ -0,0 +1,55 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); +var moment = require('moment'); + +//------------------------------------ +// rendering HTML +//------------------------------------ +module.exports.rendering_html = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + // get year + var yy = moment().format("YYYY"); + + //datePublished - Formatting using formats in Hexo config + var dateFormat = "YYYY-MM-DD"; + if(result.config.date_format) { + dateFormat = result.config.date_format; + } + var pdStr = moment( Number(result.post.date) ).format(dateFormat); + + var htmlData = result.tempData.template({ + config : result.config , + post : result.post , + content : result.post.content , + cssTxt : result.tempData.cssTxt , + copyrightDate : yy , + datePublished : pdStr , + isYoutubeContain : result.tempData.isYoutubeContain , + isVimeoContain : result.tempData.isVimeoContain , + isVideoContain : result.tempData.isVideoContain , + isInstagramContain: result.tempData.isInstagramContain , + isTwitterContain : result.tempData.isTwitterContain , + avatarPath : result.tempData.avatarPath_template , + logoPath : result.tempData.logoPath_template , + logoPath_width : result.tempData.logoPath_template_width , + logoPath_height : result.tempData.logoPath_template_height , + logoPathForAmp : result.tempData.logoPath_for_amp + }); + + var updateObj = assign( + result , + { + data : htmlData + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently rendering html now ... \r'); + + resolve( updateObj ); + }); +}; diff --git a/lib/filter/sanitize.js b/lib/filter/sanitize.js new file mode 100644 index 0000000..11fefcd --- /dev/null +++ b/lib/filter/sanitize.js @@ -0,0 +1,75 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + +//------------------------------------ +// Sanitize tag +//------------------------------------ +module.exports.filter_sanitize = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var replaceStr = result.post.content; + var cssTxt = result.tempData.cssTxt; + var idDecorator= result.tempData.idDecorator; + + replaceStr = replaceStr.replace(/\<[\s]*style[^\<]*\<\/[\s]*style[\s]*\>/g, ""); + // replaceStr = replaceStr.replace(/\<[\s]*script[^\<]*\<\/[\s]*script[\s]*\>/g, ""); + replaceStr = replaceStr.replace(/\<[\s]*script[\s\S]*?\<\/[\s]*script[\s]*\>/g, ""); + + // Remove style from tags (adding ids and promoting to css) + replaceStr = replaceStr.replace(/\<\s*([^\s\>]+)((?:[^\>]*\bid[ \t]*="([^"\\]*(?:\\.[^"\\]*)*)"|[^\>]*)[^\>]*)style="([^"\\]*(?:\\.[^"\\]*)*)"((?:[^\>]*\bid[ \t]*="([^"\\]*(?:\\.[^"\\]*)*)"|[^\>]*)[^\>]*\>)/g, function ($1, $2, $3, $4, $5, $6){ + //$1 -> type + //$2 -> content between type and style (aka id, class, etc.) + //$3 -> id if on left side of style OR empty + //$4 -> content of style (you can add this to id OR create a new id for this tag to add to) + //$5 -> content to end the tag (aka id, class, etc.) + //$6 -> id if on right side of style OR empty + var id = (arguments[3] === "") ? arguments[6] : arguments[3]; + var newID = null; + var idStyle = arguments[4].replace(/\!important/g, ""); + if(id && (typeof id != 'undefined') && id != "") + { + //console.log("Found an id"); + cssTxt += "#" + id + "{" + idStyle + "}"; + }else{ + //console.log("Generate a unique qualified id for the amp generator"); + newID = "hexo-amp-id-" + (++idDecorator); + cssTxt += "#" + newID + "{" + idStyle + "}"; + } + + //console.log("Style: " + arguments[4]); + return "<" + arguments[1] + (newID ? " id=\"" + newID + '"' : "") + " " + arguments[2] + " " + arguments[5]; + }); + + //delete a tags that contain "javascript:"" + replaceStr = replaceStr.replace(/\]*\>/g, ""); + + var updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } , + { + tempData : assign( + result.tempData , + { + cssTxt : cssTxt , + idDecorator: idDecorator + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently sanitizing tags now ... \r'); + + resolve( updateObj ); + }); +}; diff --git a/lib/filter/table-align.js b/lib/filter/table-align.js new file mode 100644 index 0000000..e0beb54 --- /dev/null +++ b/lib/filter/table-align.js @@ -0,0 +1,34 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); + +//------------------------------------ +// table's text align +//------------------------------------ +module.exports.filter_tablesAlign = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + return new Promise(function(resolve , reject){ + + var replaceStr = result.post.content; + replaceStr = replaceStr.replace(/\'); + isTwitterContain = true; + } + }); + + if(isTwitterContain){ + replaceStr = $.html(); + + updateObj = assign( + result , + { + post : assign( + result.post , + { + content : replaceStr + } + ) + } , + { + tempData : assign( + result.tempData , + { + isTwitterContain : isTwitterContain + } + ) + } + ); + + process.stdout.write('[hexo-generator-amp] Plugin is currently replacing twitter now ... \r'); + + }else{ + updateObj = result; + } + + resolve( updateObj ); + }); +}; \ No newline at end of file diff --git a/lib/filter/video.js b/lib/filter/video.js new file mode 100644 index 0000000..51fce9f --- /dev/null +++ b/lib/filter/video.js @@ -0,0 +1,80 @@ + +'use strict'; + +var Promise = require('bluebird'); +var assign = require('object-assign'); +var cheerio = require('cheerio'); +var lg = require('../log.js'); + +//------------------------------------ +// video to +//------------------------------------ +module.exports.filter_video = function(result){ + if(result.tempData.isCacheUse)return Promise.resolve(result); + + lg.setConfig(result.config); + + return new Promise(function(resolve , reject){ + + var updateObj; + var replaceStr = result.post.content; + var $ = cheerio.load(replaceStr); + var isVideoContain = false; + + $("video").each(function(i){ + + // I check required attributes in . + if( !$(this).attr( "width" ) || !$(this).attr("height") || !$(this).attr("poster") ){ + lg.log("warn", "