Skip to content

Commit f0c8c99

Browse files
Merge pull request #189 from Martii/addSupportURLsupport
Add support for only one last `@supportURL`, fix naming standardization, and such
2 parents 739e503 + 1719f88 commit f0c8c99

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

controllers/script.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var fs = require('fs');
22
var formidable = require('formidable');
33
var async = require('async');
44
var _ = require('underscore');
5+
var sanitizeHtml = require('sanitize-html');
6+
var htmlWhitelistLink = require('../libs/htmlWhitelistLink.json');
57

68
var Discussion = require('../models/discussion').Discussion;
79
var Group = require('../models/group').Group;
@@ -53,6 +55,9 @@ var getScriptPageTasks = function (options) {
5355
var script = options.script;
5456
var authedUser = options.authedUser;
5557

58+
// Temporaries
59+
var htmlStub = null;
60+
5661
//--- Tasks
5762

5863
// Show the number of open issues
@@ -64,23 +69,23 @@ var getScriptPageTasks = function (options) {
6469
if (script.meta.author && script.meta.collaborator) {
6570
options.hasCollab = true;
6671
if (typeof script.meta.collaborator === 'string') {
67-
options.script.meta.collaborators = [{ name: script.meta.collaborator }];
72+
options.script.collaborators = [{ url: encodeURIComponent(script.meta.collaborator), text: script.meta.collaborator }];
6873
} else {
69-
options.script.meta.collaborators = [];
74+
options.script.collaborators = [];
7075
script.meta.collaborator.forEach(function (collaborator) {
71-
options.script.meta.collaborators.push({ name: collaborator });
76+
options.script.collaborators.push({ url: encodeURIComponent(collaborator), text: collaborator });
7277
});
7378
}
7479
}
7580

7681
// Show licensings of the script
7782
if (script.meta.license) {
7883
if (typeof script.meta.license === 'string') {
79-
options.script.meta.licenses = [{ name: script.meta.license }];
84+
options.script.licenses = [{ name: script.meta.license }];
8085
} else {
81-
options.script.meta.licenses = [];
86+
options.script.licenses = [];
8287
script.meta.license.forEach(function (license) {
83-
options.script.meta.licenses.push({ name: license });
88+
options.script.licenses.push({ name: license });
8489
});
8590
}
8691
} else if (!script.isLib) {
@@ -90,11 +95,25 @@ var getScriptPageTasks = function (options) {
9095
// Show homepages of the script
9196
if (script.meta.homepageURL) {
9297
if (typeof script.meta.homepageURL === 'string') {
93-
options.script.meta.homepages = [{ name: script.meta.homepageURL }];
98+
htmlStub = '<a href="' + script.meta.homepageURL + '"></a>';
99+
if (htmlStub === sanitizeHtml(htmlStub, htmlWhitelistLink)) {
100+
options.script.homepages = [{
101+
url: script.meta.homepageURL,
102+
text: decodeURI(script.meta.homepageURL),
103+
hasNoFollow: !/^(?:https?:\/\/)?openuserjs\.org\//i.test(script.meta.homepageURL)
104+
}];
105+
}
94106
} else {
95-
options.script.meta.homepages = [];
107+
options.script.homepages = [];
96108
script.meta.homepageURL.forEach(function (homepage) {
97-
options.script.meta.homepages.push({ name: homepage });
109+
htmlStub = '<a href="' + homepage + '"></a>';
110+
if (htmlStub === sanitizeHtml(htmlStub, htmlWhitelistLink)) {
111+
options.script.homepages.push({
112+
url: homepage,
113+
text: decodeURI(homepage),
114+
hasNoFollow: !/^(?:https?:\/\/)?openuserjs\.org/i.test(homepage)
115+
});
116+
}
98117
});
99118
}
100119
}

libs/htmlWhitelistLink.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"allowedTags": [
3+
"a"
4+
],
5+
"allowedAttributes": {
6+
"a": [
7+
"href"
8+
]
9+
},
10+
"selfClosing": [
11+
],
12+
"allowedSchemes": [
13+
"http",
14+
"https",
15+
"mailto"
16+
]
17+
}

libs/modelParser.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var moment = require('moment');
22
var _ = require('underscore');
33
var util = require('util');
4+
var sanitizeHtml = require('sanitize-html');
5+
var htmlWhitelistLink = require('./htmlWhitelistLink.json');
46

57
var Script = require('../models/script').Script;
68

@@ -83,6 +85,9 @@ var parseScript = function (scriptData) {
8385
if (!scriptData) return;
8486
var script = scriptData.toObject ? scriptData.toObject() : scriptData;
8587

88+
// Temporaries
89+
var htmlStub = null;
90+
8691
// Author
8792
if (_.isString(script.author)) {
8893
script.author = parseUser({ name: script.author });
@@ -102,6 +107,29 @@ var parseScript = function (scriptData) {
102107
script.icon45Url = script.meta.icon64;
103108
}
104109

110+
// Support Url
111+
if (script.meta.supportURL) {
112+
if (_.isString(script.meta.supportURL)) {
113+
htmlStub = '<a href="' + script.meta.supportURL + '"></a>';
114+
if (htmlStub === sanitizeHtml(htmlStub, htmlWhitelistLink)) {
115+
script.support = [{
116+
url: script.meta.supportURL,
117+
text: decodeURI(script.meta.supportURL),
118+
hasNoFollow: !/^(?:https?:\/\/)?openuserjs\.org/i.test(script.meta.supportURL)
119+
}];
120+
}
121+
} else if (_.isArray(script.meta.supportURL) && !_.isEmpty(script.meta.supportURL)) {
122+
htmlStub = '<a href="' + script.meta.supportURL[script.meta.supportURL.length - 1] + '"></a>';
123+
if (htmlStub === sanitizeHtml(htmlStub, htmlWhitelistLink)) {
124+
script.support = [{
125+
url: script.meta.supportURL[script.meta.supportURL.length - 1],
126+
text: decodeURI(script.meta.supportURL[script.meta.supportURL.length - 1]),
127+
hasNoFollow: !/^(?:https?:\/\/)?openuserjs\.org/i.test(script.meta.supportURL[script.meta.supportURL.length - 1])
128+
}];
129+
}
130+
}
131+
}
132+
105133
//
106134
script.fullName = script.author.name + '/' + script.name; // GitHub-like name
107135

views/pages/scriptPage.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
</ul>
3636
</span>
3737
{{/script.hasGroups}}
38-
{{#script.meta.homepages}}<p><i class="fa fa-fw fa-home"></i> <b>Homepage:</b> <a href="{{name}}">{{name}}</a></p>{{/script.meta.homepages}}
38+
{{#script.homepages}}<p><i class="fa fa-fw fa-home"></i> <b>Homepage:</b> <a href="{{{url}}}"{{#hasNoFollow}} rel="nofollow"{{/hasNoFollow}}>{{text}}</a></p>{{/script.homepages}}
39+
{{#script.support}}<p><i class="fa fa-fw fa-support"></i> <b>Support:</b> <a href="{{{url}}}"{{#hasNoFollow}} rel="nofollow"{{/hasNoFollow}}>{{text}}</a></p>{{/script.support}}
3940
{{#script.meta.copyright}}<p><i class="fa fa-fw fa-legal"></i> <b>Copyright:</b> {{script.meta.copyright}}</p>{{/script.meta.copyright}}
40-
{{#script.meta.licenses}}<p><i class="fa fa-fw fa-legal"></i> <b>License:</b> {{name}}</p>{{/script.meta.licenses}}
41+
{{#script.licenses}}<p><i class="fa fa-fw fa-legal"></i> <b>License:</b> {{name}}</p>{{/script.licenses}}
4142
{{#hasCollab}}
42-
<p><i class="fa fa-fw fa-user"></i> <b>Collaborator:</b> {{#script.meta.collaborators}} <span class="label label-info"><a href="/users/{{name}}">{{name}}</a></span> {{/script.meta.collaborators}}</p>
43+
<p><i class="fa fa-fw fa-user"></i> <b>Collaborator:</b> {{#script.collaborators}} <span class="label label-info"><a href="/users/{{{url}}}">{{text}}</a></span> {{/script.collaborators}}</p>
4344
{{/hasCollab}}
4445
{{#script.fork}}
4546
<p><b>Fork History:</b></p>

0 commit comments

Comments
 (0)