Skip to content

Extensions

Thorsten Marx ㋡ edited this page Dec 26, 2023 · 9 revisions

Extension are writen in good old JavaScript.

Put your extension into the host/extension folder. All files with the nameing convention name.extension.js are loaded ad system startup.

Add custom http endpoint

Attention: Keep in mind, that all http endpoint extensions are loaded unter the endpoint /extensions. What that means is, you can not put content under this url name.

import { UTF_8 } from 'system/charsets.mjs';
import { $http } from 'system/http.mjs';

$http.get("/test", (request, response) => {
	response.addHeader("Content-Type", "text/html; charset=utf-8")
	response.write("ich bin einen test extension!öäü", UTF_8)
})

The endpoint is available at http://your_host:your_port/extensions/test

Modules

To structure your extension code, you can create modules. But keep in mind, your modules must use the .mjs extension. Otherwise our js engine will not load your modules correctly.

System modules

cms-server comes with some system modules. All system modules are in the systems package, so you can not use system as folder name for custom modules.

http

import { header } from 'system/http.mjs';

exchange.getResponseHeaders().add(header("Content-Type"), "text/html; charset=utf-8");

charsets

import { UTF_8, UTF_16, ISO_88591 } from 'system/charsets.mjs';

exchange.getResponseSender().send("I'm a test extension!", UTF_8);

logging

import { getLogger } from 'system/logging.mjs';
const logger = getLogger("extensions");
logger.debug("debug log from test extension");

template

import { $template } from 'system/template.mjs';

$template.registerTemplateSupplier(
	"myName",
	() => "Thorsten"
)

$template.registerTemplateFunction(
	"getHello",
	(name) => "Hello " + name + "!"
)

Use template extensions in template

<div th:with="name = ${myName.get()}">
	<p th:th:text="${name}"></p>
	<!-- Thorsten -->
</div>
<div th:with="hello = ${getHello.apply('Thorsten')}">
	<p th:text="${hello}"></p>
	<!-- Hello Thorsten -->
</div>

files

import { $files } from 'system/files.mjs';

let content = $files.readContent("extras/products.json")

shortcodes

With #65 we added support for shortcode.

$shortcodes.register(
	"youtube",
	(params) => `<iframe width="${params.getOrDefault("width", "560")}" height="${params.getOrDefault("height", "315")}" src="https://www.youtube.com/embed/${params.getOrDefault("video", "")}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>`
)

Usage in markdown

[[youtube video='C0DPdy98e4c'/]]
conventions

Shortcodes name: only valid characters are a-z, A-Z, 0-9 and underscore

Multiple attributes uses the , as delimiter, e.q.

[[youtube video='C0DPdy98e4c',width='256',height='512'/]]

Shortcodes can be in short and log format. The youtube example above is using the short format. If your shortcode should work with complex text input, which is impossible to handle with attribute, you can use the long form. The content between opening and closing tag is accessible in params with the key content.

$shortcodes.register(
	"mark",
	(params) => `<mark>${params.getOrDefault("content", "")}</mark>`
)
[[mark]]Here is the content[[/mark]]
Clone this wiki locally