From 02895d7705a1701e2abc1262c6f1e226c1a284c4 Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 10 May 2024 15:08:01 +0800 Subject: [PATCH] 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 (https://github.com/JuliaLang/julia/pull/51928#discussion_r1483598716). 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](https://github.com/KristofferC/OhMyREPL.jl/issues/100). By way of example, I make it italic by augmenting my `faces.toml` with ```toml [markdown] julia_prompt = { italic = true } ``` --- stdlib/Markdown/src/Markdown.jl | 1 + stdlib/Markdown/src/render/terminal/render.jl | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/stdlib/Markdown/src/Markdown.jl b/stdlib/Markdown/src/Markdown.jl index 935b5d981d6f9..b9ff56297fe51 100644 --- a/stdlib/Markdown/src/Markdown.jl +++ b/stdlib/Markdown/src/Markdown.jl @@ -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), diff --git a/stdlib/Markdown/src/render/terminal/render.jl b/stdlib/Markdown/src/render/terminal/render.jl index 2afd0bd99ff9f..f0ba44ce36571 100644 --- a/stdlib/Markdown/src/render/terminal/render.jl +++ b/stdlib/Markdown/src/render/terminal/render.jl @@ -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