Codeblock Highlighter for "pseudocode" (alias or proxy to "java")

Hello,

I have an article with code snippet in “pseudocode”, like this:

```pseudocode
// Product interface
interface Document {
    method open()
    method close()
    method save()
}
```

I want it to be itercepted and internally implemented via Chrome Java Lexer, temporarily, until I find solution (such as creating custom lexer).

Unfortunately, we do not have aliases option in configuration, like this:

markup:
  goldmark:
    renderer:
      unsafe: true
  highlight:
    noClasses: false
    lineNos: true
    lineNumbersInTable: false
    guessSyntax: false
    aliases: 
      pseudocode: java

Would be nice to add “aliases” feature…

Another option is to intercept it like this:

layouts
  _default
    _markup

```
{{- /* Custom render hook for code blocks */ -}}

{{- $language := .Language | default "plaintext" -}}

{{- if eq $language "pseudocode" -}}

{{- $language = "java" -}}

{{- end -}}

<pre><code class="language-{{ $language | urlize }}">

{{- .Inner | htmlEscape | safeHTML }}

</code></pre>
```

But it didn’t work…

I hope I don’t have XYProblem here :wink: anyway, I have text files with ````pseudocode` in it, which I want to map internally to Java highlighter (for example). Thanks,

Create a code block render hook.

1 Like

Your render hook should look something like this:

{{ $lang := or .Type "text" }}
{{ if eq .Type "pseudocode" }}
  {{ $lang = "java" }}
{{ end }}
{{ transform.Highlight .Inner $lang  }}

Thank you it works! I also found this docs: Code block render hooks | Hugo

The only thing I was hoping on right top corner of rendered code shippet I’d still see PSEUDOCODE (uppercase) instead of JAVA; but probably it is job of Chromium and so I still need to create custom lexer for pseudocode. Sometime in a future.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.