Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make spear to be changeable the api client via plugin parameter. #189

Merged
merged 3 commits into from
Aug 29, 2023

Conversation

mantaroh
Copy link
Collaborator

@mantaroh mantaroh commented Aug 28, 2023

Changes

This PR make spear to support the Content Collection (#69). This feature collect content data from local files like mdx, unified eco system files(front-matter). [1][2]

In order to collect the content data from file, we need to replace the sdk-js client on spearly-cms-js-core[3].

This changes contains the two parts:

  1. Change the cms-js-core to be able to inject the sdk-js api client.
  2. Pass the cms-js-core generator to plugin via plugin parameter.

The (1) allow the changing customized client which returning local file. Hence this feature, user can replace the resource data without Spearly CMS.
Then user can pass the this customized client via plugin by (2) changes.

After releasing this feature, we can implement the following plugin.

{
        "pluginName": "markdown-client",
        "configuration": null,
        "beforeBuild": async (state, option) => {
          try {
            state.jsGenerator.injectFakeApiClient({
              analytics: {
                pageView: (params) => {
                  console.log("Unimplemented");
                },
              },
              getList: (contentTypeId, params) => {
                console.log("Unimplemented");
              },
              // Generate content from markdown files
              getContent: async (contentTypeId, contentId, params) => {
                const fileStat = await fs.stat(`./data/${contentTypeId}/${contentId}.mdx`)
                const file = await fs.readFile(`./data/${contentTypeId}/${contentId}.mdx`)
                const {attributes, body} = fm(file.toString())
                const bodyHtml = await remark().use(html).process(body);
                const fields = [];
                for (const key of Object.keys(attributes)) {
                  fields.push({
                    key,
                    value: attributes[key]
                  })
                }
                fields.push({
                  key: "body",
                  value: bodyHtml.toString()
                })

                return generateContent(fields, contentId, fileStat.birthtime, fileStat.mtime)
              },
              getContentPreview: (contentTypeId, contentId, previewToken) => {
                console.log("Unimplemented");
              }
            })
          } catch (e) {
            console.error(e)
          }
        },
        "afterBuild": null,
        "bundle": null,
}

[1] https://unifiedjs.com/
[2] https://mdxjs.com/
[3] #69 (comment)


変更点

この PR では Spear 上で Content Collection をサポートさせるようにします(#69)。この機能は mdx や unified エコシステムのファイルのように、ローカルファイルからコンテンツデータを収集します[1][2]。

ファイルからデータを収集するために、 spearly-cms-js-coresdk-js のクライアントを置きかけ出来るようにします[3]。

この変更は2つのパートに分かれています。

  1. cms-js-coresdk-js API クライアントの差し替えを可能にする
  2. cms-js-core の Generator をプラグインのプラグイン引数で渡すようにする

(1) はローカルファイルを返すカスタムされたクライアントを変更できるようにしています。この機能により、ユーザーは Spearly CMS 抜きでリソースデータを置換可能です。
そして、ユーザーはこのカスタムされたクライアントをプラグイン経由で渡すことができます。

この機能をリリース後、以下のような MDX からコンテンツを取得するプラグインを書くことができます。

{
        "pluginName": "markdown-client",
        "configuration": null,
        "beforeBuild": async (state, option) => {
          try {
            state.jsGenerator.injectFakeApiClient({
              analytics: {
                pageView: (params) => {
                  console.log("Unimplemented");
                },
              },
              getList: (contentTypeId, params) => {
                console.log("Unimplemented");
              },
              // Generate content from markdown files
              getContent: async (contentTypeId, contentId, params) => {
                const fileStat = await fs.stat(`./data/${contentTypeId}/${contentId}.mdx`)
                const file = await fs.readFile(`./data/${contentTypeId}/${contentId}.mdx`)
                const {attributes, body} = fm(file.toString())
                const bodyHtml = await remark().use(html).process(body);
                const fields = [];
                for (const key of Object.keys(attributes)) {
                  fields.push({
                    key,
                    value: attributes[key]
                  })
                }
                fields.push({
                  key: "body",
                  value: bodyHtml.toString()
                })

                return generateContent(fields, contentId, fileStat.birthtime, fileStat.mtime)
              },
              getContentPreview: (contentTypeId, contentId, previewToken) => {
                console.log("Unimplemented");
              }
            })
          } catch (e) {
            console.error(e)
          }
        },
        "afterBuild": null,
        "bundle": null,
}

[1] https://unifiedjs.com/
[2] https://mdxjs.com/
[3] #69 (comment)

@@ -23,11 +23,6 @@ export default async function inMemoryMagic(
const logger = new SpearLog(settings.quiteMode);
settings.targetPagesPathList = settings.targetPagesPathList || [];

const jsGenerator = new SpearlyJSGenerator(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビュー用コメント: InMemoryMagic はブラウザ上で Spear を動かすために作成しましたが、 Web Containers をサポートしたことにより不要になります。(#187 のバグで今後削除予定)
Comments for Review: InMemoryMagic enable running Spear on browser, however this feature might to be unnecessary due to Web Containers (We can drop this feature on #187)

@@ -28,6 +29,7 @@ export interface SpearState {
out: {
assetsFiles: AssetFile[]
}
jsGenerator: SpearlyJSGenerator
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビュー用コメント: SpearState はプラグインの各関数の引数として渡されます。 プラグイン実装
Comments for Review: This SpearState will be passed via plugin function. Implementation of plugin

getList(contentTypeId: string, params?: any): Promise<List>;
getContent(contentTypeId: string, contentId: string, params?: any): Promise<Content>;
getContentPreview(contentTypeId: string, contentId: string, previewToken: string): Promise<Content>;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビュー用コメント: 他にも フォーム関係などの関数もありますが、Spear ではフォームを利用しないので定義から外しています。
Comments for Review: sdk-js has form realted function, however Spear doesn't use form API. So I don't defined these function.

@mantaroh mantaroh marked this pull request as ready for review August 28, 2023 20:01
Copy link
Contributor

@qst-exe qst-exe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@yoannes yoannes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mantaroh mantaroh merged commit c7502f2 into main Aug 29, 2023
@mantaroh mantaroh deleted the feature/mark-down-x-support branch August 29, 2023 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants