Skip to content

Commit a26cfd2

Browse files
Francesco NovyRobbieTheWagner
authored andcommitted
Fix path normalization for Windows (#9)
1 parent e12ca6d commit a26cfd2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/preprocessors/generate-yuidoc.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const Y = require('yuidocjs');
4+
const nodePath = require('path');
45

56
const YUIDoc = Y.YUIDoc;
67
const DIGESTERS = Y.DocParser.DIGESTERS;
@@ -49,10 +50,15 @@ DIGESTERS.yield = function(tagname, value, target, block) {
4950
}
5051

5152
function normalizePaths(document, inputPaths) {
52-
let inputPath = new RegExp(inputPaths.map(i => `${i}/`).join('|'));
53+
let regexStrs = inputPaths
54+
.map((str) => nodePath.join(str, '/'))
55+
.map((str) => str.replace(/\\/g, '\\\\')) // Ensure Windows-style paths are correctly escaped for the regex
56+
.map((str) => str.replace(/\./g, '\\.')); // Ensure eventual dots in path are correctly escaped for the regex
57+
58+
let inputPath = new RegExp(regexStrs.map((str) => `(${str})`).join('|'));
5359

5460
for (let path in document.files) {
55-
let normalizedPath = path.replace(inputPath, '').replace(/(\/index)?\.js/, '');
61+
let normalizedPath = normalizePath(path, inputPath);
5662
let file = document.files[path];
5763

5864
file.name = normalizedPath;
@@ -62,27 +68,34 @@ function normalizePaths(document, inputPaths) {
6268
}
6369

6470
for (let id in document.classes) {
65-
let normalizedId = id.replace(inputPath, '').replace(/(\/index)?\.js/, '');
71+
let normalizedId = normalizePath(id, inputPath);
6672
let klass = document.classes[id];
6773

6874
klass.name = normalizedId;
6975
klass.shortname = normalizedId;
70-
klass.file = klass.file.replace(inputPath, '').replace(/(\/index)?\.js/, '');
76+
klass.file = normalizePath(klass.file, inputPath);
7177

7278
document.classes[normalizedId] = klass;
7379
delete document.classes[id];
7480
}
7581

7682
for (let item of document.classitems) {
77-
item.file = item.file.replace(inputPath, '').replace(/(\/index)?\.js/, '');
78-
item.class = item.class.replace(inputPath, '').replace(/(\/index)?\.js/, '');
83+
item.file = normalizePath(item.file, inputPath);
84+
item.class = normalizePath(item.class, inputPath);
7985
}
8086

8187
for (let warning of document.warnings) {
82-
warning.line = warning.line.replace(inputPath, '');
88+
warning.line = normalizePath(warning.line, inputPath);
8389
}
8490
}
8591

92+
function normalizePath(filePath, inputPath) {
93+
return filePath
94+
.replace(inputPath, '') // Remove root of path
95+
.replace(/\\/g, '/') // Convert windows-style slashes
96+
.replace(/(\/index)?\.js/, ''); // Remove index.js / .js
97+
}
98+
8699
function isAcceptableWarning(warning) {
87100
return warning.message.match(/^unknown tag:/);
88101
}

0 commit comments

Comments
 (0)