Skip to content
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Version `Unreleased`

* ![Feature][badge-feature] Added support for converting `HTMLBlock` nodes to and from `Markdown.HTMLBlock` when using Julia 1.14+, where the corresponding stdlib type is available.
* ![Feature][badge-feature] Added support for converting `HTMLInline` nodes to and from `Markdown.HTMLInline` when using Julia 1.14+, where the corresponding stdlib type is available.
* ![Feature][badge-feature] Added support for `Strikethrough` nodes when converting to/from the Markdown stdlib AST. This interop requires Julia 1.14+, because `Markdown.Strikethrough` is only available in the stdlib starting there. ([#31][github-31], [#32][github-32])

## Version `v0.1.2`
Expand Down
2 changes: 2 additions & 0 deletions docs/src/stdlib.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Due to the differences between the Markdown representations, the following thing
- When converting a `Markdown.Table`, the resulting table will be normalized, such as adding empty cells to rows, to make sure that all rows have the same number of cells.
- For links and images, the `.title` attribute is set to an empty string, since the standard library AST does not support parsing titles.
- On Julia 1.14 and newer, `Markdown.HTMLBlock` values are converted to [`HTMLBlock`](@ref) elements. On older Julia versions, such nodes do not exist in the standard library AST.
- On Julia 1.14 and newer, `Markdown.HTMLInline` values are converted to [`HTMLInline`](@ref) elements. On older Julia versions, such nodes do not exist in the standard library AST.

## Conversion to standard library representation

Expand All @@ -47,6 +48,7 @@ Due to the differences between the Markdown representations, the following thing
- The standard library does not support storing the child nodes of [`Image`](@ref) elements (i.e. "alt text", `![alt text]()`) as AST, and it is instead reduced to a string with the help of the `Markdown.plain` function.
- The standard library AST does not have dedicated elements for [`SoftBreak`](@ref) and [`Backslash`](@ref), and these get converted into strings (i.e. text elements) instead.
- On Julia 1.14 and newer, [`HTMLBlock`](@ref) elements are converted into `Markdown.HTMLBlock`. On older Julia versions they are converted into `Markdown.Code("html", ...)`.
- On Julia 1.14 and newer, [`HTMLInline`](@ref) elements are converted into `Markdown.HTMLInline`. On older Julia versions they are converted into `Markdown.Code("", ...)`.

## Index

Expand Down
3 changes: 3 additions & 0 deletions src/stdlib/fromstdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ _convert_inline(nodefn::NodeFn, s::Markdown.Image) = @ast Image(s.url, "") do
s.alt # TODO: if isempty(s.alt), should be just omit adding children here?
end
_convert_inline(nodefn::NodeFn, ::Markdown.LineBreak) = nodefn(LineBreak())
@static if isdefined(Markdown, :HTMLInline)
_convert_inline(nodefn::NodeFn, s::Markdown.HTMLInline) = nodefn(HTMLInline(s.content))
end
# Non-Commonmark extensions
_convert_inline(nodefn::NodeFn, s::Markdown.LaTeX) = nodefn(InlineMath(s.formula))
function _convert_inline(nodefn::NodeFn, s::Markdown.Footnote)
Expand Down
6 changes: 5 additions & 1 deletion src/stdlib/tostdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ _convert_element(n::Node, ::Backslash) = "\\"
else
_convert_element(::Node, e::HTMLBlock) = Markdown.Code("html", e.html)
end
_convert_element(::Node, e::HTMLInline) = Markdown.Code("", e.html)
@static if isdefined(Markdown, :HTMLInline)
_convert_element(::Node, e::HTMLInline) = Markdown.HTMLInline(e.html)
else
_convert_element(::Node, e::HTMLInline) = Markdown.Code("", e.html)
end
12 changes: 11 additions & 1 deletion test/fromstdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using MarkdownAST: MarkdownAST, Node, @ast, Document,
Paragraph, Heading, CodeBlock, BlockQuote, DisplayMath, ThematicBreak,
List, Item, FootnoteLink, FootnoteDefinition, Admonition,
Table, TableHeader, TableBody, TableRow, TableCell,
JuliaValue, LineBreak, HTMLBlock
JuliaValue, LineBreak, HTMLBlock, HTMLInline
using Markdown: Markdown
using Test

Expand Down Expand Up @@ -367,6 +367,16 @@ using Test
end
end
end
@static if isdefined(Markdown, :HTMLInline)
let m = Markdown.MD()
push!(m.content, Markdown.Paragraph([Markdown.HTMLInline("<span>x</span>")]))
@test convert(Node, m) == @ast Document() do
Paragraph() do
HTMLInline("<span>x</span>")
end
end
end
end

# Tests from the Documenter Markdown2 module
let ast = convert(Node, Markdown.parse(raw"""
Expand Down
39 changes: 31 additions & 8 deletions test/tostdlib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,39 @@ struct UnknownBlock <: MarkdownAST.AbstractBlock end
md = convert(Markdown.MD, ast)
if isdefined(Markdown, :HTMLBlock)
@test md.content[1] isa Markdown.HTMLBlock
@test convert(Node, md) == @ast MarkdownAST.Document() do
HTMLBlock("<html>")
Paragraph() do; Code("</html>"); end
else
@test md.content[1] isa Markdown.Code
end
if isdefined(Markdown, :HTMLInline)
@test md.content[2].content[1] isa Markdown.HTMLInline
else
@test md.content[2].content[1] isa Markdown.Code
end
if isdefined(Markdown, :HTMLBlock)
if isdefined(Markdown, :HTMLInline)
@test convert(Node, md) == @ast MarkdownAST.Document() do
HTMLBlock("<html>")
Paragraph() do; HTMLInline("</html>"); end
end
else
@test convert(Node, md) == @ast MarkdownAST.Document() do
HTMLBlock("<html>")
Paragraph() do; Code("</html>"); end
end
end
else
# Markdown can't represent raw HTML, so it gets converted into Code
# nodes instead
@test convert(Node, md) == @ast MarkdownAST.Document() do
CodeBlock("html", "<html>")
Paragraph() do; Code("</html>"); end
if isdefined(Markdown, :HTMLInline)
@test convert(Node, md) == @ast MarkdownAST.Document() do
CodeBlock("html", "<html>")
Paragraph() do; HTMLInline("</html>"); end
end
else
# Markdown can't represent raw HTML, so it gets converted into
# Code nodes instead.
@test convert(Node, md) == @ast MarkdownAST.Document() do
CodeBlock("html", "<html>")
Paragraph() do; Code("</html>"); end
end
end
end
end
Expand Down
Loading