No internet connection
  1. Home
  2. Support

Strange problem with Prism.js syntax highlighting

By Mateusz Bodnar @vimrh
    2018-06-24 13:21:39.333Z

    Hey, since talkyard doesn't support native syntax highlighting I'm trying to set it up with Prism.js. However it works only partially, some code has syntax highlighting, some don't. Attached screenshot shows the problem.
    I used "Look and feel"/"CSS and JS" in admin settings, pasted Prism's CSS code into stylesheet box ( https://cdnjs.cloudflare.com/ajax/libs/prism/0.0.1/prism.min.css ) and JS code into Javascript box ( https://cdnjs.cloudflare.com/ajax/libs/prism/0.0.1/prism.min.js )
    PS I'm working on Polish translation :)

    Solved in post #2, click to view
    • 2 replies
    1. KajMagnus @KajMagnus2018-06-25 04:20:24.586Z2018-08-20 11:49:18.655Z

      Hi Mateusz! You forgot the screenshot? :- )

      Anyway I gave Prism.js a try, by pasting the content of the css and js you linked, into the fields here: http://my-test-site.localhost/-/admin/customize/css-js

      Thereafter, in the editor, the preview didn't work, and after saving a new post, the code didn't get highlighted — however, after page reload, the code got highlighted properly. So it didn't work when editing & saving, but did work when opening new pages.

      The reason for this is that PrismJS only looks for <code> tags on page load — but not dynamically, whilst one types text in the editor so the preview gets updated, and when a post gets saved and parts of the html page gets updated dynamically.

      There's a javascript hook you can use, to re-run PrismJS inside the editor preview, + after a post has been saved. Copy-paste this after the PrismJS javascript: (into https://your-site.com/-/admin/customize/css-js, the Javascript editor)

      talkyard.postElemPostProcessor = function(elemId) {
        var selector = '#' + elemId + ' code';
        console.log('Telling PrismJS to post process: ' + selector);
        var codeElems = document.querySelectorAll(selector);
        for (var i = 0; i < codeElems.length; ++i) {
          var elem = codeElems[i];
          Prism.highlightElement(elem);
        }
      };
      

      I tested locally, and now PrismJS works everywhere, I mean, on page load, and in the editor preview, & when editing or saving a new post.

      (That function, i.e. talkyard.postElemPostProcessor, gets called whenever the content of a post, or the editor preview, changes, and it's passed the id of the relevant element. I added it a while ago, because it was needed for processing LaTeX with Mathjax, in the preview & after saving a post, like with PrismJS.)

      ***

      Ok looking forward to adding Polish :- )

      ReplySolution
      1. VMateusz Bodnar @vimrh
          2018-06-25 07:06:49.808Z

          Thank you! It works! However now, I have another problem (I will write about it in a separate topic) ;) Unfortunately I'm not a programmer :(
          And yes, I forgot the screenshot ;)