-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
netlify_cms.ts
124 lines (104 loc) 路 3.18 KB
/
netlify_cms.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Page } from "../core/filesystem.ts";
import { merge } from "../core/utils.ts";
import { Exception } from "../core/errors.ts";
import { posix } from "../deps/path.ts";
import { stringify } from "../deps/yaml.ts";
import type { Site } from "../core.ts";
export interface Options {
/** Force the local_backend option. By default is detected automatically. */
local?: boolean;
/** Path of a CSS file with custom styles for the preview */
previewStyle?: string;
/** Directory path of the admin (by default /admin/) */
path: string;
/** Data key of the configuration */
configKey: string;
/** Whether use Netlify Identity */
netlifyIdentity: boolean;
/** Custom HTML code to append in the index.html page */
extraHTML: string;
}
const defaults: Options = {
local: undefined,
path: "/admin/",
configKey: "netlify_cms",
netlifyIdentity: false,
extraHTML: "",
};
/** A plugin to use Netlify CMS in Lume easily */
export default function (userOptions?: Partial<Options>) {
const options = merge(defaults, userOptions);
return (site: Site) => {
const local_backend = typeof options.local === "boolean"
? options.local
: site.options.location.hostname === "localhost";
// Run the local netlify server
if (local_backend) {
site.addEventListener("afterBuild", () => {
site.run("npx netlify-cms-proxy-server");
});
}
// Build the admin page
site.addEventListener("afterRender", () => {
const root = site.source.root!;
const config: Record<string, unknown> | undefined = root
.data[options.configKey] as Record<string, unknown> | undefined;
if (!config) {
throw new Exception("Missing configuration for Netlify CMS", {
key: options.configKey,
});
}
// Create config.yml
const configUrl = posix.join(options.path, "config.yml");
site.pages.push(Page.create(
configUrl,
stringify({
...config,
site_url: site.options.location.href,
local_backend,
}),
));
// Create index.html
const code = [];
code.push(
`<link href="${
site.url(configUrl)
}" type="text/yaml" rel="cms-config-url">`,
);
code.push(
`<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js"></script>`,
);
if (options.netlifyIdentity) {
code.push(
`<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>`,
);
}
if (options.extraHTML) {
code.push(options.extraHTML);
}
if (options.previewStyle) {
code.push(
`<script>CMS.registerPreviewStyle("${
site.url(options.previewStyle)
}");</script>`,
);
}
site.pages.push(Page.create(
posix.join(options.path, "index.html"),
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin</title>
</head>
<body>
${code.join("")}
</body>
</html>
`,
));
});
};
}