Skip to content

Commit 66a8a34

Browse files
Jesus David García Gomezmolant
Jesus David García Gomez
authored andcommitted
Fix: Add helpers directly to the site to allow cache
Ref #434
1 parent 71ff4c7 commit 66a8a34

File tree

18 files changed

+323
-233
lines changed

18 files changed

+323
-233
lines changed

gulpfile.js

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ gulp.task('move:static', () => {
7979
.pipe(gulp.dest(`${dirs.tmp}/source/static`));
8080
});
8181

82+
gulp.task('move:helpers', () => {
83+
return gulp.src(`${dirs.tmp}/source/js/helpers/*.js`)
84+
.pipe(gulp.dest(`${dirs.tmp}/helper`));
85+
});
86+
8287
gulp.task('optimize:js', () => {
8388
return gulp.src(`${dirs.tmp}/source/**/*.js`)
8489
.pipe(plugins.uglify())
@@ -297,6 +302,7 @@ gulp.task('build', gulp.series(
297302
'optimize:js',
298303
'optimize:css',
299304
'move:static',
305+
'move:helpers',
300306
'clean:after',
301307
'revfiles',
302308
'revreplace:content',

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979
"gulp": "gulp",
8080
"hexo:clean": "hexo clean",
8181
"lint": "npm-run-all lint:*",
82-
"lint:browser": "eslint -c .eslintrc --env browser src/sonarwhal-theme/source/**/*.js",
82+
"lint:browser": "eslint src/sonarwhal-theme/source/**/*.js",
8383
"lint:js": "npm run lint:node && npm run lint:browser",
8484
"lint:md": "markdownlint README.md ./src/hexo/source",
85-
"lint:node": "eslint -c .eslintrc --env node **/*.js --ignore-pattern src/sonarwhal-theme/source/**/*.js --ignore-pattern src/sonarwhal-theme-optimized/**/*.js --ignore-pattern dist/**/*.js",
85+
"lint:node": "eslint **/*.js --ignore-pattern src/sonarwhal-theme/source/**/*.js --ignore-pattern src/sonarwhal-theme-optimized/**/*.js --ignore-pattern dist/**/*.js",
8686
"lint:styles": "stylelint src/hexo/themes/sonarwhal/source/**/*.css --config .stylelintrc",
8787
"start": "npm run build -- watch",
8888
"watch": "gulp watch",

src/server/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ const createServer = () => {
3838
const themeDir = path.join(hexoDir, theme);
3939
const layoutsDir = path.join(themeDir, 'layout');
4040
const partialsDir = path.join(layoutsDir, 'partials');
41+
42+
/* Helpers path */
4143
const helpersPath = path.join(themeDir, 'helper/index.js');
42-
const themeHelpers = require(helpersPath)();
43-
const customHelpers = {
44-
url_for: (url) => { // eslint-disable-line camelcase
45-
return url;
46-
}
47-
};
48-
const helpers = Object.assign(handlebars.helpers, themeHelpers, customHelpers); // combine helpers
44+
45+
/* Required Helpers */
46+
const miscHelpers = require(helpersPath)();
47+
48+
const helpers = Object.assign(handlebars.helpers, miscHelpers); // Combine helpers
4949
const app = express();
5050

5151
app.disable('x-powered-by');

src/sonarwhal-theme/helper/index.js

+15-190
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
/* eslint-disable object-shorthand, prefer-template */
2-
const Handlebars = require('handlebars');
1+
const globby = require('globby');
2+
const path = require('path');
3+
34
const pagination = require('./pagination');
5+
6+
const basePath = path.join(__dirname, '..');
7+
const files = globby.sync(['helper/*.js', '**/helpers/*.js', '!helper/index.js', '!helper/pagination.js'], { cwd: basePath }); // eslint-disable-line no-sync
8+
9+
const helpers = files.reduce((result, file) => {
10+
return Object.assign(result, require(path.join(basePath, file)));
11+
}, {});
12+
413
const url = require('url');
514

615
const jobStatus = {
@@ -30,10 +39,6 @@ module.exports = function () {
3039
return isIndexPage(page) && page.title.toLowerCase().replace(' ', '-') !== page.category;
3140
};
3241

33-
const normalizeString = (str) => {
34-
return str.toLowerCase().replace(/[^a-z0-9]/gi, '-');
35-
};
36-
3742
const sortPageByAlpha = (l, r) => {
3843
if (l.title > r.title) {
3944
return 1;
@@ -75,138 +80,11 @@ module.exports = function () {
7580
};
7681

7782
const self = {
78-
/* eslint-disable object-shorthand */
79-
capitalize: function (str) {
83+
capitalize: (str) => {
8084
const filtered = str.replace(/[^a-zA-Z0-9]/g, ' ');
8185

8286
return filtered.charAt(0).toUpperCase() + filtered.slice(1);
8387
},
84-
/* eslint-enable object-shorthand */
85-
/**
86-
* Handlebars Comparison Helpers
87-
* Copyright (c) 2013 Jon Schlinkert, Brian Woodward, contributors
88-
* Licensed under the MIT License (MIT).
89-
* https://github.com/helpers/handlebars-helpers/blob/a3683bab5519882927de527077c34a98ac22067b/lib/comparison.js#L48
90-
* Modified to fit sonarwhal Website
91-
*/
92-
/**
93-
* {{#compare}}...{{/compare}}
94-
*
95-
* @credit: OOCSS
96-
* @param left value
97-
* @param operator The operator, must be between quotes ">", "=", "<=", etc...
98-
* @param right value
99-
* @param options option object sent by handlebars
100-
* @return {String} formatted html
101-
*
102-
* @example:
103-
* {{#compare unicorns "<" ponies}}
104-
* I knew it, unicorns are just low-quality ponies!
105-
* {{/compare}}
106-
*
107-
* {{#compare value ">=" 10}}
108-
* The value is greater or equal than 10
109-
* {{else}}
110-
* The value is lower than 10
111-
* {{/compare}}
112-
*/
113-
compare: function (left, operator, right, options) { // eslint-disable-line object-shorthand
114-
if (arguments.length < 3) {
115-
throw new Error('Handlebars Helper "compare" needs 2 parameters');
116-
}
117-
118-
/* eslint-disable no-param-reassign */
119-
if (!options) {
120-
options = right;
121-
right = operator;
122-
operator = '===';
123-
}
124-
/* eslint-enable no-param-reassign */
125-
126-
const operators = {
127-
'!=': function (l, r) {
128-
return l !== r;
129-
},
130-
'!==': function (l, r) {
131-
return l !== r;
132-
},
133-
'<': function (l, r) {
134-
return l < r;
135-
},
136-
'<=': function (l, r) {
137-
return l <= r;
138-
},
139-
'==': function (l, r) {
140-
return l === r;
141-
},
142-
'===': function (l, r) {
143-
if (typeof l === 'string' && typeof r === 'string') {
144-
/* eslint-disable no-param-reassign */
145-
l = normalizeString(l);
146-
r = normalizeString(r);
147-
/* eslint-enable no-param-reassign */
148-
}
149-
150-
return l === r;
151-
},
152-
'>': function (l, r) {
153-
return l > r;
154-
},
155-
'>=': function (l, r) {
156-
return l >= r;
157-
},
158-
includes: function (collection, member) {
159-
const normalizedR = member ? normalizeString(member) : member;
160-
const normalizedL = collection.split(/, */g).map(function (element) { //eslint-disable-line prefer-arrow-callback
161-
return normalizeString(element);
162-
});
163-
164-
return normalizedL.indexOf(normalizedR) !== -1;
165-
},
166-
typeof: function (l, r) {
167-
return typeof l === r;
168-
},
169-
'||': function (l, r) {
170-
return l || r;
171-
}
172-
};
173-
174-
if (!operators[operator]) {
175-
throw new Error('Handlebars Helper "compare" doesn\'t know the operator ' + operator);
176-
}
177-
178-
const result = operators[operator](left, right);
179-
180-
if (result) {
181-
return options.fn(this);
182-
}
183-
184-
return options.inverse(this);
185-
},
186-
cutCodeString: function (codeString) {
187-
return self.shortenString(codeString, 150);
188-
},
189-
cutString: function (string, maxLength) {
190-
const minLength = 0.8 * maxLength;
191-
const preferredStopChars = /[^a-zA-Z0-9]/g;
192-
let chunk;
193-
194-
for (let i = minLength; i < maxLength; i++) {
195-
// Start looking for preferred stop characters.
196-
if (preferredStopChars.test(string[i])) {
197-
chunk = string.slice(0, i);
198-
199-
break;
200-
}
201-
}
202-
203-
chunk = chunk || string.slice(0, maxLength);
204-
205-
return chunk;
206-
},
207-
cutUrlString: function (urlString) {
208-
return self.shortenString(urlString, 25);
209-
},
21088
filterErrorsAndWarnings: (results) => {
21189
if (!results) {
21290
return results;
@@ -231,16 +109,10 @@ module.exports = function () {
231109
if (match) {
232110
const ruleName = match.pop();
233111

234-
return 'packages/' + ruleName + '/README.md';
112+
return `packages/${ruleName}/README.md`;
235113
}
236114

237-
return 'packages/sonarwhal/' + originalFile;
238-
},
239-
getLength: function (messages, unit) {
240-
const length = messages.length;
241-
const units = self.pluralize(unit, length);
242-
243-
return length + ' ' + units;
115+
return `packages/sonarwhal/${originalFile}`;
244116
},
245117
getPagesByToCTitle: (title, pages) => {
246118
return pages[title].filter((page) => {
@@ -318,27 +190,6 @@ module.exports = function () {
318190
isPending: (status) => {
319191
return status === jobStatus.pending;
320192
},
321-
linkify: function (msg) {
322-
const regex = /(https?:\/\/[a-zA-Z0-9.\\/?:@\-_=#]+\.[a-zA-Z0-9&.\\/?:@-_=#]*)\s[a-zA-Z]/g;
323-
// Modified use of regular expression in https://stackoverflow.com/a/39220764
324-
// Should match:
325-
// [email protected] has 2 known vulnerabilities (1 medium, 1 low). See https://snyk.io/vuln/npm:jquery for more information.
326-
// [email protected] has 3 known vulnerabilities (3 high). See https://snyk.io/vuln/npm:angular for more information.
327-
// Shouldn't match (shortened url):
328-
// File https://www.odysys.com/ … hedule-Your-Demo-Now.png could be around 37.73kB (78%) smaller.
329-
const match = regex.exec(msg);
330-
const escapedMsg = Handlebars.Utils.escapeExpression(msg);
331-
332-
if (!match) {
333-
return escapedMsg;
334-
}
335-
336-
const urlMatch = match.pop();
337-
const escapedUrlMatch = Handlebars.Utils.escapeExpression(urlMatch);
338-
const newMsg = escapedMsg.replace(escapedUrlMatch, '<a href="' + urlMatch + '">' + escapedUrlMatch + '</a>');
339-
340-
return new Handlebars.SafeString(newMsg);
341-
},
342193
noIssue: (category) => {
343194
return category.rules.every((rule) => {
344195
return rule.status === ruleStatus.pass;
@@ -355,13 +206,6 @@ module.exports = function () {
355206
return className.toLowerCase().trim()
356207
.replace(/[^a-z0-9]/gi, '-');
357208
},
358-
normalizePosition: function (position) {
359-
if (!position || parseInt(position) === -1) {
360-
return '';
361-
}
362-
363-
return ':' + position;
364-
},
365209
or: (l, r) => {
366210
return l || r;
367211
},
@@ -375,13 +219,6 @@ module.exports = function () {
375219
passWarnings: (statistics) => {
376220
return statistics && statistics.warnings === 0;
377221
},
378-
pluralize: function (text, count) {
379-
return text + (count === 1 ? '' : 's');
380-
},
381-
reverseString: function (str) {
382-
return str.split('').reverse()
383-
.join('');
384-
},
385222
sanitize: (permalink) => {
386223
return permalink.replace(/\/index.html/g, '/');
387224
},
@@ -390,18 +227,6 @@ module.exports = function () {
390227
return accumulator || value;
391228
});
392229
},
393-
// Solution inspired by https://stackoverflow.com/a/10903003
394-
shortenString: function (string, maxLength) {
395-
if (!string || string.length < maxLength * 2) {
396-
return string;
397-
}
398-
399-
const headChunk = self.cutString(string, maxLength);
400-
const reverseTailChunk = self.cutString(self.reverseString(string), maxLength);
401-
const tailChunk = self.reverseString(reverseTailChunk);
402-
403-
return headChunk + ' … ' + tailChunk;
404-
},
405230
showMdContent: (page) => {
406231
// If the markdown Content should be used.
407232
const guildeIndexes = ['contributor guide', 'user guide'];
@@ -444,5 +269,5 @@ module.exports = function () {
444269
}
445270
};
446271

447-
return self;
272+
return Object.assign(self, helpers);
448273
};

src/sonarwhal-theme/layout/partials/helpers.hbs

-9
This file was deleted.

src/sonarwhal-theme/layout/scan.hbs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@
3131
<script src="/js/highlight/highlight.min.js"></script>
3232
<script src="/js/highlight/languages/xml.min.js"></script>
3333
<!-- endbuild -->
34-
<script src="/scanner/helpers/compare,cutCodeString,cutString,cutUrlString,getLength,normalizePosition,pluralize,reverseString,shortenString,linkify"></script>
34+
<!-- build:js /static/scripts/helpers.js -->
35+
<script src="/js/helpers/compare.js"></script>
36+
<script src="/js/helpers/cutstring.js"></script>
37+
<script src="/js/helpers/getlength.js"></script>
38+
<script src="/js/helpers/linkify.js"></script>
39+
<script src="/js/helpers/normalizeposition.js"></script>
40+
<script src="/js/helpers/compare.js"></script>
41+
<!-- endbuild -->
3542
<!-- build:js /static/scripts/scanner.js -->
3643
<script src="/js/scanner-common.js"></script>
3744
<script src="/js/scanner-submit.js"></script>

src/sonarwhal-theme/scripts/link-filter.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ hexo.extend.filter.register('before_post_render', (data) => {
1717
* For packages with multiple rules, we should have
1818
* links to each rules in the README.md
1919
* Those links will be transform:
20-
*
20+
*
2121
* ./docs/rule-name.md => ./rule-name
2222
*/
2323
data.content = data.content.replace(docLinkRegex, (link, docsString, extensionString) => {

src/sonarwhal-theme/source/.eslintrc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"node": true,
5+
"amd": false,
6+
"mocha": false,
7+
"jasmine": false,
8+
"es6": false
9+
},
10+
"parserOptions": {
11+
"ecmaVersion": 8,
12+
"impliedStrict": true,
13+
"sourceType": "module"
14+
},
15+
"rules": {
16+
"no-var": "off",
17+
"object-shorthand": "off",
18+
"prefer-arrow-callback": "off",
19+
"prefer-template": "off"
20+
}
21+
}

src/sonarwhal-theme/source/components/accordion/accordion.js

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
/* global setImmediate */
2929
(function () {
30-
'use strict';
31-
3230
var supportDetails = 'open' in document.createElement('details');
3331

3432
var animateOpen = function (parent, content) {

src/sonarwhal-theme/source/js/gdpr.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-env browser */
2-
/* eslint-disable no-var, prefer-template, strict, prefer-arrow-callback, object-shorthand, no-continue */
31
(function () {
42
/** Creates a cookie. Code based on https://www.quirksmode.org/js/cookies.html */
53
var createCookie = function (name, value, days) {

0 commit comments

Comments
 (0)