import { type Directive, Phase } from "@mizu/internal/engine"
import mapping from "./mapping.json" with { type: "json" }
export type * from "@mizu/internal/engine"
export const typings = {
modifiers: {
trim: { type: Boolean, enforce: true },
},
} as const
export const _code = {
name: "*code",
phase: Phase.CONTENT,
typings,
default: "this.textContent",
async execute(renderer, element, { attributes: [attribute], ...options }) {
if (!renderer.isHtmlElement(element)) {
return
}
const parsed = renderer.parseAttribute(attribute, this.typings, { modifiers: true })
const language = (mapping as Record<PropertyKey, string>)[parsed.tag] ?? "plaintext"
const { hljs } = await import("./import/highlight.js/core.ts")
if (!hljs.getLanguage(language)) {
const { syntax } = await import(`./import/highlight.js/languages/${language}.ts`)
hljs.registerLanguage(language, syntax)
}
let code = `${await renderer.evaluate(element, attribute.value || this.default, options)}`
if (parsed.modifiers.trim) {
const trim = code.match(/^[ \t]*\S/m)?.[0].match(/\S/)?.index ?? 0
code = code.replaceAll(new RegExp(`^[ \\t]{${trim}}`, "gm"), "")
}
code = hljs.highlight(code, { language }).value
if (parsed.modifiers.trim) {
code = code.trim()
}
element.innerHTML = code
if ((parsed.modifiers.trim) && (element.parentElement?.tagName === "PRE")) {
element.parentElement.innerHTML = element.parentElement.innerHTML.trim()
}
},
} as Directive<null, typeof typings> & { default: NonNullable<Directive["default"]> }
export default _code
|