In-REPL pager for Markdown

I’m trying to print Markdown output to the terminal, which I can accomplish with…

using Markdown

md_str = md"# My Header"
display(md_str)

Ideally, I’d like to print this to a pager, not the REPL itself. However, InteractiveUtils.less only supports ::AbstractStrings.

Is there any way to do this with code similar to the following code?

using Markdown
using InteractiveUtils

md_str = md"# My Header"
less(md_str)
2 Likes

less can interpret ANSI control sequences that get written by the Markdown.term method that’s used to print markdown in the REPL. So with the following you can kind of get what you’re after I think.

julia> using Markdown

julia> function InteractiveUtils.less(md::Markdown.MD)
           withenv("PAGER" => "less -R -f") do
               file = tempname()
               open(io -> Markdown.term(io, md), file, "w")
               less(file)
               rm(file)
           end
       end

julia> less(@doc +)

You need to change some of the PAGER flags to correctly interpret the ANSI sequences (-R) and to force (-f) it to open what may appear to be a binary file to the pager.

I believe there’s an open issue about making use of the pager to display docs in the REPL, so if anyone what’s to use this as a starting point they’re more than welcome to.

3 Likes

Awesome, thank you for the detailed answer!