Skip to content

Commit

Permalink
fix bug which removed menus from website
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesknelson committed Jan 15, 2017
1 parent 9628af0 commit 8eb40e7
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 15 deletions.
180 changes: 180 additions & 0 deletions site/loaders/img-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var htmlMinifier = require("html-minifier");
var loaderUtils = require("loader-utils");
var url = require("url");
var compile = require("es6-templates").compile;
var Parser = require("fastparse");

var processMatch = function(match, strUntilValue, name, value, index) {
if(!this.isRelevantTagAttr(this.currentTag, name)) return;
this.results.push({
start: index + strUntilValue.length,
length: value.length,
value: value
});
};

var parser = new Parser({
outside: {
"<!--.*?-->": true,
"<![CDATA[.*?]]>": true,
"<[!\\?].*?>": true,
"<\/[^>]+>": true,
"<([a-zA-Z\\-:]+)\\s*": function(match, tagName) {
this.currentTag = tagName;
return "inside";
}
},
inside: {
"\\s+": true, // eat up whitespace
">": "outside", // end of attributes
"(([0-9a-zA-Z\\-:]+)\\s*=\\s*\")([^\"]*)\"": processMatch,
"(([0-9a-zA-Z\\-:]+)\\s*=\\s*\')([^\']*)\'": processMatch,
"(([0-9a-zA-Z\\-:]+)\\s*=\\s*)([^\\s>]+)": processMatch
}
});

var attrParse = function parse(html, isRelevantTagAttr) {
return parser.parse("outside", html, {
currentTag: null,
results: [],
isRelevantTagAttr: isRelevantTagAttr
}).results;
};


function randomIdent() {
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
}

function getLoaderConfig(context) {
var query = loaderUtils.parseQuery(context.query);
var configKey = query.config || 'htmlLoader';
var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};

delete query.config;

return Object.assign(query, config);
}

module.exports = function(content) {
this.cacheable && this.cacheable();

this.value = this.inputValue;

var config = getLoaderConfig(this);
var attributes = ["img:src"];
if(config.attrs !== undefined) {
if(typeof config.attrs === "string")
attributes = config.attrs.split(" ");
else if(Array.isArray(config.attrs))
attributes = config.attrs;
else if(config.attrs === false)
attributes = [];
else
throw new Error("Invalid value to config parameter attrs");
}
var root = config.root;
var links = attrParse(content, function(tag, attr) {
return attributes.indexOf(tag + ":" + attr) >= 0;
});
links.reverse();
var data = {};
content = [content];
links.forEach(function(link) {
if(!loaderUtils.isUrlRequest(link.value, root)) return;

var uri = url.parse(link.value);
if (uri.hash !== null && uri.hash !== undefined) {
uri.hash = null;
link.value = uri.format();
link.length = link.value.length;
}

do {
var ident = randomIdent();
} while(data[ident]);
data[ident] = link.value;
var x = content.pop();
content.push(x.substr(link.start + link.length));
content.push(ident);
content.push(x.substr(0, link.start));
});
content.reverse();
content = content.join("");

if (config.interpolate === 'require'){

var reg = /\$\{require\([^)]*\)\}/g;
var result;
var reqList = [];
while(result = reg.exec(content)){
reqList.push({
length : result[0].length,
start : result.index,
value : result[0]
})
}
reqList.reverse();
content = [content];
reqList.forEach(function(link) {
var x = content.pop();
do {
var ident = randomIdent();
} while(data[ident]);
data[ident] = link.value.substring(11,link.length - 3)
content.push(x.substr(link.start + link.length));
content.push(ident);
content.push(x.substr(0, link.start));
});
content.reverse();
content = content.join("");
}

if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
var minimizeOptions = Object.assign({}, config);

