deno.land / x / lume@v2.1.4 / plugins / decap_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Page } from "../core/file.ts";import { merge } from "../core/utils/object.ts";import { posix } from "../deps/path.ts";import { stringify } from "../deps/yaml.ts";
import type Site from "../core/site.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 a identity method */ identity?: "netlify";
/** Custom HTML code to append in the index.html page */ extraHTML?: string;
/** Command to run the proxy server */ proxyCommand?: string;}
export const defaults: Options = { local: undefined, path: "/admin/", configKey: "decap_cms", extraHTML: "", proxyCommand: "deno run --allow-read --allow-net=0.0.0.0 --allow-write --allow-env npm:decap-server",};
/** A plugin to use Decap CMS in Lume easily */export default function (userOptions?: 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("afterStartServer", () => { site.run(options.proxyCommand); }); }
// Build the admin page site.addEventListener("afterRender", () => { const config = site.source.data.get("/")?.[options.configKey] as | Record<string, unknown> | undefined;
if (!config) { throw new Error( `Missing configuration for Netlify CMS: ${options.configKey}`, ); }
// Create config.yml const configUrl = posix.join(options.path, "config.yml");
site.pages.push(Page.create({ url: configUrl, content: 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/decap-cms@3.0.12/dist/decap-cms.js"></script>`, );
if (options.identity === "netlify") { 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({ url: posix.join(options.path, "index.html"), content: ` <!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> `, }));
// Redirect to the admin page from the home page if the URL has a token if (options.identity === "netlify") { const homePage = site.pages.find((page) => page.data.url === "/"); const document = homePage?.document;
if (document) { const script = document.createElement("script"); script.innerHTML = `if (document.location.hash.startsWith("#invite_token=") || document.location.hash.startsWith("#recovery_token=")) { document.location = "${ site.url(options.path) }" + document.location.hash; }`; document.head.appendChild(script); } } }); };}
/** Extends Data interface */declare global { namespace Lume { export interface Data { /** * Decap CMS configuration * @see https://lume.land/plugins/decap_cms/ */ decap_cms: Record<string, unknown>; } }}
lume

Version Info

Tagged at
6 months ago