Fuma Content

Plugins

Introduction to plugins.

Creating Plugin

You can create a plugin with:

import type { Plugin } from "fuma-content";

export default function myPlugin(): Plugin {
  return {
    name: "custom:my-plugin",
    config() {
      const { cwd } = this.core.getOptions();
      console.log(cwd);
    },
    // you can define different hooks
    async collection(collection) {
      console.log(collection);
    },
  };
}

Hook Order

When triggering event on multiple plugins, event hooks are called according to its order in plugins array. Order is strictly preserved.

Plugin Hook

Plugins can attach custom data & hooks to collections using a Plugin Hook:

import { defineCollectionHook } from "fuma-content/collections";
import { asyncHook, type AsyncHook } from "fuma-content";

export interface MyHook {
  /**
   * example for defining hook to listen to events.
   */
  onCustomEvent: AsyncHook<{ content: string }>;
}

export const myHook = defineCollectionHook<MyHook>(() => ({
  // initial value
  onCustomEvent: asyncHook(),
}));

It can be accessed or attached from collections:

// initialize & access hook value
const hook = collection.pluginHook(myHook);

// call hooks
await hook.onCustomEvent.run({ content: "test" });

On this page