[
"removeComments",
"removeCommentsFromCDATA",
"removeCDATASectionsFromCDATA",
"collapseWhitespace",
"conservativeCollapse",
"removeAttributeQuotes",
"useShortDoctype",
"keepClosingSlash",
"minifyJS",
"minifyCSS",
"removeScriptTypeAttributes",
"removeStyleTypeAttributes",
].forEach(function(name) {
if(typeof minimizeOptions[name] === "undefined") {
minimizeOptions[name] = true;
}
});

content = htmlMinifier.minify(content, minimizeOptions);
}

if(config.interpolate && config.interpolate !== 'require') {
content = compile('`' + content + '`').code;
} else {
content = JSON.stringify(content);
}

var exportsString = "module.exports = ";
if (config.exportAsDefault) {
exportsString = "exports.default = ";

} else if (config.exportAsEs6Default) {
exportsString = "export default ";
}

return exportsString + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
if(!data[match]) return match;
return '" + require(' + JSON.stringify(loaderUtils.urlToRequest(data[match], root)) + ') + "';
}) + ";";
}
7 changes: 6 additions & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"file-loader": "^0.9.0",
"front-matter": "^2.1.1",
"history": "^4.5.0",
"html-loader": "^0.4.4",
"html-webpack-plugin": "^2.22.0",
"less": "^2.7.2",
"less-loader": "^2.2.3",
Expand All @@ -27,5 +26,11 @@
"url-loader": "^0.5.7",
"webpack": "1.13.1",
"webpack-dev-server": "1.14.1"
},
"dependencies": {
"es6-templates": "^0.2.3",
"fastparse": "^1.1.1",
"html-minifier": "^3.2.3",
"loader-utils": "^0.2.16"
}
}
2 changes: 1 addition & 1 deletion site/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = {
loader: 'style!css!less'
},
{ test: /\.md$/,
loader: 'sitepack?preload!html!markdown'
loader: 'sitepack?preload!img!markdown'
},
{ test: /\.(gif|jpe?g|png|ico)$/,
loader: 'url?limit=4000'
Expand Down
16 changes: 3 additions & 13 deletions site/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ es6-promise@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.0.5.tgz#7882f30adde5b240ccfa7f7d78c548330951ae42"

es6-templates@^0.2.2:
es6-templates@^0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/es6-templates/-/es6-templates-0.2.3.tgz#5cb9ac9fb1ded6eb1239342b81d792bbb4078ee4"
dependencies:
Expand Down Expand Up @@ -1104,17 +1104,7 @@ html-comment-regex@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/html-comment-regex/-/html-comment-regex-1.1.1.tgz#668b93776eaae55ebde8f3ad464b307a4963625e"

html-loader@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/html-loader/-/html-loader-0.4.4.tgz#f2b5b9acd5e035ff3ab5fd369c13c97a7bb014da"
dependencies:
es6-templates "^0.2.2"
fastparse "^1.1.1"
html-minifier "^3.0.1"
loader-utils "^0.2.15"
object-assign "^4.1.0"

html-minifier@^3.0.1, html-minifier@^3.2.3:
html-minifier@^3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/html-minifier/-/html-minifier-3.2.3.tgz#d2ff536e24d95726c332493d8f77d84dbed85372"
dependencies:
Expand Down Expand Up @@ -1496,7 +1486,7 @@ loader-utils@0.2.14:
json5 "^0.5.0"
object-assign "^4.0.1"

loader-utils@0.2.x, loader-utils@^0.2.11, loader-utils@^0.2.15, loader-utils@^0.2.16, loader-utils@^0.2.5, loader-utils@^0.2.7, loader-utils@~0.2.2, loader-utils@~0.2.5:
loader-utils@0.2.x, loader-utils@^0.2.11, loader-utils@^0.2.16, loader-utils@^0.2.5, loader-utils@^0.2.7, loader-utils@~0.2.2, loader-utils@~0.2.5:
version "0.2.16"
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-0.2.16.tgz#f08632066ed8282835dff88dfb52704765adee6d"
dependencies:
Expand Down

0 comments on commit 8eb40e7

Please sign in to comment.