Skip to content

Commit

Permalink
Styled markdown, with a few tweaks (JuliaLang#51928)
Browse files Browse the repository at this point in the history
With `StyledStrings` and `JuliaSyntaxHighlighting` we can make the
rendering of `Markdown.MD`:
- prettier
- customisable
- more composable

Let's do so!
  • Loading branch information
tecosaur authored May 7, 2024
2 parents a267cec + ada49c3 commit 1614e11
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 165 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a02cd2c8bedd83b74917cf3821c89f46
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2e86daa832533f0369e66e359d7d8f47002f93525f83233c809007a13dfd05a201bcd273b3cb4f3eba2586e98cc9afa43c242f67dc18b91fc898d98a0bd8fde9
6 changes: 5 additions & 1 deletion doc/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "8.6.0+0"

[[deps.JuliaSyntaxHighlighting]]
deps = ["StyledStrings"]
uuid = "dc6e5ff7-fb65-4e79-a425-ec3bc9c03011"

[[deps.LibGit2]]
deps = ["LibGit2_jll", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
Expand Down Expand Up @@ -145,7 +149,7 @@ uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
version = "1.11.0"

[[deps.Markdown]]
deps = ["Base64"]
deps = ["Base64", "JuliaSyntaxHighlighting", "StyledStrings"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
version = "1.11.0"

Expand Down
2 changes: 1 addition & 1 deletion stdlib/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ uuid = "3a97d323-0669-5f0c-9066-3539efd106a3"
version = "4.2.1+0"

[[deps.Markdown]]
deps = ["Base64"]
deps = ["Base64", "JuliaSyntaxHighlighting", "StyledStrings"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
version = "1.11.0"

Expand Down
2 changes: 2 additions & 0 deletions stdlib/Markdown/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version = "1.11.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
JuliaSyntaxHighlighting = "dc6e5ff7-fb65-4e79-a425-ec3bc9c03011"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
2 changes: 2 additions & 0 deletions stdlib/Markdown/src/Common/Common.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

abstract type MarkdownElement end

include("block.jl")
include("inline.jl")

Expand Down
16 changes: 8 additions & 8 deletions stdlib/Markdown/src/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Paragraphs
# ––––––––––

mutable struct Paragraph
mutable struct Paragraph <: MarkdownElement
content
end

Expand Down Expand Up @@ -39,7 +39,7 @@ end
# Headers
# –––––––

mutable struct Header{level}
mutable struct Header{level} <: MarkdownElement
text
end

Expand Down Expand Up @@ -95,7 +95,7 @@ end
# Code
# ––––

mutable struct Code
mutable struct Code <: MarkdownElement
language::String
code::String
end
Expand Down Expand Up @@ -124,7 +124,7 @@ end
# Footnote
# --------

mutable struct Footnote
mutable struct Footnote <: MarkdownElement
id::String
text
end
Expand Down Expand Up @@ -159,7 +159,7 @@ end
# Quotes
# ––––––

mutable struct BlockQuote
mutable struct BlockQuote <: MarkdownElement
content
end

Expand Down Expand Up @@ -188,7 +188,7 @@ end
# Admonitions
# -----------

mutable struct Admonition
mutable struct Admonition <: MarkdownElement
category::String
title::String
content::Vector
Expand Down Expand Up @@ -246,7 +246,7 @@ end
# Lists
# –––––

mutable struct List
mutable struct List <: MarkdownElement
items::Vector{Any}
ordered::Int # `-1` is unordered, `>= 0` is ordered.
loose::Bool # TODO: Renderers should use this field
Expand Down Expand Up @@ -332,7 +332,7 @@ pushitem!(list, buffer) = push!(list.items, parse(String(take!(buffer))).content
# HorizontalRule
# ––––––––––––––

mutable struct HorizontalRule
mutable struct HorizontalRule <: MarkdownElement
end

function horizontalrule(stream::IO, block::MD)
Expand Down
10 changes: 5 additions & 5 deletions stdlib/Markdown/src/Common/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Emphasis
# ––––––––

mutable struct Italic
mutable struct Italic <: MarkdownElement
text
end

Expand All @@ -20,7 +20,7 @@ function underscore_italic(stream::IO, md::MD)
return result === nothing ? nothing : Italic(parseinline(result, md))
end

mutable struct Bold
mutable struct Bold <: MarkdownElement
text
end

Expand Down Expand Up @@ -66,7 +66,7 @@ end
# Images & Links
# ––––––––––––––

mutable struct Image
mutable struct Image <: MarkdownElement
url::String
alt::String
end
Expand All @@ -85,7 +85,7 @@ function image(stream::IO, md::MD)
end
end

mutable struct Link
mutable struct Link <: MarkdownElement
text
url::String
end
Expand Down Expand Up @@ -156,7 +156,7 @@ end
# Punctuation
# –––––––––––

mutable struct LineBreak end
mutable struct LineBreak <: MarkdownElement end

@trigger '\\' ->
function linebreak(stream::IO, md::MD)
Expand Down
6 changes: 3 additions & 3 deletions stdlib/Markdown/src/GitHub/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ end

function term(io::IO, md::Table, columns)
margin_str = " "^margin
cells = mapmap(x -> terminline_string(io, x), md.rows)
padcells!(cells, md.align, len = ansi_length)
cells = mapmap(x -> annotprint(terminline, x), md.rows)
padcells!(cells, md.align, len = textwidth)
for i = 1:length(cells)
print(io, margin_str)
join(io, cells[i], " ")
if i == 1
println(io)
print(io, margin_str)
join(io, [""^ansi_length(cells[i][j]) for j = 1:length(cells[1])], " ")
join(io, [""^textwidth(cells[i][j]) for j = 1:length(cells[1])], " ")
end
i < length(cells) && println(io)
end
Expand Down
25 changes: 24 additions & 1 deletion stdlib/Markdown/src/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ literals `md"..."` and `doc"..."`.
"""
module Markdown

import Base: show, ==, with_output_color, mapany
import Base: AnnotatedString, AnnotatedIOBuffer, show, ==, with_output_color, mapany
using Base64: stringmime

using StyledStrings: StyledStrings, Face, addface!, @styled_str, styled
using JuliaSyntaxHighlighting: highlight, highlight!

# Margin for printing in terminal.
const margin = 2

Expand All @@ -32,6 +35,26 @@ include("render/terminal/render.jl")

export @md_str, @doc_str

const MARKDOWN_FACES = [
:markdown_header => Face(weight=:bold),
:markdown_h1 => Face(height=1.25, inherit=:markdown_header),
:markdown_h2 => Face(height=1.20, inherit=:markdown_header),
:markdown_h3 => Face(height=1.15, inherit=:markdown_header),
:markdown_h4 => Face(height=1.12, inherit=:markdown_header),
:markdown_h5 => Face(height=1.08, inherit=:markdown_header),
:markdown_h6 => Face(height=1.05, inherit=:markdown_header),
:markdown_admonition => Face(weight=:bold),
:markdown_code => Face(inherit=:code),
:markdown_footnote => Face(inherit=:bright_yellow),
:markdown_hrule => Face(inherit=:shadow),
:markdown_inlinecode => Face(inherit=:markdown_code),
:markdown_latex => Face(inherit=:magenta),
:markdown_link => Face(underline=:bright_blue),
:markdown_list => Face(foreground=:blue),
]

__init__() = foreach(addface!, MARKDOWN_FACES)

parse(markdown::AbstractString; flavor = julia) = parse(IOBuffer(markdown), flavor = flavor)
parse_file(file::AbstractString; flavor = julia) = parse(read(file, String), flavor = flavor)

Expand Down
6 changes: 6 additions & 0 deletions stdlib/Markdown/src/render/html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ end

function html(io::IO, code::Code)
withtag(io, :pre) do
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
maybe_lang = !isempty(code.language) ? Any[:class=>"language-$(code.language)"] : []
withtag(io, :code, maybe_lang...) do
htmlesc(io, code.code)
Expand Down Expand Up @@ -134,6 +137,9 @@ function htmlinline(io::IO, content::Vector)
end

function htmlinline(io::IO, code::Code)
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
withtag(io, :code) do
htmlesc(io, code.code)
end
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Markdown/src/render/latex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function latex(io::IO, header::Header{l}) where l
end

function latex(io::IO, code::Code)
if code.language == "styled"
code = Code("", String(styled(code.code)))
end
occursin("\\end{verbatim}", code.code) && error("Cannot include \"\\end{verbatim}\" in a latex code block")
wrapblock(io, "verbatim") do
println(io, code.code)
Expand Down
12 changes: 9 additions & 3 deletions stdlib/Markdown/src/render/rst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ end
function rst(io::IO, code::Code)
if code.language == "jldoctest"
println(io, ".. doctest::\n")
elseif code.language != "rst"
elseif code.language in ("", "julia", "julia-repl")
println(io, ".. code-block:: julia\n")
elseif code.language == "rst"
elseif code.language == "styled"
code = Code("", String(styled(code.code)))
println(io, "::\n")
else
println(io, "::\n")
end
for l in lines(code.code)
for l in eachsplit(code.code, '\n')
println(io, " ", l)
end
end
Expand Down Expand Up @@ -90,7 +96,7 @@ end

function rst(io::IO, l::LaTeX)
println(io, ".. math::\n")
for line in lines(l.formula)
for line in eachsplit(l.formula, '\n')
println(io, " ", line)
end
end
Expand Down
Loading

0 comments on commit 1614e11

Please sign in to comment.