Skip to content

Commit 4f1842f

Browse files
authored
Make Markdown.parse public and add a docstring (JuliaLang#56818)
1 parent 51c2766 commit 4f1842f

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

stdlib/Markdown/docs/src/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ custom flavour of Markdown can be used, but this should generally be unnecessary
420420
Markdown.MD
421421
Markdown.@md_str
422422
Markdown.@doc_str
423+
Markdown.parse
423424
Markdown.html
424425
Markdown.latex
425426
```

stdlib/Markdown/src/Common/block.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function paragraph(stream::IO, md::MD)
2121
char == '\r' && !eof(stream) && peek(stream, Char) == '\n' && read(stream, Char)
2222
if prev_char == '\\'
2323
write(buffer, '\n')
24-
elseif blankline(stream) || parse(stream, md, breaking = true)
24+
elseif blankline(stream) || _parse(stream, md, breaking = true)
2525
break
2626
else
2727
write(buffer, ' ')

stdlib/Markdown/src/GitHub/GitHub.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function github_paragraph(stream::IO, md::MD)
4444
for char in readeach(stream, Char)
4545
if char == '\n'
4646
eof(stream) && break
47-
if blankline(stream) || parse(stream, md, breaking = true)
47+
if blankline(stream) || _parse(stream, md, breaking = true)
4848
break
4949
else
5050
write(buffer, '\n')

stdlib/Markdown/src/Markdown.jl

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ include("render/terminal/render.jl")
3535

3636
export @md_str, @doc_str
3737

38+
public MD, parse
39+
3840
const MARKDOWN_FACES = [
3941
:markdown_header => Face(weight=:bold),
4042
:markdown_h1 => Face(height=1.25, inherit=:markdown_header),
@@ -57,7 +59,16 @@ const MARKDOWN_FACES = [
5759
__init__() = foreach(addface!, MARKDOWN_FACES)
5860

5961
parse(markdown::String; flavor = julia) = parse(IOBuffer(markdown), flavor = flavor)
62+
63+
"""
64+
Markdown.parse(markdown::AbstractString) -> MD
65+
66+
Parse `markdown` as Julia-flavored Markdown text and return the corresponding `MD` object.
67+
68+
See also [`@md_str`](@ref).
69+
"""
6070
parse(markdown::AbstractString; flavor = julia) = parse(String(markdown), flavor = flavor)
71+
6172
parse_file(file::AbstractString; flavor = julia) = parse(read(file, String), flavor = flavor)
6273

6374
function mdexpr(s, flavor = :julia)
@@ -74,6 +85,8 @@ end
7485
7586
Parse the given string as Markdown text and return a corresponding [`MD`](@ref) object.
7687
88+
See also [`Markdown.parse`](@ref Markdown.parse(::AbstractString)).
89+
7790
# Examples
7891
```jldoctest
7992
julia> s = md"# Hello, world!"

stdlib/Markdown/src/parse/parse.jl

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ mutable struct MD
1515
new(content, meta)
1616
end
1717

18-
public MD
19-
2018
MD(xs...) = MD(vcat(xs...))
2119

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

8684
# Block parsing
8785

88-
function parse(stream::IO, block::MD, config::Config; breaking = false)
86+
function _parse(stream::IO, block::MD, config::Config; breaking = false)
8987
skipblank(stream)
9088
eof(stream) && return false
9189
for parser in (breaking ? config.breaking : [config.breaking; config.regular])
@@ -94,12 +92,17 @@ function parse(stream::IO, block::MD, config::Config; breaking = false)
9492
return false
9593
end
9694

97-
parse(stream::IO, block::MD; breaking = false) =
98-
parse(stream, block, config(block), breaking = breaking)
95+
_parse(stream::IO, block::MD; breaking = false) =
96+
_parse(stream, block, config(block), breaking = breaking)
97+
98+
"""
99+
parse(stream::IO) -> MD
99100
101+
Parse the content of `stream` as Julia-flavored Markdown text and return the corresponding `MD` object.
102+
"""
100103
function parse(stream::IO; flavor = julia)
101104
isa(flavor, Symbol) && (flavor = flavors[flavor])
102105
markdown = MD(flavor)
103-
while parse(stream, markdown, flavor) end
106+
while _parse(stream, markdown, flavor) end
104107
return markdown
105108
end

0 commit comments

Comments
 (0)