top | item 26572620

Syntax Highlighting in a Webpage Without JavaScript

71 points| tathisit | 5 years ago |fctorial.github.io | reply

42 comments

order
[+] kelnos|5 years ago|reply
It's funny to see us remembering that things can be done on the server. The trend to render everything client-side has always been troubling to me. Such a waste of resources (but it's not your resources, so developers don't care). Sure, things that are truly dynamic or are different for every user is probably usefully done on the client. But a static page with code samples can easily be rendered on the server, with syntax highlighting, and then cached. You only need to do it once, rather than hundreds or thousands of times on every client device.
[+] Zababa|5 years ago|reply
> Such a waste of resources (but it's not your resources, so developers don't care).

I've always wondered if it wasn't intentional. Things running on the client means they don't have to run on the server, and you can save money this way.

[+] apatheticonion|5 years ago|reply
The browser is just a generalised client implementation where the application is streamed and sandboxed. It's similar to a native app written on Kotlin/Swift or desktop application written in C#/wpf.

The same logic could be applied to those client application technologies, too.

[+] genericstorage3|5 years ago|reply
Css will work until a certain point. What do you do when css can't handle?

Give up, do some super hacky unmaintainable css mess, or suck it up and use js...

[+] tantalor|5 years ago|reply
Offloading rendering to CSS is still making the client do some work. Excessive CSS can bloat the page, hurting latency.

If you take this approach to the extreme, you should render to png on the server for display, and provide plain text for accessibility only.

[+] bugfix|5 years ago|reply
What is so special about this? This is the way websites have been doing syntax highlighting before people started using javascript libraries.
[+] mixedCase|5 years ago|reply
There's a increasingly larger amount of web developers who are only "trained in React/Angular/Vue" and are unable to understand how to use or the trade-offs of regular websites using just HTML+CSS with only a little or no JS (that goes for both templated or fully static sites), and a surprising number of them even thinking they are some kind of "legacy technology".

At the very least, this kind of article is informative for that crowd.

[+] amatecha|5 years ago|reply
Right, looks like it's just preprocessing the content with a static site generator to insert markup + CSS classes around the different syntactical pieces. /shrug
[+] SahAssar|5 years ago|reply
...By running the same JS on the server or in a build step.
[+] anitil|5 years ago|reply
Damn, I was hoping to discover some dark css magic that can be used to contruct some sort of parser
[+] zelphirkalt|5 years ago|reply
Although at that point one could also make use of the excellent pygments library from Python, to truly have no JS, if that was the point. It is used from inside many other languages' tools as well.
[+] franciscop|5 years ago|reply
This is not new, but it's nice to spread awareness since it seems everyone loads it in the front-end nowadays and I've noticed that it also avoids the page moving around too much.

In https://documentation.page/ I'm using `markdown-it` with `prismjs` to generate the code snippets (see example on e.g. https://react-test.dev/). It's in fact a default helper, so you don't even need hacks:

    import MarkdownIt from "markdown-it";
    import Prism from "prismjs";

    function highlight(str, lang) {
      if (!lang || !Prism.languages[lang]) return;
      return Prism.highlight(str, Prism.languages[lang], lang);
    }

    const md = new MarkdownIt({ highlight }).use(...
[+] kaeruct|5 years ago|reply
I think the point of this article is the constexpr.js library, not the fact that syntax highlighting on the server-side is possible
[+] viseztrance|5 years ago|reply
Looks like we've gone full circle.
[+] gtsop|5 years ago|reply
What do you mean? Honestly interested
[+] pcr910303|5 years ago|reply
So many HNers pointing out that it was possible since the dawn of the web... yet the point of this article is that you can just add highlighting JS and constexpr.js will automatically optimize it. JS-based highlighters have all sorts of plugins, so it makes sense to reuse the effort.

So basically the script renders the JS-needed website, execute the JS as a build step and spit out the highlighted site. Pretty cool!

[+] random3|5 years ago|reply
There’s a code parsing intrinsic complexity cost. It can be paid in space / memory by pre-rendering on the server or it could be pushed at runtime as JS and potentially as CSS (which I understand is Turing complete) but one way or another the cost will be paid.
[+] madeofpalk|5 years ago|reply
Right. You just do it on the server. Okay?
[+] mrozbarry|5 years ago|reply
There are pros and cons to highlighting on the client vs the server.

If you highlight on the server:

pros: no javascript, can cache

cons: might be a way to inject scripts into everyone's browser, since the client has to trust the html from the server

--

If you write it on the client:

pros: server can't distribute injected scripts, server stores less things

cons: more javascript, every client has to do work

--

All software choices are weighing the pros and cons. Do the thing that lets you work quickly and safely.

[+] pornel|5 years ago|reply
Server-side I use https://lib.rs/syntect, which supports Sublime Text language definitions and themes.

BTW: the markup grows substantially. It's worth having a simplification step. Also the old-school <tt> element is shorter and cooler than <span>.

[+] eyelidlessness|5 years ago|reply
Well I for one wish I’d known about this (specifically constexpr.js) sooner. I like to develop sites with TS, but generally prefer to ship as little of it to the client as possible. There are a few solutions for this but most have some kind of compromise or another. It’s good to see another contender out there!
[+] jvik|5 years ago|reply
I thought our octa core 3 GHz mobile processors would be able to handle a little bit of javascript once in a while.
[+] u801e|5 years ago|reply
vim comes with a 2html vim script file that generates an HTML page that only uses HTML and CSS for syntax highlighting.
[+] xXx_uwu_xXx|5 years ago|reply
Why the prog element? Something wrong with <code>?