DocumenterCodeBlocks.jl announcement

I want to to announce DocumenterCodeBlocks.jl, a Documenter.jl plugin that makes the code blocks in your package documentation quite a bit more interesting. To enable it the only thing you have to change is to pass the CodeBlocks() to Documenter.makedocs:

using Documenter, DocumenterCodeBlocks

makedocs(
    # ...
    plugins = [CodeBlocks()],
)

I think that the defaults are good, but some things can be configured by passing keyword arguments to the constructor, see the documentation.

The best way to get a feel for it is the documentation itself. Every code block there is rendered by the plugin, so hover, click, and select away. Here is what you get:

Reference links and hover tooltips

Identifiers in code blocks that name a documented object become links to their docstring. Resolution is call-arity aware: foo(1, 2) links to the foo(a, b) method documentation, not just to “some docstring for foo”. Every link also gets a doxygen-style hover tooltip with the target’s signature and a one-line summary:

tooltips

Links only attach where the syntax vouches for the meaning (call callees like add_numbers(...) and type positions like m::MyType) and names that don’t resolve are silently left alone. For ambiguous references, e.g. a splatted call measure(args...) that could hit several methods, the tooltip shows the “arity-pruned” candidate list instead, as seen at the end of the clip above.

Line numbers and linkable lines

Every code block gets GitHub-style line numbers with a content-addressed permalink. Clicking the gutter selects a line, shift-click or drag selects a range, and the selection is reflected in the URL as a stable fragment like #c-1a2b3c4d-L5-L11. Selections survive reload and scroll into view on page load.

linenumbers

The copy code block button is unaffected, the gutter numbers never end up in your clipboard.

Build-time syntax highlighting with JuliaSyntax

Julia code blocks are highlighted at build time using JuliaSyntax.jl instead of highlight.js’s regex approximation. This means correct handling of the tricky cases (nested string interpolation, command literals, type parameters, word operators, etc) and more granularity. Works for julia, julia-repl, jldoctest, and executed @repl blocks (including their ANSI-colored output):

The screenshot is using the catppuccin-mocha theme but all six default Documenter themes are supported.

Docstring-quality warnings

The tooltips are only as good as the docstrings they summarize, so the plugin can optionally warn when a docstring is missing a leading signature block or a short first sentence.

Installation

julia> import Pkg

julia> Pkg.add("DocumenterCodeBlocks")

Status and caveats

The plugin necessarily builds on some Documenter internals beyond the documented plugin API so there might have to be updates to this package for new Documenter releases. If you maintain docs for a package, I’d love for you to try it out and report anything that breaks. Feedcback, issues and PRs are very welcome!

Thanks!

This is great - thanks! Even my code looks pretty good now!

I noticed that nearly every function call is underlined. It’s almost becoming visually distracting…

I think a subtler visual style would work just as well - perhaps it could be arranged such that the underlining is less noticeable, and/or activated only when the focus is in the code-block? Or make the underlining get brighter as your pointer gets nearer? :slight_smile:

Yea maybe just a different color for links would be better.

Thanks for making this improvement happen so quickly!

Yea, toned it down a bit in the new release. Thanks for the feedback!

Does the package play together with DocumenterInterlinks.jl ? Or is it possible to specify custom links for some methods from other packages?

I’m trying it whilst using DocumenterVitepress.jl but it doesn’t seem supported :(. Would it be a matter for DocumenterVitepress.jl or DocumenterCodeBlocks.jl?

probably DocumenterVitepress :smiley: , and possible and small caller extension will be needed. Open to PRs :+1:

And oh!! nice we have this in Julia now, I always wanted to have something similar to twoslash Syntax Highlighting with Twoslash | Twoslash, now is here :heart_exclamation:

It is probably neither. DocumenterCodeBlocks modifies the generated HTML and that probably looks completely different in the Vitepress output.

This is fantastic!!! I’m going to add it to the JuMP documentation, which should give it a very good thrashing: [docs] add DocumenterCodeBlocks.jl - Pull Request #4216 - jump-dev/JuMP.jl - GitHub

I have a couple of questions, but I’ll open an issue for them.