Skip to content

Commit

Permalink
Highlight julia-repl code in Markdown specially (JuliaLang#54423)
Browse files Browse the repository at this point in the history
Fixes JuliaLang#54399 by re-introducing the code seperated out from the styled
Markdown PR at Jameson's request
(JuliaLang#51928 (comment)).

The code itself is modelled after [equivalent code in
OhMyREPL](https://github.com/KristofferC/OhMyREPL.jl/blob/b0071f5ee785a81ca1e69a561586ff270b4dc2bb/src/MarkdownHighlighter.jl#L15-L31).

The new `markdown_julia_prompt` face allows people to make the "prompt"
shown in Markdown code visually distinct, to [avoid confusing it with
the REPL prompt at a
glance](KristofferC/OhMyREPL.jl#100). By way
of example, I make it italic by augmenting my `faces.toml` with

```toml
[markdown]
julia_prompt = { italic = true }
```
  • Loading branch information
tecosaur authored and lazarusA committed Jul 12, 2024
1 parent 10d04b1 commit 02895d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions stdlib/Markdown/src/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const MARKDOWN_FACES = [
:markdown_h6 => Face(height=1.05, inherit=:markdown_header),
:markdown_admonition => Face(weight=:bold),
:markdown_code => Face(inherit=:code),
:markdown_julia_prompt => Face(inherit=:repl_prompt_julia),
:markdown_footnote => Face(inherit=:bright_yellow),
:markdown_hrule => Face(inherit=:shadow),
:markdown_inlinecode => Face(inherit=:markdown_code),
Expand Down
17 changes: 16 additions & 1 deletion stdlib/Markdown/src/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,23 @@ function term(io::AnnotIO, md::Header{l}, columns) where l
end

function term(io::IO, md::Code, columns)
code = if md.language ("", "julia", "julia-repl", "jldoctest")
code = if md.language ("", "julia")
highlight(md.code)
elseif md.language == "julia-repl" || Base.startswith(md.language, "jldoctest")
hl = AnnotatedString(md.code)
for (; match) in eachmatch(r"(?:^|\n)julia>", hl)
StyledStrings.face!(match, :markdown_julia_prompt)
afterprompt = match.offset + ncodeunits(match) + 1
_, exprend = Meta.parse(md.code, afterprompt, raise = false)
highlight!(hl[afterprompt:prevind(md.code, exprend)])
if (nextspace = findnext(' ', md.code, exprend)) |> !isnothing
nextword = hl[exprend:prevind(hl, nextspace)]
if nextword == "ERROR:"
StyledStrings.face!(nextword, :error)
end
end
end
hl
elseif md.language == "styled"
styled(md.code)
else
Expand Down

0 comments on commit 02895d7

Please sign in to comment.