Writing a Plugin

Plugins can be written in any language that compiles to WASM with an Extism PDK. This page walks through a complete Rust plugin, then summarizes the toolchain for other languages.

Writing a Plugin (Rust)

This example uses Rust.

Prerequisites

# Install the wasm32-wasip1 target
rustup target add wasm32-wasip1

Create the Plugin Project

cargo new --lib my-plugin
cd my-plugin

Cargo.toml

[package]
name = "my-plugin"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
extism-pdk = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

src/lib.rs

use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
struct HookInput {
    content: String,
    page_path: String,
    config: serde_json::Value,
}

#[derive(Serialize)]
struct HookOutput {
    content: String,
    error: Option<String>,
}

#[plugin_fn]
pub fn on_render(input: String) -> FnResult<String> {
    let input: HookInput = serde_json::from_str(&input)?;

    // Transform the HTML content
    let modified = input.content.replace(
        "<pre><code>",
        "<pre class=\"highlighted\"><code>",
    );

    let output = HookOutput {
        content: modified,
        error: None,
    };

    Ok(serde_json::to_string(&output)?)
}

Build

cargo build --target wasm32-wasip1 --release

The compiled module will be at target/wasm32-wasip1/release/my_plugin.wasm.

Install

Copy the built module and create the metadata file:

mkdir -p ~/my-site/site/plugins/my-plugin

# Copy the compiled WASM
cp target/wasm32-wasip1/release/my_plugin.wasm \
   ~/my-site/site/plugins/my-plugin/plugin.wasm

# Create metadata
cat > ~/my-site/site/plugins/my-plugin/plugin.toml << 'EOF'
[plugin]
name = "my-plugin"
version = "0.1.0"
api_version = "0.1.0"
description = "My first Accent CMS plugin"

[hooks]
on_render = true
EOF

Verify

Start the server and check the logs:

accent serve

You should see:

INFO accent: Loaded plugin: my-plugin
INFO accent: Plugin registry loaded: 1 plugin(s)

Writing a Plugin (Other Languages)

Extism provides PDKs for many languages. The JSON protocol is the same regardless of language.

LanguagePDKBuild Target
Rustextism-pdkwasm32-wasip1
Gogo-pdkGOOS=wasip1 GOARCH=wasm
JavaScriptjs-pdkExtism CLI
AssemblyScriptas-pdkasc compiler
Cc-pdkwasm32-wasip1

Refer to the Extism documentation for language-specific guides.