Syntax highlighting here on discourse

Hello

I’m trying to implement better SC support on discourse.flucoma.org and I found this:

sexy. but now, I went on meta.discourse and here and claude.ai and nothing works in term of trying to get that thing working :smiley:

Help?

reading a bit more, it feels like one has to copy/paste some code. I have tried for a few hours to find the right combination to no avail…

the I tried the following and it seems it is not working here either!

x = {arg freq = 100; SinOsc.ar(freq,amp:-23.dbamp)}.play;

If one of the discourse maintainer is reading this, let me know if you’ve been more successful than me. If I make it work, I’ll let you know.

Does anyone of the @admins know about this?

IIRC - the implementation by moss is currently broken b/c of some highlight-js upgrade by discourse.
I fixed it 2 years ago through a fork (see GitHub - capital-G/highlightjs-supercollider · GitHub) and contacted the mods about it, but got no response, but maybe we cant ping the @moderators again?

One workaround I can recommend is to use smalltalk as highlighting language, e.g.

x =  {arg freq = 100; SinOsc.ar(freq,amp:-23.dbamp)}.play;
1 Like

indeed that would be great if that was sorted. @dscheiba I’ll try your version on discourse.flucoma.org and will report back here so @moderators and @admins know if that works for me too :slight_smile:

1 Like

ok a bit of claude time and i got it to work in discourse. thanks for the fix. if anyone wants it here, I can share the how-to.

1 Like

pinging @ioif who can maybe fix it for this forum :slight_smile:

1 Like

I have to say it is quite sime, thanks to your update. it can be done within the discourse admin window. I can copy paste what is needed where :slight_smile:

1 Like

Maybe you can just share it publicly here, so other discourse communities also get to know how they can get SC syntax highlighting support :slight_smile:

ok here is an attempt to explain it in plain English:

  1. an admin has to make a new component by going in /admin/config/customize/components and press ‘install’ on the right, then in the pop up, + create new.
  2. name it something relevant, like “hightlight-sclang” and include it in the current used theme.
  3. click ‘edit custom code’ and in the tab ‘JS’ replace by the code below, which is mostly stolen from your repo. I took the complete verbose one to make it simpler to edit.
  4. save and exit this editor
  5. go to /admin/site_settings/category/all_results?filter=highlighted_languages
  6. click the + sign beside the list of languages
  7. type ‘sclang’ - do NOT expect autocompletion, it won’t happen.
  8. test (by making a new post, with a code block preceded by ```sclang)

it works here.

the code to copy:

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  const sclangLang = function (hljs) {
  const KEYWORDS = {
    keyword: "arg classvar|10 const super this var|2",
    built_in:
      "false inf|2 nil|2 true thisFunction|10 thisFunctionDef|10 thisMethod|10 " +
      "thisProcess|10 thisThread|10 currentEnvironment|10 topEnvironment|10",
  };

  const CLASS = {
    className: "type",
    begin: "\\b[A-Z]\\w*\\b",
    relevance: 0,
  };
  const PRIMITIVE = {
    className: "meta",
    begin: "_\\w+",
    relevance: 0,
  };
  const CHAR_LITERAL = {
    className: "literal",
    begin: "\\$\\\\?.",
  };
  const ENV_VAR = {
    className: "title",
    begin: "~\\w+",
    relevance: 2, // ~env vars are pretty common in SC
  };

  const NUMBER_RADIX_RE = "\\b\\d+r[0-9a-zA-Z]*(\\.[0-9A-Z]*)?\\b";
  const NUMBER_FLOAT_RE = "\\b((\\d+(\\.\\d+)?([eE][-+]?\\d+)?(pi)?)|pi)\\b";
  const NUMBER_INT_RE = "\\b0x[a-fA-F0-9]+\\b";
  const NUMBER_SCALE_RE = /\b\d+(s+|b+|[sb]\d+)\b/;
  const NUMBER = {
    className: "number",
    variants: [
      { begin: NUMBER_RADIX_RE },
      { begin: NUMBER_SCALE_RE },
      { begin: NUMBER_FLOAT_RE },
      { begin: NUMBER_INT_RE },
    ],
    relevance: 0,
  };

  const BLOCK_COMMENT = hljs.COMMENT(
    '/\\*', '\\*/',
    { contains: [ hljs.C_BLOCK_COMMENT_MODE ] }
  );

  return {
    aliases: ["supercollider", "sc"],
    name: "sclang",
    keywords: KEYWORDS,
    contains: [
      {
        className: "type",
        // relevance boost for common but sc-specific class names: Synth, SynthDef
        begin: /\b(Synth|SynthDef)\b/,
        relevance: 10,
      },
      CLASS,
      PRIMITIVE,
      CHAR_LITERAL,
      ENV_VAR,
      NUMBER,
      {
        className: "string",
        variants: [
          { begin: "\\\\\\w+", relevance: 5 }, // backslash syms are pretty common
          hljs.APOS_STRING_MODE,
          { begin: "[A-Za-z_]\\w*\\:", relevance: 0 },
        ],
      },

      hljs.QUOTE_STRING_MODE,
      hljs.C_LINE_COMMENT_MODE,
        BLOCK_COMMENT
    ],
    // avoid common confusions: we don't declare classes with "class Aaa"
    illegal: /\bclass\s+[A-Z]/,
  };
};
  api.registerHighlightJSLanguage("sclang", sclangLang);
});
1 Like

I like this idea, don’t know why it hasn’t been done already?!

Just posting some easy fixes/ along with some stuff that might be impossible to fix.

The int hexadecimal actually accepts any digit at the start 999x123 == 0x123

The multiline comment mode is probably wrong, in c you can’t have nested comments.

\* \* *\ that closes a c comment, but doesn’t in SC.

You can have any white space in env vars ~ foo.

All the radixs are impossible to write in regex because they only match valid radixs - this seems like a fine compromise.

I’m unclear about how the string regex works, but it should accept white space too

"

"

maybe I should have written a comment in the js code to give @dscheiba credits. I’ll do that on FluCoMa.

as for the fixes, maybe we could try them here with a mod or an admin. if we get no replies, I’ll try them on temp posts on flucoma and delete after :slight_smile:

Maybe you should link the repo instead of giving me credit, allows to centralize efforts in the future :wink: I’ll try to add your text into the readme if this is fine w/ you.

1 Like

Will do. BTW I tried but cannot load dynamically, but failed after hours of claude-aided attempts…

will put the link