Files
authentik/website/remark/support-directive.mjs
Teffen Ellis 614740a4ff web/NPM Workspaces: Prep ESBuild plugin for publish. (#14552)
* web: Prep ESBuild plugin for publish.

* prettier-config: Update deps.

* eslint-config: Update deps.

* docusaurus-config: Update deps.

* docs: Update deps.

* docs: Enable linter.

* docs: Lint.

* web/sfe: Clean up types. Prep for monorepo.

* esbuild-plugin-live-reload: Update deps.

* web: Tidy ESLint, script commands.

* web: Fix logs.

* web: Lint.

* web: Split compile check from cached version.
2025-05-21 16:09:33 -04:00

87 lines
2.3 KiB
JavaScript

/**
* @file Remark plugin to transform `ak-support` directives into support level badges.
*
* @import { Root } from "mdast";
*/
import { h } from "hastscript";
import { SKIP, visit } from "unist-util-visit";
/**
* Support levels for authentik.
* @typedef {"authentik" | "community" | "vendor" | "deprecated"} SupportLevel
*/
/**
* Mapping of support levels to badge classes.
*
* @satisfies {Record<SupportLevel, string>}
*/
export const SupportLevelToLabel = /** @type {const} */ ({
authentik: "authentik",
community: "Community",
vendor: "Vendor",
deprecated: "Deprecated",
});
/**
* Type-predicate to determine if a string is a known support level.
*
* @param {string} input The string to check.
* @return {input is SupportLevel} True if the string is a known support level.
*/
export function isSupportLevel(input) {
return Object.hasOwn(SupportLevelToLabel, input);
}
/**
* MDAST plugin to transform `ak-support` directives into preview badges.
*/
function remarkSupportDirective() {
/**
* @param {Root} tree The MDAST tree to transform.
*/
return (tree) => {
visit(tree, "textDirective", (node) => {
if (node.name !== "ak-support") return SKIP;
const firstChild = node.children[0];
if (firstChild?.type !== "text") return SKIP;
const level = firstChild.value.trim();
if (!isSupportLevel(level)) {
throw new TypeError(`Invalid support level: ${level}`);
}
const label = SupportLevelToLabel[level];
const data = node.data || (node.data = {});
const hast = h("span", {
...node.attributes,
"className": `badge badge--support-${level}`,
"title": `This feature is supported at the ${label} level.`,
"aria-description": "Support level badge",
"role": "img",
});
data.hName = hast.tagName;
data.hProperties = hast.properties;
data.hChildren = [
{
type: "text",
value: `Support level: ${label}`,
},
];
node.children = [];
return SKIP;
});
};
}
export default remarkSupportDirective;