Skip to content

Commit 4b7ec19

Browse files
committed
Refactor document.js and update canvas README line refs
Improved code formatting and readability in utils/document.js, including consistent spacing, function formatting, and directory creation safety. Updated line number references in wiki/docs/renderers/canvas/README.md to reflect changes in the source file.
1 parent 01d8f97 commit 4b7ec19

2 files changed

Lines changed: 63 additions & 74 deletions

File tree

utils/document.js

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ var path = require('path');
44
var compiler = require('jsdoc-api');
55
var sourceFiles = require('./source-files');
66

7-
var pkg = JSON.parse(fs.readFileSync(
8-
path.resolve(__dirname, '../package.json')));
7+
var pkg = JSON.parse(
8+
fs.readFileSync(path.resolve(__dirname, '../package.json'))
9+
);
910
var directory = [];
1011

1112
var template = _.template(
12-
fs.readFileSync(
13-
path.resolve(__dirname, './docs.template'),
14-
{ encoding: 'utf8' }
15-
)
13+
fs.readFileSync(path.resolve(__dirname, './docs.template'), {
14+
encoding: 'utf8',
15+
})
1616
);
1717

18-
pkg.template = function(str) {
18+
pkg.template = function (str) {
1919
if (typeof str === 'string') {
2020
var regex = /\$\w*/g;
2121
var match = str.match(regex);
@@ -30,58 +30,52 @@ pkg.template = function(str) {
3030
preprocess();
3131

3232
function preprocess() {
33-
34-
_.each(sourceFiles, function(file) {
35-
33+
_.each(sourceFiles, function (file) {
3634
var sourceFile = path.resolve(__dirname, '../', file);
37-
var pivotDir = ['/docs/', file.replace('jsm/', '')
38-
.replace('src/', '').replace('.js', '/')].join('');
35+
var pivotDir = [
36+
'/docs/',
37+
file.replace('jsm/', '').replace('src/', '').replace('.js', '/'),
38+
].join('');
3939

4040
var citations = compiler.explainSync({
4141
files: sourceFile,
42-
cache: false
42+
cache: false,
4343
});
4444

4545
var root = getRoot(citations);
4646

4747
directory.push({ name: root.longname, dir: pivotDir });
48-
4948
});
5049

5150
process();
52-
5351
}
5452

5553
function process() {
56-
57-
_.each(sourceFiles, function(file) {
58-
54+
_.each(sourceFiles, function (file) {
5955
var sourceFile = path.resolve(__dirname, '../', file);
60-
var pivotDir = ['/docs/', file.replace('jsm/', '')
61-
.replace('src/', '').replace('.js', '/')].join('');
56+
var pivotDir = [
57+
'/docs/',
58+
file.replace('jsm/', '').replace('src/', '').replace('.js', '/'),
59+
].join('');
6260

6361
var outputDir = path.resolve(__dirname, '../wiki' + pivotDir);
6462
var outputFile = path.join(outputDir, '/README.md');
6563

6664
var citations = compiler.explainSync({
6765
files: sourceFile,
68-
cache: false
66+
cache: false,
6967
});
7068

71-
citations.slice(0).forEach(function(object) {
72-
69+
citations.slice(0).forEach(function (object) {
7370
var a = object.undocumented;
7471
var b = /package:undefined/i.test(object.longname);
7572
var c = /Two\.Utils\.Events\.(bind|unbind)/i.test(object.memberof);
7673
var d = /(private|protected)/i.test(object.access);
7774

7875
if (a || b || c || d) {
79-
8076
// Remove private / hidden / incomplete documented citations
8177
citations.splice(citations.indexOf(object), 1);
82-
8378
} else {
84-
8579
expandLink(object, 'description');
8680
_.each(object.params, expandParam, object);
8781
_.each(object.returns, expandParam, object);
@@ -90,11 +84,11 @@ function process() {
9084
object.see = _.map(object.see, expandSee, object);
9185

9286
var sn;
93-
sn = object.longname.replace(/#/ig, '.');
87+
sn = object.longname.replace(/#/gi, '.');
9488
var snList = sn.split('.');
95-
var snIndex = (snList.length > 2) ? 2 : 1;
89+
var snIndex = snList.length > 2 ? 2 : 1;
9690
object.shortname = snList.slice(snIndex).join('.');
97-
object.prefixname = sn.replace(object.shortname, "");
91+
object.prefixname = sn.replace(object.shortname, '');
9892

9993
// name and href for augments property
10094
var an;
@@ -108,17 +102,15 @@ function process() {
108102
if (an) {
109103
object.augmentsHref = getHref(an.slice(1).join('.'));
110104
}
111-
112105
}
113-
114106
});
115107

116108
var citationsByScope = {
117109
instance: [],
118-
static: []
110+
static: [],
119111
};
120112

121-
_.each(citations, function(citation) {
113+
_.each(citations, function (citation) {
122114
if (/#/i.test(citation.longname)) {
123115
citationsByScope.instance.push(citation);
124116
} else {
@@ -137,33 +129,43 @@ function process() {
137129
// })
138130
// );
139131

140-
fs.mkdirSync(outputDir + '/', { recursive: true });
141-
fs.writeFileSync(outputFile.replace('README.md', 'docs.json'),
142-
JSON.stringify(citations));
132+
// Ensure outputDir is inside the project root
133+
var projectRoot = path.resolve(__dirname, '../');
134+
var relative = path.relative(projectRoot, outputDir);
143135

144-
fs.writeFileSync(outputFile, template({
145-
root: getRoot(citations, true),
146-
citations: citations,
147-
package: pkg
148-
}));
136+
if (relative && !relative.startsWith('..') && !path.isAbsolute(relative)) {
137+
fs.mkdirSync(outputDir, { recursive: true });
138+
} else {
139+
throw new Error(
140+
'Refusing to create output directory outside project root: ' + outputDir
141+
);
142+
}
143+
fs.writeFileSync(
144+
outputFile.replace('README.md', 'docs.json'),
145+
JSON.stringify(citations)
146+
);
147+
148+
fs.writeFileSync(
149+
outputFile,
150+
template({
151+
root: getRoot(citations, true),
152+
citations: citations,
153+
package: pkg,
154+
})
155+
);
149156

150157
console.log('Generated', outputFile);
151-
152158
});
153-
154159
}
155160

156161
function getHref(name) {
157-
158162
name = name.toLowerCase();
159163

160164
for (var i = 0; i < sourceFiles.length; i++) {
161-
162165
var sf = sourceFiles[i];
163166
if (sf.includes(name)) {
164167
return transform(sf);
165168
}
166-
167169
}
168170

169171
return null;
@@ -172,11 +174,8 @@ function getHref(name) {
172174
var path = str.replace('src/', '').replace('jsm/', '').replace('.js', '');
173175
return `/docs/${path}/`;
174176
}
175-
176177
}
177178

178-
179-
180179
function getRoot(citations, shouldSplice) {
181180
var list = citations.slice(0);
182181
for (var i = 0; i < list.length; i++) {
@@ -214,26 +213,20 @@ function expandParam(param) {
214213
}
215214

216215
function expandLink(object, property) {
217-
218216
var value = object[property];
219217
var shouldRecurse = false;
220218

221219
if (value) {
222-
223220
var regex = /\{@link ([\w\d:/?\-.#]*)\}/i;
224221
var link = value.match(regex);
225222

226223
if (link && link.length > 1) {
227-
228224
var name = link[1];
229225

230226
if (/http/i.test(name)) {
231-
232227
object[property] = value.replace(regex, '[$1]($1)');
233228
shouldRecurse = true;
234-
235229
} else {
236-
237230
var fragments = name.split(/[.#]/i);
238231
var longname = name.replace(/#/i, '.');
239232

@@ -250,24 +243,20 @@ function expandLink(object, property) {
250243
'(',
251244
dir,
252245
hash ? '#' + hash.toLowerCase() : '',
253-
')'
246+
')',
254247
].join('');
255248

256249
object[property] = value.replace(regex, href);
257250
shouldRecurse = true;
258-
259251
}
260-
261252
}
262-
263253
}
264254

265255
if (shouldRecurse) {
266256
expandLink(object, property);
267257
}
268258

269259
return object;
270-
271260
}
272261

273262
function getDirectoryMatch(str) {

wiki/docs/renderers/canvas/README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ A massive object filled with utility functions and properties to render Two.js o
7171

7272
<div class="meta">
7373

74-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1123">
75-
canvas.js:1123
74+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1128">
75+
canvas.js:1128
7676
</a>
7777

7878
</div>
@@ -118,8 +118,8 @@ The `<canvas />` associated with the Two.js scene.
118118

119119
<div class="meta">
120120

121-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1092">
122-
canvas.js:1092
121+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1097">
122+
canvas.js:1097
123123
</a>
124124

125125
</div>
@@ -165,8 +165,8 @@ Associated two dimensional context to render on the `<canvas />`.
165165

166166
<div class="meta">
167167

168-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1098">
169-
canvas.js:1098
168+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1103">
169+
canvas.js:1103
170170
</a>
171171

172172
</div>
@@ -212,8 +212,8 @@ Determines whether the canvas clears the background each draw call.
212212

213213
<div class="meta">
214214

215-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1104">
216-
canvas.js:1104
215+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1109">
216+
canvas.js:1109
217217
</a>
218218

219219
</div>
@@ -259,8 +259,8 @@ The root group of the scenegraph.
259259

260260
<div class="meta">
261261

262-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1115">
263-
canvas.js:1115
262+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1120">
263+
canvas.js:1120
264264
</a>
265265

266266
</div>
@@ -321,8 +321,8 @@ Change the size of the renderer.
321321

322322
<div class="meta">
323323

324-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1129">
325-
canvas.js:1129
324+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1134">
325+
canvas.js:1134
326326
</a>
327327

328328
</div>
@@ -366,8 +366,8 @@ Render the current scene to the `<canvas />`.
366366

367367
<div class="meta">
368368

369-
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1157">
370-
canvas.js:1157
369+
<a class="lineno" target="_blank" rel="noopener noreferrer" href="https://github.com/jonobr1/two.js/blob/main/src/renderers/canvas.js#L1162">
370+
canvas.js:1162
371371
</a>
372372

373373
</div>

0 commit comments

Comments
 (0)