Skip to content

Commit

Permalink
Make Markdown.parse public and add a docstring (#56818)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias314 authored Jan 2, 2025
1 parent 51c2766 commit 4f1842f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
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

4 comments on commit 4f1842f

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@vtjnash
Copy link
Member

@vtjnash vtjnash commented on 4f1842f Jan 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.