Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to prevent markdown parsing? #48

Open
ssfrr opened this issue Jul 11, 2023 · 3 comments
Open

Easier way to prevent markdown parsing? #48

ssfrr opened this issue Jul 11, 2023 · 3 comments

Comments

@ssfrr
Copy link

ssfrr commented Jul 11, 2023

I’m logging an interpolated string, but underlines in the path are being interpreted as markdown by the logging:

julia> path = "some_path_foo"
"some_path_foo"

julia> @info "writing to $path"
[ Info: writing to somepathfoo

(you can’t see it, but path is underlined in the somepathfoo in the REPL).

My first idea was to add escapes to the _:

julia> @info replace("writing to $path", "_" => "\\_")
[ Info: writing to some_path_foo

This isn't super great because folks who are using the default ConsoleLogger will see the extra escape characters.

The other option would be to define a custom type that's not an AbstractString and defines the right show method, but this also seems clunky.

Is there an easier way to prevent markdown parsing?

@ssfrr
Copy link
Author

ssfrr commented Jul 11, 2023

For now I just defined

struct RawStr
    x::String
end
Base.show(io::IO, m::MIME"text/plain", s::RawStr) = show(io, m, s.x)

and I can do @info RawStr("writing to $path")

@pepijndevos
Copy link

There should really be a way to disable this IMHO as it breaks any multiline messages, making it unsuitable as a drop-in default logger or in combination with other loggers that don't do markdown

@lmshk
Copy link

lmshk commented Dec 10, 2023

I just had exactly the same problem. The fact that log messages are interpreted as Markdown by default surprised me, especially since if I actually wanted that I could just do:

using Markdown
@info md"Some _emphasized_ text."

Output: Some emphasized text.

Regarding the specific issue of interpolating paths: I usually quote values inside text output, especially if they are text themselves, e.g. in single quotes to signify which part of the message is variable. For TerminalLogger, quoting with backticks instead bypasses the issue:

@info "Some `path_with_underscores`."

Output: Some path_with_underscores.

This is not perfect because TerminalLogger just drops all styling, including the quotations, when the output stream does not support displaying it (like when redirecting standard error into a file) -- instead it should arguably output the source Markdown instead.

In my opinion, Markdown formatting should be opt-in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants