Skip to content

Commit 6582eaa

Browse files
authored
Highlight julia-repl code in Markdown specially (#54423)
Fixes #54399 by re-introducing the code seperated out from the styled Markdown PR at Jameson's request (#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 } ```
1 parent 01556a7 commit 6582eaa

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

stdlib/Markdown/src/Markdown.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const MARKDOWN_FACES = [
4545
:markdown_h6 => Face(height=1.05, inherit=:markdown_header),
4646
:markdown_admonition => Face(weight=:bold),
4747
:markdown_code => Face(inherit=:code),
48+
:markdown_julia_prompt => Face(inherit=:repl_prompt_julia),
4849
:markdown_footnote => Face(inherit=:bright_yellow),
4950
:markdown_hrule => Face(inherit=:shadow),
5051
:markdown_inlinecode => Face(inherit=:markdown_code),

stdlib/Markdown/src/render/terminal/render.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,23 @@ function term(io::AnnotIO, md::Header{l}, columns) where l
111111
end
112112

113113
function term(io::IO, md::Code, columns)
114-
code = if md.language ("", "julia", "julia-repl", "jldoctest")
114+
code = if md.language ("", "julia")
115115
highlight(md.code)
116+
elseif md.language == "julia-repl" || Base.startswith(md.language, "jldoctest")
117+
hl = AnnotatedString(md.code)
118+
for (; match) in eachmatch(r"(?:^|\n)julia>", hl)
119+
StyledStrings.face!(match, :markdown_julia_prompt)
120+
afterprompt = match.offset + ncodeunits(match) + 1
121+
_, exprend = Meta.parse(md.code, afterprompt, raise = false)
122+
highlight!(hl[afterprompt:prevind(md.code, exprend)])
123+
if (nextspace = findnext(' ', md.code, exprend)) |> !isnothing
124+
nextword = hl[exprend:prevind(hl, nextspace)]
125+
if nextword == "ERROR:"
126+
StyledStrings.face!(nextword, :error)
127+
end
128+
end
129+
end
130+
hl
116131
elseif md.language == "styled"
117132
styled(md.code)
118133
else

0 commit comments

Comments
 (0)