-
-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Config UI: clean yaml on paste (#19148)
- Loading branch information
Showing
12 changed files
with
113 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { cleanYaml } from "./cleanYaml"; | ||
|
||
describe("cleanYaml", () => { | ||
test("removes key from single line", () => { | ||
const result = cleanYaml("key: value", "key"); | ||
expect(result).toBe("value"); | ||
}); | ||
test("removes key from multi-line yaml", () => { | ||
const input = `key:\n nested: value\n another: thing`; | ||
const expected = `nested: value\nanother: thing`; | ||
const result = cleanYaml(input, "key"); | ||
expect(result).toBe(expected); | ||
}); | ||
test("keep untouched if key not found", () => { | ||
const result = cleanYaml("key: value", "not-found"); | ||
expect(result).toBe("key: value"); | ||
}); | ||
test("trim whitespace at the end of the line", () => { | ||
const result = cleanYaml("key: \n - foo \n - bar ", "key"); | ||
expect(result).toBe("- foo\n- bar"); | ||
}); | ||
test("should remove leading comment lines", () => { | ||
const result = cleanYaml("# this is\n# a comment\nkey: value", "key"); | ||
expect(result).toBe("value"); | ||
}); | ||
test("should note remove leading comment lines if key is not found", () => { | ||
const result = cleanYaml("# this is\n# a comment\nnot-found: value", "key"); | ||
expect(result).toBe("# this is\n# a comment\nnot-found: value"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// accepts a multiline yaml string. if it starts with a key, the key will be removed, and the indent level will be reduced by one | ||
// example 1: "key: value" -> "value" | ||
// example 2: | ||
// """ | ||
// key: | ||
// foo: bar | ||
// """ | ||
// will be transformed into: | ||
// """ | ||
// foo: bar | ||
// """ | ||
export function cleanYaml(text: string, removeKey: string) { | ||
if (!removeKey) return text; | ||
const result: string[] = []; | ||
|
||
const prefix = removeKey + ":"; | ||
const lines = text.split("\n"); | ||
|
||
// remove first comment lines | ||
while (lines[0].startsWith("#")) lines.shift(); | ||
|
||
const [firstLine, ...restLines] = lines; | ||
|
||
if (!firstLine.startsWith(prefix)) { | ||
// does not start with key, skip | ||
return text; | ||
} else { | ||
const first = firstLine.slice(prefix.length).trim(); | ||
if (first) { | ||
result.push(first); | ||
} | ||
} | ||
|
||
if (restLines.length > 0) { | ||
const indentChars = restLines[0].match(/^(\s+)/)?.[0] || ""; | ||
restLines | ||
.map((l) => (l.startsWith(indentChars) ? l.slice(indentChars.length) : l)) | ||
.map((l) => l.trimEnd()) | ||
.forEach((l) => result.push(l)); | ||
} | ||
|
||
return result.join("\n"); | ||
} |