Skip to content

Commit

Permalink
refactor: cms
Browse files Browse the repository at this point in the history
  • Loading branch information
Slavik191 committed Apr 4, 2023
1 parent e2f6677 commit dfeb329
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 83 deletions.
10 changes: 7 additions & 3 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import { cacheControlMiddlewareFactory } from "@lidofinance/next-cache-files-mid

export const CACHE_HEADERS_HTML_PAGE = `public, max-age=300, stale-if-error=1200, stale-while-revalidate=30`;

export const CACHE_ALLOWED_LIST_FILES_PATHS = [{ path: /content\/(.*)/, headers: CACHE_HEADERS_HTML_PAGE }];
export const CACHE_ALLOWED_LIST_FILES_PATHS = [
{ path: /content\/(.*)/, headers: CACHE_HEADERS_HTML_PAGE },
];

// use only for cache files
export const middleware = cacheControlMiddlewareFactory(CACHE_ALLOWED_LIST_FILES_PATHS);
export const middleware = cacheControlMiddlewareFactory(
CACHE_ALLOWED_LIST_FILES_PATHS
);

export default middleware;
export default middleware;
9 changes: 7 additions & 2 deletions pages/admin.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { useEffect } from "react";
import { initializeCMS, registerTable } from "widgets/cms";
import { initializeCMS, registerScorecardTable } from "widgets/cms";

export default function Tmp() {
useEffect(() => {
(async () => {
const CMS = (await import("netlify-cms-app")).default;
globalThis.CMS = CMS;

// initialization CMS
initializeCMS();
registerTable();

// Custom widgets
// All custom widgets must be initialized here
// Scorecard:
registerScorecardTable();
})();
}, []);

Expand Down
24 changes: 0 additions & 24 deletions widgets/cms/customDirectiveUtils.ts

This file was deleted.

2 changes: 2 additions & 0 deletions widgets/cms/customWidgets/editorComponent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// scorecard
export * from "./scorecard/table";
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { registerCustomDirective } from "./customDirectiveUtils";
import { registerCustomDirective } from "widgets/cms/utils/customDirectiveUtils";

export const registerTable = () =>
registerCustomDirective("table", {
interface Row {
["scorecard-attribute"]?: string;
["categories"]?: string;
["self-assessment"]?: string;
["comments"]?: string;
}

export const registerScorecardTable = () =>
registerCustomDirective("scorecard-table", {
label: "Table",
fields: [
{
name: "rows",
Expand Down Expand Up @@ -42,9 +50,9 @@ export const registerTable = () =>
],
},
],
toPreview: ({ rows = [] }) => {
toPreview: ({ rows = [] }: { rows: Row[] }) => {
let rowsString = "";
rows.forEach((row) => {
rows.forEach((row: Row) => {
rowsString = `
${rowsString}
${`
Expand Down
2 changes: 2 additions & 0 deletions widgets/cms/customWidgets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Editor components
export * from "./editorComponent";
8 changes: 5 additions & 3 deletions widgets/cms/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from "./customDirectiveUtils";
export * from "./initialize";
export * from "./table";
// Initialization CMS
export * from "./initialize/initialize";

// Custom widgets
export * from "./customWidgets";
46 changes: 0 additions & 46 deletions widgets/cms/initialize.ts

This file was deleted.

1 change: 1 addition & 0 deletions widgets/cms/initialize/collections/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lidoLanding/lidoLanding";
20 changes: 20 additions & 0 deletions widgets/cms/initialize/collections/lidoLanding/files/scorecard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { CmsCollectionFile } from "netlify-cms-core";

export const scorecard: CmsCollectionFile = {
name: "scorecard",
label: "Scorecard",
description: "Scorecard page",
file: "public/content/lido-landing/scorecard.md",
fields: [
{ label: "Title", name: "title", widget: "string" },
{ label: "Description", name: "description", widget: "text" },
{
label: "Body",
name: "body",
widget: "markdown",
buttons: ["heading-two", "link"],
editor_components: ["scorecard-table"],
modes: ["rich_text"],
},
],
};
9 changes: 9 additions & 0 deletions widgets/cms/initialize/collections/lidoLanding/lidoLanding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { CmsCollection } from "netlify-cms-core";

import { scorecard } from "./files/scorecard";

export const lidoLanding: CmsCollection = {
name: "lido-landing",
label: "Lido landing",
files: [scorecard],
};
23 changes: 23 additions & 0 deletions widgets/cms/initialize/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import getConfig from "next/config";

import { lidoLanding } from "./collections";

const { publicRuntimeConfig } = getConfig();
export const initializeCMS = () => {
CMS.init({
config: {
backend: {
name: "github",
repo: publicRuntimeConfig.githubRepo,
branch: publicRuntimeConfig.githubBranch,
base_url: publicRuntimeConfig.baseUrl,
},
local_backend: true,
publish_mode: "editorial_workflow",
show_preview_links: false,
media_folder: "public/img",
public_folder: "img",
collections: [lidoLanding],
},
});
};
32 changes: 32 additions & 0 deletions widgets/cms/utils/customDirectiveUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import jsyaml from "js-yaml";

interface Options {
id?: string;
label?: string;
fields: any;
pattern?: RegExp;
allow_add?: boolean;
fromBlock?: (match: RegExpMatchArray) => any;
toBlock?: (data: any) => string;
toPreview: (data: any) => string;
}

export const renderCustomDirective = (name: string, object: object) =>
`:::${name}\n` + (object ? jsyaml.dump(object) : "") + ":::";

export const customDirectivePattern = (name: string) =>
new RegExp(`^:::${name}\\n([\\s\\S]+?\\n)?:::$`, "m");

export const registerCustomDirective = (name: string, options: Options) =>
CMS.registerEditorComponent({
id: name,
label: options.label || `${name[0].toUpperCase()}${name.slice(1)}`,
pattern: customDirectivePattern(name),
fromBlock([, string]) {
return {
...(jsyaml.load(string) as object),
};
},
toBlock: (yaml) => renderCustomDirective(name, yaml),
...options,
});

0 comments on commit dfeb329

Please sign in to comment.