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.
> 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.
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.
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.
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
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.
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(...
In Go we have https://github.com/alecthomas/chroma, and I implemented the lexer that the Caddy docs use for syntax highlighting the Caddyfile (see https://caddyserver.com/docs/caddyfile/concepts for example). What's cool about this is Caddy is serving the site from markdown files rendered via Go templates, with Chroma for code blocks. So Caddy can highlight its own config.
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!
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.
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!
[+] [-] kelnos|5 years ago|reply
[+] [-] Zababa|5 years ago|reply
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 same logic could be applied to those client application technologies, too.
[+] [-] genericstorage3|5 years ago|reply
Give up, do some super hacky unmaintainable css mess, or suck it up and use js...
[+] [-] canadianfella|5 years ago|reply
[deleted]
[+] [-] tantalor|5 years ago|reply
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
[+] [-] mixedCase|5 years ago|reply
At the very least, this kind of article is informative for that crowd.
[+] [-] amatecha|5 years ago|reply
[+] [-] SahAssar|5 years ago|reply
[+] [-] anitil|5 years ago|reply
[+] [-] zelphirkalt|5 years ago|reply
[+] [-] franciscop|5 years ago|reply
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:
[+] [-] kaeruct|5 years ago|reply
[+] [-] slow_donkey|5 years ago|reply
[+] [-] viseztrance|5 years ago|reply
[+] [-] gtsop|5 years ago|reply
[+] [-] francislavoie|5 years ago|reply
[+] [-] pcr910303|5 years ago|reply
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
[+] [-] madeofpalk|5 years ago|reply
[+] [-] mrozbarry|5 years ago|reply
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
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
[+] [-] sedachv|5 years ago|reply
[+] [-] jvik|5 years ago|reply
[+] [-] u801e|5 years ago|reply
[+] [-] xXx_uwu_xXx|5 years ago|reply