Skip to content

Commit

Permalink
Add hljs class to all code blocks
Browse files Browse the repository at this point in the history
This adds hljs to all code blocks for two reasons:

 - nohighlight blocks are left untouched by HighlightJS, meaning that
   the hljs class isn't attached at runtime to those blocks. Adding this
   explicitly makes sure that CSS rules for code blocks still apply
   such that the correct "regular text color" is used.
 - If JavaScript is disabled in the browser this makes sure that at
   least the intended "regular text color" is used for code blocks.
  • Loading branch information
fredrikekre committed Jul 16, 2021
1 parent 797a96f commit 5d6e5e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

* ![Enhancement][badge-enhancement] The `julia>` prompt is now colored in green in the `julia-repl` language highlighting. ([#1639][github-1639], [#1641][github-1641])

* ![Enhancement][badge-enhancement] The `.hljs` CSS class is now added to all code blocks to make sure that the correct text color is used for non-highlighted code blocks and if JavaScript is disabled. ([#1645][github-1645])

* ![Enhancement][badge-enhancement] The sandbox module used for evaluating `@repl` and `@example` blocks is now removed (replaced with `Main`) in text output. ([#1633][github-1633])

* ![Enhancement][badge-enhancement] `@repl`, `@example`, and `@eval` blocks now have `LineNumberNodes` inserted such that e.g. `@__FILE__` and `@__LINE__` give better output and not just `"none"` for the file and `1` for the line. This requires Julia 1.6 or higher (no change on earlier Julia versions). ([#1634][github-1634])
Expand Down Expand Up @@ -879,6 +881,7 @@
[github-1634]: https://github.com/JuliaDocs/Documenter.jl/pull/1634
[github-1639]: https://github.com/JuliaDocs/Documenter.jl/issues/1639
[github-1641]: https://github.com/JuliaDocs/Documenter.jl/pull/1641
[github-1645]: https://github.com/JuliaDocs/Documenter.jl/pull/1645

[julia-38079]: https://github.com/JuliaLang/julia/issues/38079
[julia-39841]: https://github.com/JuliaLang/julia/pull/39841
Expand Down
8 changes: 4 additions & 4 deletions src/Writers/HTMLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1762,15 +1762,15 @@ mdconvert(b::Markdown.Bold, parent; kwargs...) = Tag(:strong)(mdconvert(b.text,
function mdconvert(c::Markdown.Code, parent::MDBlockContext; settings::Union{HTML,Nothing}=nothing, kwargs...)
@tags pre code
language = Utilities.codelang(c.language)
class = isempty(language) ? "nohighlight" : "language-$(language)"
if language == "documenter-ansi" # From @repl blocks (through MultiCodeBlock)
return pre(domify_ansicoloredtext(c.code, "nohighlight"))
return pre(domify_ansicoloredtext(c.code, "nohighlight hljs"))
elseif settings !== nothing && settings.prerender &&
!(isempty(language) || language == "nohighlight")
r = hljs_prerender(c, settings)
r !== nothing && return r
end
return pre(code[".$class"](c.code))
class = isempty(language) ? "nohighlight" : "language-$(language)"
return pre(code[".$(class) .hljs"](c.code))
end
function mdconvert(mcb::Documents.MultiCodeBlock, parent::MDBlockContext; kwargs...)
@tags pre br
Expand Down Expand Up @@ -2005,7 +2005,7 @@ function mdconvert(d::Dict{MIME,Any}, parent; kwargs...)
elseif haskey(d, MIME"text/plain"())
@tags pre
text = d[MIME"text/plain"()]
return pre[".documenter-example-output"](domify_ansicoloredtext(text, "nohighlight"))
return pre[".documenter-example-output"](domify_ansicoloredtext(text, "nohighlight hljs"))
else
error("this should never happen.")
end
Expand Down
18 changes: 9 additions & 9 deletions test/prerender/prerender.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ assets = read_assets()
@test occursin("'highlight-llvm'", assets)
@test occursin("/languages/llvm.min", assets)
index = read_index()
@test occursin("<code class=\"language-julia\">function f()", index)
@test occursin("<code class=\"language-julia-repl\">julia&gt; function f()", index)
@test occursin("<code class=\"language-llvm\">; @ int.jl:87 within", index)
@test occursin("<code class=\"language-julia hljs\">function f()", index)
@test occursin("<code class=\"language-julia-repl hljs\">julia&gt; function f()", index)
@test occursin("<code class=\"language-llvm hljs\">; @ int.jl:87 within", index)

# With prerender
HLJSFILES = Documenter.Writers.HTMLWriter.HLJSFILES
Expand All @@ -51,9 +51,9 @@ for _ in 1:2 # test with and without highlightjs file given
@test !occursin("'highlight-llvm'", assets)
@test !occursin("/languages/llvm.min", assets)
local index = read_index()
@test !occursin("<code class=\"language-julia\">function f()", index)
@test !occursin("<code class=\"language-julia-repl\">julia&gt; function f()", index)
@test !occursin("<code class=\"language-llvm\">; @ int.jl:87 within", index)
@test !occursin("<code class=\"language-julia hljs\">function f()", index)
@test !occursin("<code class=\"language-julia-repl hljs\">julia&gt; function f()", index)
@test !occursin("<code class=\"language-llvm hljs\">; @ int.jl:87 within", index)
@test occursin("<code class=\"language-julia hljs\"><span class=\"hljs-keyword\">function</span> f()", index)
@test occursin("<code class=\"language-julia-repl hljs\"><span class=\"hljs-meta\">julia&gt;</span>", index)
@test occursin("<code class=\"language-llvm hljs\"><span class=\"hljs-comment\">; @ int.jl:87", index)
Expand All @@ -78,9 +78,9 @@ assets = read_assets()
@test !occursin("'highlight-llvm'", assets)
@test !occursin("/languages/llvm.min", assets)
index = read_index()
@test !occursin("<code class=\"language-julia\">function f()", index)
@test !occursin("<code class=\"language-julia-repl\">julia&gt; function f()", index)
@test occursin("<code class=\"language-llvm\">; @ int.jl:87 within", index)
@test !occursin("<code class=\"language-julia hljs\">function f()", index)
@test !occursin("<code class=\"language-julia-repl hljs\">julia&gt; function f()", index)
@test occursin("<code class=\"language-llvm hljs\">; @ int.jl:87 within", index)
@test occursin("<code class=\"language-julia hljs\"><span class=\"hljs-keyword\">function</span> f()", index)
@test occursin("<code class=\"language-julia-repl hljs\"><span class=\"hljs-meta\">julia&gt;</span>", index)
@test !occursin("<code class=\"language-llvm hljs\"><span class=\"hljs-comment\">; @ int.jl:87", index)
Expand Down

0 comments on commit 5d6e5e9

Please sign in to comment.