import { decapUrl, serverUrl } from "../deps/decap.ts"; 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 { log } from "../core/utils/log.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 ${serverUrl}`, }; /** * A plugin to use Decap CMS in Lume easily * @see https://lume.land/plugins/decap_cms/ */ export function decapCMS(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 decap server if (local_backend) { site.addEventListener("afterStartServer", () => { site.run(options.proxyCommand); }); } // Create the admin HTML page const configUrl = posix.join(options.path, "config.yml"); const code: string[] = []; code.push( ``, ); code.push( ``, ); if (options.identity === "netlify") { code.push( ``, ); } if (options.extraHTML) { code.push(options.extraHTML); } if (options.previewStyle) { code.push( ``, ); } // Register the page site.page({ url: posix.join(options.path, "index.html"), unlisted: true, layout: undefined, content: `