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

Accept CMS config in multiple formats #386

Open
fool opened this issue Apr 21, 2017 · 26 comments
Open

Accept CMS config in multiple formats #386

fool opened this issue Apr 21, 2017 · 26 comments

Comments

@fool
Copy link

fool commented Apr 21, 2017

- Do you want to request a feature or report a bug?

neither, but, a suggestion for the docs.

- What is the current behavior?

hexo can disable the CMS non-intuitively.

- If the current behavior is a bug, please provide the steps to reproduce.

  • run a hexo site
  • put cms in /admin/
  • use config.yml as your config file
  • run hexo generate in /
  • notice that config.yml is no longer present and CMS no longer loads in deployed site

- What is the expected behavior?

the docs would mention this :)

- Please mention your node.js, and operating system version.

(so really this is about the docs, not cms version)

@bdougie
Copy link
Contributor

bdougie commented May 6, 2017

I think #348 might have solved this. JSON is an acceptable format

@fool
Copy link
Author

fool commented May 6, 2017

probably worth testing the resulting json to make sure it works. I think #348 was already merged before I got this report.

@erquhart
Copy link
Contributor

#348 only affected markdown frontmatter. #238 will address this issue, but if someone wants to document it as a stop gap, it'd help.

@CeleRn
Copy link

CeleRn commented Jul 4, 2017

Change Build command:
hexo generate
to
hexo generate; cp source/admin/config.yml public/admin/config.yml

@erquhart erquhart changed the title hexo generate transforms "config.yml" to "config.json" Accept CMS config in multiple formats Dec 7, 2017
@erquhart
Copy link
Contributor

erquhart commented Dec 7, 2017

Currently the CMS config only works in yaml, but we may as well support any other data format that we can handle (currently yaml, json, and toml).

@tech4him1
Copy link
Contributor

@erquhart I'm with supporting all the formats, but the main problem is knowing what config filename to fetch. Right now we only fetch config.yml and fail if it is not found.

@tech4him1
Copy link
Contributor

This will be much easier after #1132.

@schalkventer
Copy link

schalkventer commented Jan 7, 2019

@erquhart @tech4him1 I'm happy to give this a go via config.json and config.js, since I would love to be able to dynamically generate values and import NPM modules like https://www.npmjs.com/package/change-case into a config.js.

Just checking that this issue is still open, and no one is actively working on it?

@tech4him1
Copy link
Contributor

@schalkventer It's yours (sorry for the delay in getting back to you)!

@erquhart
Copy link
Contributor

erquhart commented Feb 1, 2019

@schalkventer actually, this issue is focused on multiple data formats, eg. json, toml, etc. What you're trying to do can already be done via injecting config during initialization: https://www.netlifycms.org/docs/beta-features/#manual-initialization

I've also thought about having a straight js config file as an option, but it's a bit bigger than this issue.

@schalkventer
Copy link

schalkventer commented Feb 1, 2019

@erquhart, yeah I realised this after posting the above. 👍

Regardless I'm happy attempting this none-the-less. Where can I learn a bit more about how data is loaded into Netlify CMS? Or would it take some reverse engineering?

@erquhart
Copy link
Contributor

erquhart commented Feb 2, 2019

After considering this a bit more, I remembered that manual initialization is the only way to do this. Without manual initialization, the CMS auto bootstraps, so loading the config is always a race condition.

Have you tried JS config via manual init yet? Interested to hear if there are any downsides.

@schalkventer
Copy link

@erquhart, by manual init do you mean via import { init } from 'netlify-cms';? I'm using it in one project and have yet to run into any issues.

@erquhart
Copy link
Contributor

erquhart commented Feb 8, 2019

Yeah that's the one. So the way you do JavaScript config, in case you haven't tried it:

import { init } from 'netlify-cms'

init({
  config: {
    backend: {
      name: 'test-repo',
    },
  },
})

It's overwritten by config.yml if that file is present, but this behavior can be switched off starting with the next release: #2053

The reason we can't pull in a config.js file the way we pull in config.yml is because yaml/toml/json are string formats that we can simply read in and parse, whereas JavaScript has to be executed, and may also require transpiling via Babel, something I'm not keen to introduce when init already allows JavaScript configuration.

