Skip to content

Commit

Permalink
Merge pull request #14 from weseek/feat/save-with-frontmatter
Browse files Browse the repository at this point in the history
feat: Save cmsMetadata in frontmatter
  • Loading branch information
yuki-takei authored Oct 12, 2023
2 parents 59a0768 + 34ab03e commit f9e14a2
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"express-validator": "^6.14.0",
"extensible-custom-error": "^0.0.7",
"form-data": "^4.0.0",
"front-matter": "^4.0.2",
"graceful-fs": "^4.1.11",
"hast-util-select": "^5.0.5",
"helmet": "^4.6.0",
Expand Down
42 changes: 42 additions & 0 deletions apps/app/src/features/cms/server/utils/extract-frontmatter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { extract } from './extract-frontmatter';

describe('extract-frontmatter', () => {

it('returns undefined when the document does not have frontmatter', () => {
// when
const result = extract('# Heading\nsome text');
// then
expect(result).toBeUndefined();
});

it('returns undefined when the frontmatter does not have "cmsMetadata" key', () => {
// when
const result = extract(`---
title: This is the Title
---
# Heading
some text
`);
// then
expect(result).toBeUndefined();
});

it('extract data collectly', () => {
// when
const result = extract(`---
title: This is the Title
cms:
foo: bar
---
# Heading
some text
`);
// then
expect(result).toStrictEqual({
foo: 'bar',
});
});

});
14 changes: 14 additions & 0 deletions apps/app/src/features/cms/server/utils/extract-frontmatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// TODO: replace with remark
import fm from 'front-matter';
import yaml from 'js-yaml';

export const extract = (markdown: string): Record<string, any> | undefined => {
const extractedData = fm(markdown);

const { frontmatter } = extractedData;
if (frontmatter == null) {
return undefined;
}

return yaml.load(frontmatter).cms;
};
1 change: 1 addition & 0 deletions apps/app/src/features/cms/server/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './extract-frontmatter';
7 changes: 7 additions & 0 deletions apps/app/src/server/models/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface PageDocument extends IPage, Document {
[x:string]: any // for obsolete methods
getLatestRevisionBodyLength(): Promise<number | null | undefined>
calculateAndUpdateLatestRevisionBodyLength(this: PageDocument): Promise<void>
updateCmsMetadata(cmsMetadata: Record<string, any>): Promise<void>
}


Expand Down Expand Up @@ -108,6 +109,8 @@ const schema = new Schema<PageDocument, PageModel>({
updatedAt: { type: Date, default: Date.now }, // Do not use timetamps for updatedAt because it breaks 'updateMetadata: false' option
deleteUser: { type: ObjectId, ref: 'User' },
deletedAt: { type: Date },
// for CMS
cmsMetadata: { type: Map },
}, {
timestamps: { createdAt: true, updatedAt: false },
toJSON: { getters: true },
Expand Down Expand Up @@ -1013,6 +1016,10 @@ schema.methods.calculateAndUpdateLatestRevisionBodyLength = async function(this:
await this.save();
};

schema.methods.updateCmsMetadata = async function(this: PageDocument, cmsMetadata: Record<string, any>): Promise<void> {
await this.updateOne({ $set: { cmsMetadata } });
};

export type PageCreateOptions = {
format?: string
grantUserGroupId?: ObjectIdLike
Expand Down
15 changes: 15 additions & 0 deletions apps/app/src/server/service/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import escapeStringRegexp from 'escape-string-regexp';
import mongoose, { ObjectId, Cursor } from 'mongoose';
import streamToPromise from 'stream-to-promise';

import { extract as extractCmsMetadata } from '~/features/cms/server/utils';
import { SupportedAction } from '~/interfaces/activity';
import { V5ConversionErrCode } from '~/interfaces/errors/v5-conversion-error';
import {
Expand Down Expand Up @@ -3707,6 +3708,13 @@ class PageService {
// Update descendantCount
await this.updateDescendantCountOfAncestors(page._id, 1, false);

// Update cmsMetadata
// TODO: update only when the document is under a valid cms namespace
const cmsMetadata = extractCmsMetadata(page.revision.body);
if (cmsMetadata != null) {
page.updateCmsMetadata(cmsMetadata);
}

// Delete PageRedirect if exists
try {
await PageRedirect.deleteOne({ fromPath: page.path });
Expand Down Expand Up @@ -3927,6 +3935,13 @@ class PageService {
await Page.applyScopesToDescendantsAsyncronously(currentPage, user);
}

// 4. Update cmsMetadata
// TODO: update only when the document is under a valid cms namespace
const cmsMetadata = extractCmsMetadata(currentPage.revision.body);
if (cmsMetadata != null) {
await currentPage.updateCmsMetadata(cmsMetadata);
}

await PageOperation.findByIdAndDelete(pageOpId);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/interfaces/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type IPage = {
latestRevision?: Ref<IRevision>,
latestRevisionBodyLength?: number,
expandContentWidth?: boolean,
// for CMS
cmsMetadata?: Record<string, any>;
}

export type IPagePopulatedToList = Omit<IPageHasId, 'lastUpdateUser'> & {
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7902,6 +7902,13 @@ fresh@0.5.2:
version "0.5.2"
resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7"

front-matter@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-4.0.2.tgz#b14e54dc745cfd7293484f3210d15ea4edd7f4d5"
integrity sha512-I8ZuJ/qG92NWX8i5x1Y8qyj3vizhXS31OxjKDu3LKP+7/qBgfIKValiZIEwoVoJKUHlhWtYrktkxV1XsX+pPlg==
dependencies:
js-yaml "^3.13.1"

fs-constants@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
Expand Down

0 comments on commit f9e14a2

Please sign in to comment.