Fossil Forum

jan 2 weeks ago

Post: Syntax highlighting code blocks

A while back, someone wrote a guide about how to get syntax highlighting in code blocks. I am unable to find that post/page, so I went on my merry way to try to reproduce it. I'm using highlight.js (I believe the instructions I read previously uses another highlighter, but I don't think it should matter much).

Basically, I can see that the highligher is running, and it's marking the code block has having been highligted, but there's no actual visual highlighting going on. At first I thought it simply wasn't loading the highlight.css properly, but I copied the entire contents of the css and pasted it into the skin's css, but still no luck.

I'm a little bit at a loss here as to what is going wrong. A part of me wonders if the default fossil css is setting all the colors and is treated as having a higher priority?

Anywho, has anyone dabbled with this sort of thing and has some insights? To me, it kind of looks like it should work, but it just doesn't do any actual highlighting. If I inspect the DOM I can see that the highlighter has assigned a bunch of classes to the code block, so the js part seems to be working.

Anyone happen to know where I might have seen the guide that I referencing earlier? I think I read it on these forums, but I'm not 100% certain.

chungy 2 weeks ago

I know it's not the only solution and not the one you asked about, but I've had luck with Prism. You click the download button, customize the distribution to what you need, download the resulting prism.css and prism.js files.

In Fossil's Admin→Skins page, edit a custom skin (you can base on one of the built-in ones), append the contents of the downloaded prism.css file to the end of the CSS for the skin, and for the footer, append the text:

```

```

Upload the prism.js file to the Unversioned Files section of your repository. This can be done on the WebUI, or if your user has the y privilege, you can do it through fossil uv add, fossil uv sync... Z

danield 2 weeks ago

Here is Warren's take in an older thread.

stephan 2 weeks ago

/uv/prism.js

The JS can also be in the source tree, reference in the skin by /doc/ckout/path/to/prism.js. In that context "ckout" resolves to a checkout when you're in a checkout or "tip" if there's no checkout. sqlite.org/wasm does it that way with highlight.js.

chungy 2 weeks ago

Yeah, that'd work, or even an external host if you wanted to.

I guess it depends on how much you view syntax highlighting as a critical feature of the repository. I never have :-)

jan 1 week, 6 days ago

I know it's not the only solution and not the one you asked about, but I've had luck with Prism.

Just to be clear, I'm not wedded to the specifics -- I'm just interested in getting the feature to work, so I'm perfectly willing to switch to Prism :)