You could also approximate what you're looking for by creating a config.js, importing it into your CMS script and passing it to init.

Let me know if you have any further thoughts!

@schalkventer
Copy link

Yup. Unfortunately only realized this after my initial comment in this issue. However, I'm happy to attempt to add .json if that would resolve this issue?

@erquhart
Copy link
Contributor

Resolving this issue should entail config support for all of the formats that Netlify CMS can parse, which is YAML, JSON, and TOML. Technically JSON is already supported, as YAML is a superset of JSON (so valid JSON is always valid YAML), but the CMS won't pick up a config.json file due to the extension. You can, however, work around this by specifying the config path.

Most (maybe even all) of the code that currently requires the config file to be YAML is in the config redux action file, namely these two functions:

https://github.com/netlify/netlify-cms/blob/15d221d4a4d16b795893441108cfc05909ca347a/packages/netlify-cms-core/src/actions/config.js#L13-L23

https://github.com/netlify/netlify-cms/blob/15d221d4a4d16b795893441108cfc05909ca347a/packages/netlify-cms-core/src/actions/config.js#L54-L62

As @tech4him1 mentioned, there's the issue of knowing what file to look for. I think it would be fine to just look for config files in order, maybe yaml -> json -> toml. Which is kinda silly if you're using config.toml as you'll have two 404's every time. But I guess you can avoid them by setting the config path. Which you should be able to do via config (manual init), but the CMS doesn't have initialization stages, so it can't currently get manual init config and then determine based on that config where to load the actual config file from. That would be ideal.

Eventually I expect most config files to be passed in via manual init or pulled from the repo, as static deployed config files won't work with the Configuration UI that is definitely happening at some point, and which I expect most folks will want to use.

Whew. All the things. Anyhow, yeah, worth making config.json and config.toml work if that's beneficial to you :)

@talves
Copy link
Collaborator

talves commented Feb 11, 2019

Which you should be able to do via config (manual init), but the CMS doesn't have initialization stages, so it can't currently get manual init config and then determine based on that config where to load the actual config file from.

Sorry, this is confusing. If you set it to manual init, the init config you pass is able to be read prior to the loading of the config.yml. Maybe I am misreading what you mean here.

@erquhart
Copy link
Contributor

erquhart commented Feb 11, 2019

Context:

But I guess you can avoid them by setting the config path. Which you should be able to do via config (manual init)

For example:

config_path: "config.toml"

That ^^ would be ideal, but it can't work currently because Netlify CMS doesn't process any configuration values until all configuration has been registered, and then the final object is used to configure the CMS.

I expect that we'll at some point accept partial configuration for cases like this, config driven manual init, or setting just the backend where the actual config source is, which we would then pull through the API and use to configure the CMS.

@talves
Copy link
Collaborator

talves commented Feb 11, 2019

But that is where I am getting confused, because we do process the manual init prior to going after the config.yml. That is how I ignore the config.yml loading using the load_config_file: false flag.

We could just pass a load_config_file_path into the init and have that override the default file if it exists.

@erquhart
Copy link
Contributor

True, guess we could just toss in the logic.

@tech4him1
Copy link
Contributor

Sorry if I'm missing context here, but lets not forget we have the header value that we can set format on as well. That was part of the original intent for that addition IIRC. #1132

@erquhart
Copy link
Contributor

erquhart commented Feb 25, 2019

@tech4him1 that PR opened the door for custom naming of the config file, and provided a means for specifying a non-yaml format, but didn't include support for actually handling non-yaml formats. By contrast, this issue isn't really concerned with renaming and focuses only on supporting non-yaml formats.

@stale
Copy link

stale bot commented Oct 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Soren234
Copy link

You can use skip_render: "*admin/*" in config for Hexo, so that yaml does not convert to json.

@zhouhaixian
Copy link

您可以skip_render: "*admin/*"在 Hexo 的配置中使用,这样 yaml 就不会转换为 json。

Thank you so much!This solves the problem that I am bothering me for two hours.

@snimavat
Copy link

is it possible to have a json config file ? or a js file tht exports json etc...
tht makes it really easy to reuse some of the config across collections etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests