Repository navigation
perf: replace js-yaml with tinyaml #529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { dump, JSON_SCHEMA, loadAll, YAMLException, type DumpOptions } from 'js-yaml' | ||
| import { parse, stringify, type StringifyOptions } from 'tinyaml' | ||
|
|
||
| /** | ||
| * Parse YAML content. | ||
|
|
@@ -7,50 +7,14 @@ import { dump, JSON_SCHEMA, loadAll, YAMLException, type DumpOptions } from 'js- | |
| * @returns The parsed data, or `undefined` when the content has no YAML document | ||
| */ | ||
| export function parseYaml(content: string): Record<string, unknown> | undefined { | ||
| const documents = loadAll(content, { schema: JSON_SCHEMA }) as Record<string, unknown>[] | ||
| // Preserve `load()`'s single-document guard rather than silently taking the first. | ||
| if (documents.length > 1) { | ||
| throw new YAMLException('expected a single document in the stream, but found more') | ||
| } | ||
| return documents[0] | ||
| return parse(content) as Record<string, unknown> | undefined | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve non-finite YAML values across the JSON boundary. tinyaml 0.1.0 parses
|
||
| } | ||
|
|
||
| /** | ||
| * Stringify YAML data | ||
| * @param data - The data to stringify | ||
| * @returns The stringified data | ||
| */ | ||
| export function stringifyYaml(data: Record<string, unknown>, options?: DumpOptions): string { | ||
| const yamlOutput = dump(data, { | ||
| indent: 2, | ||
| lineWidth: -1, | ||
| ...options, | ||
| }) | ||
|
|
||
| /** | ||
| * js-yaml wraps keys with quotes if they start with a colon. This function removes the quotes. | ||
| * `':test': true` becomes `:test: true` | ||
| * | ||
| * Using js-yaml and this function is faster than using other libraries like yaml. | ||
| */ | ||
| const lines = yamlOutput.split('\n') | ||
| for (let i = 0; i < lines.length; i++) { | ||
| const line = lines[i] | ||
| const trimmed = line.trimStart() | ||
|
|
||
| // Check if line starts with a quote followed by colon | ||
| if (trimmed[0] === "'" || trimmed[0] === '"') { | ||
| const quote = trimmed[0] | ||
| if (trimmed[1] === ':') { | ||
| // Find the closing quote | ||
| const quoteEnd = trimmed.indexOf(quote, 1) | ||
| if (quoteEnd > 1 && trimmed[quoteEnd + 1] === ':') { | ||
| // Remove quotes: keep indentation + unquoted key + rest | ||
| const indent = line.length - trimmed.length | ||
| lines[i] = ' '.repeat(indent) + trimmed.slice(1, quoteEnd) + trimmed.slice(quoteEnd + 1) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return lines.join('\n') | ||
| export function stringifyYaml(data: Record<string, unknown>, options?: StringifyOptions): string { | ||
| return stringify(data, options) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For component YAML props containing
limit: .inforfallback: .nan, the new core-schema parser returns non-finite numbers instead of the strings returned by the previous JSON schema. However,comark_block_yamlinpackages/comark/src/plugins/components.ts:315-318still passes every non-string value throughJSON.stringify, so these props silently becomenull, including occurrences nested inside objects or arrays. The original value is therefore lost before rendering or Markdown round-tripping. Preserve these scalars or explicitly reject non-JSON-safe values recursively before passing them into the document's JSON encoding boundary.AGENTS.md reference: AGENTS.md:L9-L9
Useful? React with 👍 / 👎.