diff --git a/apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.utils.ts b/apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.utils.ts index a0659cf8aca..519c40c25e0 100644 --- a/apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.utils.ts +++ b/apps/docs/components/ProjectConfigVariables/ProjectConfigVariables.utils.ts @@ -8,6 +8,26 @@ export type Branch = BranchesData[number] export type Variable = 'url' | 'anonKey' +function removeDoubleQuotes(str: string) { + return str.replaceAll('"', '') +} + +function escapeDoubleQuotes(str: string) { + return str.replaceAll('"', '\\"') +} + +function unescapeDoubleQuotes(str: string) { + /** + * Regex matches a backslash followed by a double quote, except when the + * backslash is escaped. + * + * Negative lookbehind: + * \\(?:\\\\)* -> An odd number of backslashes (means the next backslash is escaped) + * (?:^|[^\\]) -> Backslash group is at start or follows non-backslash + */ + return str.replace(/(? = { url: 'Project URL', anonKey: 'Anon key', @@ -18,15 +38,19 @@ export function toDisplayNameOrgProject(org: Org, project: Project) { } export function toOrgProjectValue(org: Org, project: Project) { - // @ts-ignore -- problem in OpenAPI spec -- project has ref property - return JSON.stringify([org.id, project.ref, toDisplayNameOrgProject(org, project)]) + return escapeDoubleQuotes( + // @ts-ignore -- problem in OpenAPI spec -- project has ref property + JSON.stringify([org.id, project.ref, removeDoubleQuotes(toDisplayNameOrgProject(org, project))]) + ) } export function fromOrgProjectValue( maybeOrgProject: string ): [string, string, string] | [null, null, null] { try { - const data = JSON.parse(maybeOrgProject) + // not restoring the double quotes on the display name is fine because it's only used for + // command fuzzy search, not for exact/literal matching + const data = JSON.parse(unescapeDoubleQuotes(maybeOrgProject)) if (!Array.isArray(data) || data.length !== 3) { throw Error("Shape of parsed JSON doesn't match form of org and project value") } @@ -37,12 +61,14 @@ export function fromOrgProjectValue( } export function toBranchValue(branch: Branch) { - return JSON.stringify([branch.id, branch.name]) + return escapeDoubleQuotes(JSON.stringify([branch.id, removeDoubleQuotes(branch.name)])) } export function fromBranchValue(maybeBranch: string): [string, string] | [null, null] { try { - const data = JSON.parse(maybeBranch) + // not restoring the double quotes on the branch name is fine because it's only used for + // command fuzzy search, not for exact/literal matching + const data = JSON.parse(unescapeDoubleQuotes(maybeBranch)) if (!Array.isArray(data) || data.length !== 2) { throw Error("Shape of parsed JSON doesn't match form of branch value") }