decap-cms icon indicating copy to clipboard operation
decap-cms copied to clipboard

Accept CMS config in multiple formats

Open fool opened this issue 7 years ago • 26 comments

- 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)

fool avatar Apr 21 '17 01:04 fool

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

bdougie avatar May 06 '17 16:05 bdougie

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

fool avatar May 06 '17 19:05 fool

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

erquhart avatar Jun 28 '17 01:06 erquhart

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

CeleRn avatar Jul 04 '17 15:07 CeleRn

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).

erquhart avatar Dec 07 '17 21:12 erquhart

@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 avatar Dec 07 '17 23:12 tech4him1

This will be much easier after #1132.

tech4him1 avatar Mar 29 '18 18:03 tech4him1

@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?

schalkventer avatar Jan 07 '19 12:01 schalkventer

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

tech4him1 avatar Jan 14 '19 16:01 tech4him1

@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.

erquhart avatar Feb 01 '19 17:02 erquhart

@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?

schalkventer avatar Feb 01 '19 19:02 schalkventer

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.

erquhart avatar Feb 02 '19 00:02 erquhart

@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.

schalkventer avatar Feb 08 '19 13:02 schalkventer

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: https://github.com/netlify/netlify-cms/pull/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!

erquhart avatar Feb 08 '19 19:02 erquhart

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?

schalkventer avatar Feb 11 '19 17:02 schalkventer

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 :)

erquhart avatar Feb 11 '19 18:02 erquhart

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.

talves avatar Feb 11 '19 19:02 talves

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.

erquhart avatar Feb 11 '19 20:02 erquhart

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.

talves avatar Feb 11 '19 20:02 talves

True, guess we could just toss in the logic.

erquhart avatar Feb 15 '19 21:02 erquhart

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. https://github.com/netlify/netlify-cms/issues/1132

tech4him1 avatar Feb 20 '19 18:02 tech4him1

@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.

erquhart avatar Feb 25 '19 16:02 erquhart

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.

stale[bot] avatar Oct 29 '19 10:10 stale[bot]

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

Soren234 avatar Dec 30 '20 23:12 Soren234

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

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

zhouhaixian avatar Dec 12 '21 12:12 zhouhaixian

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

snimavat avatar Apr 29 '24 12:04 snimavat