Skip to content
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

Add commentString option #336

Merged
merged 1 commit into from
Nov 26, 2021
Merged

Add commentString option #336

merged 1 commit into from
Nov 26, 2021

Conversation

eemeli
Copy link
Owner

@eemeli eemeli commented Nov 15, 2021

Related: #322, cc @leipert

With this, it's possible to define a custom stringifier for comments, using a pretty simple API:

type ToStringOptions = {
  /**
   * Comment stringifier.
   * Output should be valid for the current schema.
   *
   * By default, empty comment lines are left empty,
   * lines consisting of a single space are replaced by `#`,
   * and all other lines are prefixed with a `#`.
   */
  commentString?: (comment: string) => string,
  ...
}

This has at least three potential uses:

  1. Folding long comment lines.
  2. Ensuring that a space always follows the #.
  3. Using the library to emit non-YAML comments, such as /* foo */.

The default value is rather compact:

const commentString = (comment: string) => comment.replace(/^(?!$)(?: $)?/gm, '#')

@eemeli eemeli merged commit b94d10b into master Nov 26, 2021
@eemeli eemeli deleted the custom-comments branch November 26, 2021 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant