decaporg / decap-cms

A Git-based CMS for Static Site Generators
https://decapcms.org
MIT License
17.95k stars 3.05k forks source link

Accept CMS config in multiple formats #386

Open fool opened 7 years ago

fool commented 7 years ago

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

- 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 commented 7 years ago

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

fool commented 7 years ago

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

erquhart commented 7 years ago

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 commented 7 years ago

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

erquhart commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

This will be much easier after #1132.

schalkventer commented 5 years ago

@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 commented 5 years ago

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

erquhart commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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!

schalkventer commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

True, guess we could just toss in the logic.

tech4him1 commented 5 years ago

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

erquhart commented 5 years ago

@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[bot] commented 5 years ago

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 commented 3 years ago

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

zhouhaixian commented 2 years ago

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

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

snimavat commented 6 months ago

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