storybook-addon-source-link
Version:
Add a button to the Storybook toolbar that opens the file containing the Story in an IDE like VSCode.
226 lines (213 loc) • 6.98 kB
JavaScript
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/assert_path.js
function assertPath(path) {
if (typeof path !== "string") {
throw new TypeError(`Path must be a string, received "${JSON.stringify(path)}"`);
}
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/from_file_url.js
function assertArg(url) {
url = url instanceof URL ? url : new URL(url);
if (url.protocol !== "file:") {
throw new TypeError(`URL must be a file URL: received "${url.protocol}"`);
}
return url;
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/posix/from_file_url.js
function fromFileUrl(url) {
url = assertArg(url);
return decodeURIComponent(url.pathname.replace(/%(?![0-9A-Fa-f]{2})/g, "%25"));
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/strip_trailing_separators.js
function stripTrailingSeparators(segment, isSep) {
if (segment.length <= 1) {
return segment;
}
let end = segment.length;
for (let i = segment.length - 1; i > 0; i--) {
if (isSep(segment.charCodeAt(i))) {
end = i;
} else {
break;
}
}
return segment.slice(0, end);
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/constants.js
var CHAR_DOT = 46;
var CHAR_FORWARD_SLASH = 47;
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/posix/_util.js
function isPosixPathSeparator(code) {
return code === CHAR_FORWARD_SLASH;
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/normalize.js
function assertArg4(path) {
assertPath(path);
if (path.length === 0) return ".";
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/_common/normalize_string.js
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
let res = "";
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code;
for (let i = 0; i <= path.length; ++i) {
if (i < path.length) code = path.charCodeAt(i);
else if (isPathSeparator(code)) break;
else code = CHAR_FORWARD_SLASH;
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) ; else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 || lastSegmentLength !== 2 || res.charCodeAt(res.length - 1) !== CHAR_DOT || res.charCodeAt(res.length - 2) !== CHAR_DOT) {
if (res.length > 2) {
const lastSlashIndex = res.lastIndexOf(separator);
if (lastSlashIndex === -1) {
res = "";
lastSegmentLength = 0;
} else {
res = res.slice(0, lastSlashIndex);
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
}
lastSlash = i;
dots = 0;
continue;
} else if (res.length === 2 || res.length === 1) {
res = "";
lastSegmentLength = 0;
lastSlash = i;
dots = 0;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0) res += `${separator}..`;
else res = "..";
lastSegmentLength = 2;
}
} else {
if (res.length > 0) res += separator + path.slice(lastSlash + 1, i);
else res = path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
dots = 0;
} else if (code === CHAR_DOT && dots !== -1) {
++dots;
} else {
dots = -1;
}
}
return res;
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/posix/normalize.js
function normalize(path) {
if (path instanceof URL) {
path = fromFileUrl(path);
}
assertArg4(path);
const isAbsolute2 = isPosixPathSeparator(path.charCodeAt(0));
const trailingSeparator = isPosixPathSeparator(path.charCodeAt(path.length - 1));
path = normalizeString(path, !isAbsolute2, "/", isPosixPathSeparator);
if (path.length === 0 && !isAbsolute2) path = ".";
if (path.length > 0 && trailingSeparator) path += "/";
if (isAbsolute2) return `/${path}`;
return path;
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/posix/join.js
function join(path, ...paths) {
if (path === void 0) return ".";
if (path instanceof URL) {
path = fromFileUrl(path);
}
paths = path ? [
path,
...paths
] : paths;
paths.forEach((path2) => assertPath(path2));
const joined = paths.filter((path2) => path2.length > 0).join("/");
return joined === "" ? "." : normalize(joined);
}
// ../../node_modules/.pnpm/@jsr+std__path@1.1.2/node_modules/@jsr/std__path/posix/parse.js
function parse(path) {
assertPath(path);
const ret = {
root: "",
dir: "",
base: "",
ext: "",
name: ""
};
if (path.length === 0) return ret;
const isAbsolute2 = isPosixPathSeparator(path.charCodeAt(0));
let start;
if (isAbsolute2) {
ret.root = "/";
start = 1;
} else {
start = 0;
}
let startDot = -1;
let startPart = 0;
let end = -1;
let matchedSlash = true;
let i = path.length - 1;
let preDotState = 0;
for (; i >= start; --i) {
const code = path.charCodeAt(i);
if (isPosixPathSeparator(code)) {
if (!matchedSlash) {
startPart = i + 1;
break;
}
continue;
}
if (end === -1) {
matchedSlash = false;
end = i + 1;
}
if (code === CHAR_DOT) {
if (startDot === -1) startDot = i;
else if (preDotState !== 1) preDotState = 1;
} else if (startDot !== -1) {
preDotState = -1;
}
}
if (startDot === -1 || end === -1 || // We saw a non-dot character immediately before the dot
preDotState === 0 || // The (right-most) trimmed path component is exactly '..'
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
if (end !== -1) {
if (startPart === 0 && isAbsolute2) {
ret.base = ret.name = path.slice(1, end);
} else {
ret.base = ret.name = path.slice(startPart, end);
}
}
ret.base = ret.base || "/";
} else {
if (startPart === 0 && isAbsolute2) {
ret.name = path.slice(1, startDot);
ret.base = path.slice(1, end);
} else {
ret.name = path.slice(startPart, startDot);
ret.base = path.slice(startPart, end);
}
ret.ext = path.slice(startDot, end);
}
if (startPart > 0) {
ret.dir = stripTrailingSeparators(path.slice(0, startPart - 1), isPosixPathSeparator);
} else if (isAbsolute2) ret.dir = "/";
return ret;
}
// src/linkUtil.ts
var getFileUrl = (rootPath, importPath) => {
return new URL(`file:///${join(rootPath, importPath)}`);
};
var joinPath = (...paths) => {
return join(...paths);
};
var parsePath = (path) => {
return parse(path);
};
var normalizePath = (path) => {
return normalize(path);
};
export { getFileUrl, joinPath, normalizePath, parsePath };