forked from r.muellner/velum-raycast
fix: restore project placeholders regardless of case
This commit is contained in:
@@ -244,24 +244,41 @@ export function replaceSpans(
|
||||
return output;
|
||||
}
|
||||
|
||||
function restorePattern(mapping: PlaceholderMapping, matchModes: ProjectRun["matchModes"], pathSegment = false): RegExp | undefined {
|
||||
function restorePattern(mapping: PlaceholderMapping, matchModes: ProjectRun["matchModes"], pathSegment = false): {
|
||||
pattern: RegExp; originalFor: (match: string) => string | undefined;
|
||||
} | undefined {
|
||||
const keys = Object.keys(mapping).sort((a, b) => b.length - a.length);
|
||||
if (!keys.length) return undefined;
|
||||
const foldedKeys = new Map<string, string | null>();
|
||||
for (const key of keys) {
|
||||
const folded = key.toUpperCase();
|
||||
if (foldedKeys.has(folded)) foldedKeys.set(folded, null);
|
||||
else foldedKeys.set(folded, key);
|
||||
}
|
||||
const parts = keys.map((key) => {
|
||||
const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
return pathSegment || matchModes?.[key] === "anywhere"
|
||||
? escaped : `(?<![\\p{L}\\p{N}])${escaped}(?![\\p{L}\\p{N}])`;
|
||||
});
|
||||
return new RegExp(parts.join("|"), "gu");
|
||||
return {
|
||||
pattern: new RegExp(parts.join("|"), "giu"),
|
||||
originalFor: (match) => {
|
||||
const key = Object.prototype.hasOwnProperty.call(mapping, match)
|
||||
? match : foldedKeys.get(match.toUpperCase());
|
||||
return key ? mapping[key].original : undefined;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function restoreTextWithCount(source: string, mapping: PlaceholderMapping,
|
||||
matchModes?: ProjectRun["matchModes"]): { text: string; replacements: number } {
|
||||
const pattern = restorePattern(mapping, matchModes);
|
||||
let replacements = 0;
|
||||
const text = pattern ? source.replace(pattern, (key) => {
|
||||
const text = pattern ? source.replace(pattern.pattern, (match) => {
|
||||
const original = pattern.originalFor(match);
|
||||
if (original === undefined) return match;
|
||||
replacements++;
|
||||
return mapping[key].original;
|
||||
return original;
|
||||
}) : source;
|
||||
return { text, replacements };
|
||||
}
|
||||
@@ -275,7 +292,7 @@ export function restorePath(relPath: string, mapping: PlaceholderMapping, matchM
|
||||
// Restricting replacements to word boundaries would leave those names masked.
|
||||
const pattern = restorePattern(mapping, matchModes, true);
|
||||
const segments = safeRelativePath(relPath).split("/").map((segment) => {
|
||||
const restored = pattern ? segment.replace(pattern, (key) => mapping[key].original) : segment;
|
||||
const restored = pattern ? segment.replace(pattern.pattern, (match) => pattern.originalFor(match) ?? match) : segment;
|
||||
if (restored.includes("/") || restored.includes("\\") || restored === "." || restored === "..") {
|
||||
throw new Error("Rückübersetzter Dateiname enthält einen ungültigen Pfadteil.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user