The only thing that I'm doing a little differently is that I want to implement this for multiple repos (hosted on the same domain/host), so I've placed (using apache's Alias) the Highlight.js files under /static/{js,css}, and reference them as appropriate in the footer snippet.

I'll try to switch to Prism and see if I get better results.

jan 1 week, 6 days ago

Thank you -- that was exactly the post I was looking for.

My Search Fu is apparently weaker that I thought.

jan 1 week, 6 days ago

I've been poking at this some more, and I think I discovered what the problem is.

As I outlined before, the javascript seems to be working fine. It's running, and I can see that it has rewritten the code block. It's just not actually applying the style.

I tried putting @import url("/static/css/highlight-styles/default.min.css") in the CSS editor. When I do this, and check the Network tab in the Firefox's Web Developer Tools, it never actually tries to fetch the file. I read up on @import and there are some rules about how it must be used at the top of the file, but I figured I'd just paste the file's contents into the CSS editor instead. This also had no effect. After fiddling around with it for a while, it suddenly worked(!), but I was unsure what I had changed to make it work. I removed the highlighting to see if I could recreate it -- but it was still highlighting(!). And then it struck me, that I was probably looking a cached copeies of the style sheet. I held shift and clicked on the reload button, and sure enough now I see the CSS changes.

So afaict, I can't use @import because it needs to be "at the top" of the style sheet, but I can't really control that (from the skin style editor). If I copy and paste the contents of the file, and make sure to hold shift when I reload the page, I can see it highlighting.

Now, assuming I actually want to use the client-accessible /static/css/highlight-styles/default.min.css -- is there a way to accomplish this (without needing fossil source code changes?). I'm kind of willing to commit some mild crimes, like .. loading the css in a script-block if that lets me highlight without stuffing the css in the repo.

Anyone have any ideas?

Also, how bad would it be to have a "css imports" list in the skin editor, which would exapand to `@import 'blah' at the top of the generated css?

jan 1 week, 6 days ago

I tried this:

<script src="/static/js/highlight.js"></script>
<script nonce="$nonce">

  const link = document.createElement("link");
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = "/static/css/highlight-styles/default.min.css";
  document.head.appendChild(link);

  document.addEventListener("DOMContentLoaded", () => {
    hljs.highlightAll();
  });
</script>

This seems to get the job done, so I'll stick with this for now.

stephan 1 week, 6 days ago

Anyone have any ideas?

FWIW: https://sqlite.org/wasm/dir?name=assets/skin

The @import has to be at the of its own sheet, which you can work around like that site (just by accident) does:

<style>
  @import url($<home>/doc/$<docBranch>/assets/fossil-doc.css)
</style>
stephan 1 week, 6 days ago

Anyone have any ideas?

FWIW: https://sqlite.org/wasm/dir?name=assets/skin

The @import has to be at the of its own sheet, which you can work around like that site (just by accident) does in its header.txt:

<style>
  @import url($<home>/doc/$<docBranch>/assets/fossil-doc.css)
</style>
<script src="$<home>/doc/$<docBranch>/assets/highlightjs/highlight-sqlite3.min.js"></script>
spindrift 1 week, 6 days ago

I'm no CSS guru.

But I presume you've tried loading it like a normal style sheet rather than trying to get @import to work...?

And while it's in the wrong place, technically, I'd be very tempted just to put

<link rel="stylesheet"
      href="/static/css/highlight-styles/default.min.css">

into header.txt (or footer if you really want) and see if it works. I can't remember about the CSP but the nonce is available to you here if you wish to use it.

jan 1 week, 6 days ago

The @import has to be at the of its own sheet, [...]

Thank you -- some lines trimmed, and I'm pretty happy with the final results.

jan 1 week, 6 days ago

I thought <link> needed to be in <head>, which afaict is before header. I may be working under several false assumptions here though, because it would be generous to even call me a html/css "beginner". I will experiment more with this.

But I have another question, related to this - and other things mentioned/referenced in the thread - what actually is this header.txt? I presume this is what I'm editing via the skin editor, but is there a way to modify it more directly -- via a checkout of sorts?

The sqlite/wasm repo link posted earlier by @stephan has these files as .txt. Are these the raw in-use skin files?

stephan 1 week, 6 days ago

what actually is this header.txt?

explains this in detail. In short, those files get assembled in a certain order, together with common scaffolding bits, to create the main page layout.|

jan 1 week, 6 days ago

Oh, thank you -- that page explains .. well, everything. :)

spindrift 1 week, 6 days ago

I thought <link> needed to be in <head>

I actually thought the same, while expecting that browsers are usually permissive enough to make it work anyway.

However, I've looked up the spec for educational reasons and a stylesheet link is actually explicitly a "body-safe" link flavour.

It's not the "right" place, but apparently it's fine.

https://html.spec.whatwg.org

anonymous 6 days, 3 hours ago

If you want something minimal, there is microlight. It is based on a static set of regexes for punctuation and keywords, good enough for a handful or languages.

I ported it to Ruby for my static blog to highlight source code without any Javascript, just static HTML and CSS:

https://codeberg.org/mneumann/hacksigen/src/branch/main/hacksigen.rb#L195

It shouldn't be too hard to port to C. Just in case, you want a no-JS solution :)

For larger code blocks, it might bloat the HTML code a bit, though.

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button