discourse/app/assets/javascripts/admin/addon/components/highlighted-code.gjs
Jarek Radosz 62d00722e1
DEV: Convert highlighted-code to glimmer/gjs (#28914)
the radical change in the implementation doesn't stem from the glimmer migration, but rather the fact that previously the component was single-use – changing any of its args didn't (and couldn't) be reflected because hljs was replacing the nodes so all the ember bookkeeping was gone.

Co-authored-by: David Taylor <david@taylorhq.com>
2024-09-17 13:49:35 +02:00

26 lines
723 B
Plaintext

import Component from "@glimmer/component";
import { service } from "@ember/service";
import { modifier } from "ember-modifier";
import highlightSyntax from "discourse/lib/highlight-syntax";
export default class HighlightedCode extends Component {
@service session;
@service siteSettings;
highlight = modifier(async (element) => {
const code = document.createElement("code");
code.classList.add(`lang-${this.args.lang}`);
code.textContent = this.args.code;
const pre = document.createElement("pre");
pre.appendChild(code);
element.replaceChildren(pre);
await highlightSyntax(pre, this.siteSettings, this.session);
});
<template>
<div {{this.highlight}}></div>
</template>
}