1
- import type { CmsField , CmsConfig } from 'netlify-cms-core' ;
2
- import CMS from 'netlify-cms-app' ;
3
- import { initIdentity } from './identity-widget' ;
4
- import type { NormalizedPreviewStyle } from './types' ;
1
+ import { InitCmsOptions } from './types' ;
5
2
6
3
export default function initCMS ( {
7
- adminPath ,
4
+ cms ,
8
5
config,
9
- components = { } ,
10
6
previewStyles = [ ] ,
11
- } : {
12
- adminPath : string ;
13
- config : CmsConfig ;
14
- // collections: {
15
- // component;
16
- // css: string;
17
- // collection: CmsCollection;
18
- // }[];
19
- components : Record <
20
- string ,
21
- {
22
- component : React . ComponentType < any > ;
23
- fields : ( CmsField & { name : keyof any } ) [ ] ;
24
- }
25
- > ;
26
- previewStyles : NormalizedPreviewStyle [ ] ;
27
- } ) {
28
- initIdentity ( adminPath ) ;
29
-
7
+ } : InitCmsOptions ) {
30
8
// Provide default values given we can make a reasonable guess
31
9
const mediaDefaults = ! config . media_folder
32
10
? { media_folder : 'public' , public_folder : '/' }
33
11
: { } ;
34
12
35
- CMS . init ( {
13
+ cms . init ( {
36
14
config : {
37
15
// Don’t try to load config.yml as we’re providing the config below
38
16
load_config_file : false ,
@@ -44,97 +22,15 @@ export default function initCMS({
44
22
} ) ;
45
23
46
24
/**
47
- * Another drawback of using Netlify CMS is that it registers all preview
25
+ * One drawback of using Netlify CMS is that it registers all preview
48
26
* styles globally — not scoped to a specific collection.
49
- * You’ve lost Astro component’s scoped styling anyway by being forced
27
+ * You lose Astro components’ scoped styling anyway by being forced
50
28
* to use React, but just be extra careful.
51
29
*
52
30
* The (undocumented?) `raw: true` setting treats the first argument as
53
31
* a raw CSS string to inject instead of as a URL to load a stylesheet from.
54
32
*/
55
33
previewStyles . forEach ( ( [ style , opts ] ) =>
56
- CMS . registerPreviewStyle ( style , opts )
34
+ cms . registerPreviewStyle ( style , opts )
57
35
) ;
58
-
59
- /**
60
- * Register each of our collections’ preview styles and components.
61
- */
62
- // collections.forEach(({ component, collection }) => {
63
- // CMS.registerPreviewTemplate(
64
- // collection.name,
65
- // ({ entry, widgetFor, getAsset }) => {
66
- // // Netlify CMS gives us an Immutable.js object for entry data.
67
- // // Here we convert this into a plain JS object.
68
- // const props = entry.get('data').toJS();
69
-
70
- // // Some data types need extra work before passing them to the component.
71
- // collection.fields.forEach(({ name, widget }) => {
72
- // switch (widget) {
73
- // // Images can be in-memory before they’re uploaded, so we use the
74
- // // `getAsset` helper to get a useable source URL.
75
- // case 'image':
76
- // props[name] = props[name] && getAsset(props[name]).toString();
77
- // break;
78
- // // The `widgetFor` helper hands us rendered Markdown content.
79
- // // We also map fields named `body` to `children` props.
80
- // case 'markdown':
81
- // props[name === 'body' ? 'children' : name] = widgetFor(name);
82
- // break;
83
- // }
84
- // });
85
-
86
- // return component(props);
87
- // }
88
- // );
89
- // });
90
-
91
- /**
92
- * Below implements the infrastructure for custom components in the Markdown
93
- * editor.
94
- *
95
- * Caveats:
96
- * - Components MUST be block-level rather than inline
97
- * - Accessing `frontmatter` is not supported.
98
- * - Components MUST be imported in a post’s `setup` front matter.
99
- * The default value for the hidden `setup` field above does this, but
100
- * can’t update the value if new components are added after a post is
101
- * created.
102
- * - No `client:...` directive support yet. This should be do-able, just
103
- * needs some more RegExp wrangling.
104
- * - Attributes *must* be strings for now. Should be possible to support
105
- * other data types eventually to some degree.
106
- */
107
-
108
- // for (const Name in components) {
109
- // const { component, fields } = components[Name];
110
- // CMS.registerEditorComponent({
111
- // id: Name,
112
- // label: Name,
113
- // fields,
114
- // pattern: new RegExp(`^<${Name} (.*?)/>$`),
115
- // fromBlock: (match) =>
116
- // match[1]
117
- // .split('" ')
118
- // .map((attr) => attr.split('="'))
119
- // .reduce((attrs, [attr, val]) => ({ ...attrs, [attr]: val }), {}),
120
- // toBlock: (obj) => {
121
- // const attrs = Object.entries(obj)
122
- // .map(([attr, val]) => `${attr}="${val}"`)
123
- // .join(' ');
124
- // return `<${Name} ${attrs} />`;
125
- // },
126
- // toPreview: component,
127
- // });
128
- // }
129
-
130
- /**
131
- * Things that would be nice but are currently impossible:
132
- *
133
- * - Expression syntax:
134
- * i. Netlify CMS doesn’t support creating custom inline widgets.
135
- * ii. Even for a custom block widget, Netlify CMS won’t provide
136
- * `frontmatter`, so you’re limited to plain JS, like `{5 * 2}`,
137
- * which is not very useful. (Obviously Astro APIs are also
138
- * not available in the browser.)
139
- */
140
36
}
0 commit comments