-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
theme.json
can't have comments
#35099
Comments
Is adding a schema link into theme.json an alternative approach to comments? eg. |
you can also write ✍🏽 the configuration files 📝 in YAML and convert it to JSON with tools like https://www.json2yaml.com/convert-yaml-to-json YAML is also a superset of JSON and uses python 🐍 like line separation & indentation and commenting with theme configuration file in YAMLsettings:
spacing:
# blockGap: "The design calls for a 24px gap. Coincidentally, that's the default value that Gutenberg defines. The default could change in the future, though, so this should be explicitly set."
blockGap: 24px
custom:
alignment:
# alignedMaxWidth: Foo bar blah blah blah...
alignedMaxWidth: 50% the equivalent JSON code converted from YAML with https://www.json2yaml.com/convert-yaml-to-json{
"settings": {
"spacing": {
"blockGap": "24px"
},
"custom": {
"alignment": {
"alignedMaxWidth": "50%"
}
}
}
} |
I think those are both good workarounds, but not ideal solutions. Maintaining a fork of the canonical schema would be too much work for the benefit you'd get IMO, could lead to things being out of sync and inaccurate, and tooltips are really easy to miss. I think having to setup and run extra tooling would also add more pain than it'd remove. Tooling often breaks, someone might manually modify the I think using YAML directly is worth considering, though, instead of JSON5. It might come down to which has a more reliable PHP parser. |
I would also like to see the default format changed. Between JSON5 and YAML, I'd strongly prefer YAML. It's much easier for humans to read/write, no commas or quotes to worry about. I tried using the $schema with JSON, but it leaves a lot to be desired. I just created a YAML version of theme.json and documented it (including the new/experimental stuff that's missing from $schema) for the other developers at my company and added it to my webpack build/watch tasks to convert it back to JSON. I save it as a single line of text and don't pretty print it so anyone who opens theme.json will realize it's a generated file. It works, I'm happy with it, but would still prefer native support. YAML parsers are available for just about every language and are fast. Going forward theme.json could be deprecated and only loaded if theme.yaml isn't present. |
Using a file format that doesn't provide for comments is frankly indefensible. |
I strongly recommend using YAML instead of JSON for the In case sombody is interested: Personally I use a setup where I convert a |
+1 for YAML. One of the biggest barriers for developers new to FSE and Hybrid themes is understanding what and why things exist in theme.json. As a developer on the project I have a responsibility to leave comments for the team and at the moment these are either added to a seperate readme or are discussed ad-hoc on Slack, neither are ideal and both get lost over time. |
Hi there, It's very simple and converts an existing theme.yaml file to theme.json. Basically it's for people who know what they are doing and obviously you should not touch the theme.json file manually. You can find it here: https://wordpress.org/plugins/theme-yaml/ |
But obviously I would prefer if theme.yaml would be supported by core Wordpress and not via a plugin/workaround. |
This appears to work in theme.json: One way is using adding key-value pairs with comments and description JSON always contains data of key and values, so add comments key in json object with value is a comment string value. { More here: https://www.w3schools.io/file/json-comments/ Also how about the newer JSON5 which allows comments: https://json5.org/ |
In #34180 we added comments like this
|
I've tried that workaround in other contexts, but they're often removed by encoders because they're duplicate and/or invalid keys (e.g., My IDE also adds an inspection warning because of the duplicate key. If you disambiguate it with something like It's an ok workaround for now, but I think it'd be much better to solve the root issue instead. |
That's great news @scruffian. However, is you the WP JSON scheme (which is very handy, nearly essential to use I think), your IDE will report errors with your |
@onetrev That style of comment isn't valid for theme.json yet, only for block.json, for now. |
For anyone considering YAML, if you are VS Code person, then I've found this work flow to be pretty smooth. And it has working schema support for error checking! Add a YAML language server to enable schema support. Then add a YAML to JSON converter. Job done. You have comments, schema error checking, and your brain won't explode by trying to work with a huge JSON file. :) The only thing missing is having different parts of the main YAML / JSON file coming from different source files. That would be the nirvana solution for sure. |
I've noticed for a while that comments are now allowed in theme.json files, like so:
So can we now close this issue @iandunn / @scruffian? |
This feels like a hacky work-around for a poor decision, but if this is all we have left then this issue should not be closed until this information appears in the appropriate documentation for this file. |
What problem does this address?
theme.json
is a configuration file maintained by humans, rather than a data-interchange file used by software. Comments are an essential feature of any configuration file, in order to document important decisions, explain things that aren't obvious, etc. For example:JSON tooling usually (and mistakenly) doesn't allow comments, though. Many tools will strip them out, or just fail to run. There are lots of workarounds, but the half dozen I've tried have all had significant downsides. For example:
...results in:
What is your proposed solution?
Consider using JSON5 instead of JSON. It's a superset, so there aren't any back-compat issues.
If that's done, we'll need to document it, otherwise devs will assume they can't use comments and miss out on the benefits.
The text was updated successfully, but these errors were encountered: