Skip to content

Commit 283e889

Browse files
committed
Add heading_setext test suite.
1 parent 546fc1c commit 283e889

File tree

3 files changed

+1161
-7
lines changed

3 files changed

+1161
-7
lines changed

apps/markdown/src/mdast/markdown_mdast.erl

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ enter(CompileContext1 = #markdown_mdast_compile_context{events = Events, index =
283283
%% on_enter_heading
284284
heading_atx ->
285285
on_enter_heading(CompileContext1);
286-
% heading_setext -> on_enter_heading(CompileContext1);
286+
heading_setext ->
287+
on_enter_heading(CompileContext1);
287288
% %% on_enter_html
288289
% html_flow -> on_enter_html(CompileContext1);
289290
% html_text -> on_enter_html(CompileContext1);
@@ -1002,12 +1003,15 @@ exit(CompileContext1 = #markdown_mdast_compile_context{events = Events, index =
10021003
%% on_exit_heading_atx_sequence
10031004
heading_atx_sequence ->
10041005
on_exit_heading_atx_sequence(CompileContext1);
1005-
% %% on_exit_heading_setext
1006-
% heading_setext -> on_exit_heading_setext(CompileContext1);
1007-
% %% on_exit_heading_setext_underline_sequence
1008-
% heading_setext_underline_sequence -> on_exit_heading_setext_underline_sequence(CompileContext1);
1009-
% %% on_exit_heading_setext_text
1010-
% heading_setext_text -> on_exit_heading_setext_text(CompileContext1);
1006+
%% on_exit_heading_setext
1007+
heading_setext ->
1008+
on_exit_heading_setext(CompileContext1);
1009+
%% on_exit_heading_setext_underline_sequence
1010+
heading_setext_underline_sequence ->
1011+
on_exit_heading_setext_underline_sequence(CompileContext1);
1012+
%% on_exit_heading_setext_text
1013+
heading_setext_text ->
1014+
on_exit_heading_setext_text(CompileContext1);
10111015
% %% on_exit_html
10121016
% html_flow -> on_exit_html(CompileContext1);
10131017
% html_text -> on_exit_html(CompileContext1);
@@ -1519,6 +1523,62 @@ on_exit_heading_atx_sequence__node_mut_func(
15191523
on_exit_heading_atx_sequence__node_mut_func(_Node = #markdown_mdast_node{}, {some, _Depth}) ->
15201524
?'unreachable!'("expected heading on stack", []).
15211525

1526+
%% @private
1527+
-doc """
1528+
Handle [`Exit`][Kind::Exit]:[`HeadingSetext`][Name::HeadingSetext].
1529+
""".
1530+
-spec on_exit_heading_setext(CompileContext) -> {ok, CompileContext} | {error, Message} when
1531+
CompileContext :: markdown_mdast_compile_context:t(), Message :: markdown_message:t().
1532+
on_exit_heading_setext(CompileContext1 = #markdown_mdast_compile_context{}) ->
1533+
maybe
1534+
CompileContext2 = CompileContext1#markdown_mdast_compile_context{heading_setext_text_after = false},
1535+
{ok, CompileContext3} ?= on_exit(CompileContext2),
1536+
{ok, CompileContext3}
1537+
end.
1538+
1539+
%% @private
1540+
-doc """
1541+
Handle [`Exit`][Kind::Exit]:[`HeadingSetextText`][Name::HeadingSetextText].
1542+
""".
1543+
-spec on_exit_heading_setext_text(CompileContext) -> {ok, CompileContext} when
1544+
CompileContext :: markdown_mdast_compile_context:t().
1545+
on_exit_heading_setext_text(CompileContext1 = #markdown_mdast_compile_context{}) ->
1546+
CompileContext2 = CompileContext1#markdown_mdast_compile_context{heading_setext_text_after = true},
1547+
{ok, CompileContext2}.
1548+
1549+
%% @private
1550+
-doc """
1551+
Handle [`Exit`][Kind::Exit]:[`HeadingSetextUnderlineSequence`][Name::HeadingSetextUnderlineSequence].
1552+
""".
1553+
-spec on_exit_heading_setext_underline_sequence(CompileContext) -> {ok, CompileContext} when
1554+
CompileContext :: markdown_mdast_compile_context:t().
1555+
on_exit_heading_setext_underline_sequence(
1556+
CompileContext1 = #markdown_mdast_compile_context{bytes = Bytes, events = Events, index = Index}
1557+
) ->
1558+
Slice = markdown_slice:from_position(Bytes, markdown_position:from_exit_event(Events, Index)),
1559+
SliceBytes = markdown_slice:as_binary(Slice),
1560+
Head = binary:first(SliceBytes),
1561+
Depth =
1562+
case Head of
1563+
$- -> 2;
1564+
_ -> 1
1565+
end,
1566+
NodeMutFunc = fun on_exit_heading_setext_underline_sequence__node_mut_func/2,
1567+
{CompileContext2, none} = markdown_mdast_compile_context:tail_mut(CompileContext1, {some, Depth}, NodeMutFunc),
1568+
{ok, CompileContext2}.
1569+
1570+
%% @private
1571+
-spec on_exit_heading_setext_underline_sequence__node_mut_func(Node, OptionDepth) -> {Node, OptionDepth} when
1572+
Node :: markdown_mdast_node:t(), OptionDepth :: markdown_option:t(Depth), Depth :: pos_integer().
1573+
on_exit_heading_setext_underline_sequence__node_mut_func(
1574+
Node1 = #markdown_mdast_node{inner = Heading1 = #markdown_mdast_heading{}}, {some, Depth}
1575+
) ->
1576+
Heading2 = Heading1#markdown_mdast_heading{depth = Depth},
1577+
Node2 = Node1#markdown_mdast_node{inner = Heading2},
1578+
{Node2, none};
1579+
on_exit_heading_setext_underline_sequence__node_mut_func(_Node = #markdown_mdast_node{}, {some, _Depth}) ->
1580+
?'unreachable!'("expected heading on stack", []).
1581+
15221582
%% @private
15231583
-doc """
15241584
Handle [`Exit`][Kind::Exit]:[`LabelText`][Name::LabelText].

0 commit comments

Comments
 (0)