Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlight at build time #184

Open
toastal opened this issue Oct 21, 2022 · 5 comments
Open

Syntax highlight at build time #184

toastal opened this issue Oct 21, 2022 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@toastal
Copy link

toastal commented Oct 21, 2022

The use of highlight.js has some issues:

  1. a user with JavaScript disabled by default for security reasons, will not get syntax highlighting
  2. a user with must trust a third-party CDN for delivery
    a. with browser sandboxing, there is no performance benefit from using a CDN in this way
    b. the CDN will leak private user data such as IPs to an entity that may not act in good faith with that data
    c. subresource-integrity check is also missing so this code could be insecure and changed by the CDN at will because it's not within Gleam's control
    d. technically since these URLs lack the protocol, they could be served over the insecure HTTP vs. HTTPS+TLS
  3. a user must execute the same syntax highlight parsing code as all other users
    a. wasteful to CPU + battery
    b. wasteful on the network (and user data plans)
    c. introducing a rendering and paint delay

My suggestion is to render syntax highlighting at build time and include the syntax highlighting CSS into the base style sheet. This would mean the syntax highlighting step would be executed once and it would be static. Built static, this eliminates the need for JavaScript for the basic documentation, does not require consenting to third-party CDN and security risks involved with it, saves people's CPUs+batteries+data plans, and would increase render performance.

@lpil
Copy link
Member

lpil commented Oct 21, 2022

Hello!

Sounds good. Do you have any ideas for how we would do this?

@lpil lpil added the help wanted Extra attention is needed label Oct 21, 2022
@toastal
Copy link
Author

toastal commented Oct 22, 2022

A quick phase 1 could be hosting the highlight.js files on the project's domain so that it's first-party code and can be trusted to a greater degree.

A phase 2... well, peeking at the source, it appears mdbook is used and it doesn't do this rendering server side--meaning all downstream projects have this issue (which I didn't realize when I opened this). Route 1 would be getting mdbook to get this feature, or route 2 use a another tool to parse and add syntax highlighting (anecdote: to fix rendering on SourceHut, I used a script with OCaml + Lambda Soup to pipe to a highlighter and post-process the output).

@lpil
Copy link
Member

lpil commented Oct 22, 2022

We intend to stop using mdbook in future, just not something we've got around to yet.

@Enderchief
Copy link

For syntax highlighting at build time, the syntect library (written in Rust) could work. Deno Doc uses it for documentation. (See this package for an example: https://deno.land/x/[email protected]/application.ts)

@lpil
Copy link
Member

lpil commented Mar 8, 2024

Another option would be to teach the compiler how to print with colours, seeing as it already knows the Gleam grammar. Avoid a large-ish dep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants