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

make Markdown.parse public #56818

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/Markdown/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ custom flavour of Markdown can be used, but this should generally be unnecessary
Markdown.MD
Markdown.@md_str
Markdown.@doc_str
Markdown.parse
Markdown.html
Markdown.latex
```
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/Common/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function paragraph(stream::IO, md::MD)
char == '\r' && !eof(stream) && peek(stream, Char) == '\n' && read(stream, Char)
if prev_char == '\\'
write(buffer, '\n')
elseif blankline(stream) || parse(stream, md, breaking = true)
elseif blankline(stream) || _parse(stream, md, breaking = true)
break
else
write(buffer, ' ')
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/GitHub/GitHub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function github_paragraph(stream::IO, md::MD)
for char in readeach(stream, Char)
if char == '\n'
eof(stream) && break
if blankline(stream) || parse(stream, md, breaking = true)
if blankline(stream) || _parse(stream, md, breaking = true)
break
else
write(buffer, '\n')
Expand Down
13 changes: 13 additions & 0 deletions stdlib/Markdown/src/Markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ include("render/terminal/render.jl")

export @md_str, @doc_str

public MD, parse

const MARKDOWN_FACES = [
:markdown_header => Face(weight=:bold),
:markdown_h1 => Face(height=1.25, inherit=:markdown_header),
Expand All @@ -57,7 +59,16 @@ const MARKDOWN_FACES = [
__init__() = foreach(addface!, MARKDOWN_FACES)

parse(markdown::String; flavor = julia) = parse(IOBuffer(markdown), flavor = flavor)

"""
Markdown.parse(markdown::AbstractString) -> MD

Parse `markdown` as Julia-flavored Markdown text and return the corresponding `MD` object.

See also [`@md_str`](@ref).
"""
parse(markdown::AbstractString; flavor = julia) = parse(String(markdown), flavor = flavor)

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

function mdexpr(s, flavor = :julia)
Expand All @@ -74,6 +85,8 @@ end

Parse the given string as Markdown text and return a corresponding [`MD`](@ref) object.

See also [`Markdown.parse`](@ref Markdown.parse(::AbstractString)).

# Examples
```jldoctest
julia> s = md"# Hello, world!"
Expand Down
15 changes: 9 additions & 6 deletions stdlib/Markdown/src/parse/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ mutable struct MD
new(content, meta)
end

public MD

MD(xs...) = MD(vcat(xs...))

function MD(cfg::Config, xs...)
Expand Down Expand Up @@ -85,7 +83,7 @@ parseinline(s, md::MD) = parseinline(s, md, config(md))

# Block parsing

function parse(stream::IO, block::MD, config::Config; breaking = false)
function _parse(stream::IO, block::MD, config::Config; breaking = false)
skipblank(stream)
eof(stream) && return false
for parser in (breaking ? config.breaking : [config.breaking; config.regular])
Expand All @@ -94,12 +92,17 @@ function parse(stream::IO, block::MD, config::Config; breaking = false)
return false
end

parse(stream::IO, block::MD; breaking = false) =
parse(stream, block, config(block), breaking = breaking)
_parse(stream::IO, block::MD; breaking = false) =
_parse(stream, block, config(block), breaking = breaking)

"""
parse(stream::IO) -> MD

Parse the content of `stream` as Julia-flavored Markdown text and return the corresponding `MD` object.
"""
function parse(stream::IO; flavor = julia)
isa(flavor, Symbol) && (flavor = flavors[flavor])
markdown = MD(flavor)
while parse(stream, markdown, flavor) end
while _parse(stream, markdown, flavor) end
return markdown
end
Loading