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

Should kramdown treat dd elements like other list entries? #482

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/parser/kramdown.page
Expand Up @@ -28,7 +28,7 @@ option `html_to_native` to `true` to achieve this.

The kramdown parser supports the following options:

{options: {items: [parse_block_html, parse_span_html, html_to_native]}}
{options: {items: [auto_dd_para, parse_block_html, parse_span_html, html_to_native]}}


[PHP Markdown Extra]: http://michelf.com/projects/php-markdown/extra/
Expand Down
5 changes: 4 additions & 1 deletion doc/syntax.page
Expand Up @@ -732,13 +732,16 @@ paragraph otherwise:

definition term
: This definition will just be text because it would normally be a
paragraph and the there is no preceding blank line.
paragraph and there is no preceding blank line.

> although the definition contains other block-level elements

: This definition *will* be a paragraph since it is preceded by a
blank line.

To avoid the resulting mix of inline and block elements, you can use the `:auto_dd_para` option to
mark the first paragraph as such if it is followed by more blocks belonging to the same definition.

The rules about having any block-level element as first element in a list item also apply to a
definition.

Expand Down
19 changes: 19 additions & 0 deletions lib/kramdown/options.rb
Expand Up @@ -305,6 +305,25 @@ def self.simple_hash_validator(val, name)
val
end

define(:auto_dd_para, Boolean, false, <<EOF)
Avoid creation of inline elements adjacent to block elements in
definitions (`:dd` elements).

Without this option, the first paragraph following a definition marker
is marked as a block only if the preceding source line is blank.
Subsequent paragraphs following the same definition marker are always
converted to block elements however. This can result in inline text
followed by block elements in HTML output.

With this option, the parser also checks if there is more than one
paragraph following the same definition marker. If so, the first
paragraph is marked as a block as well. This is roughly similar to how
other list types are handled.

Default: false
Used by: kramdown parser
EOF

define(:footnote_nr, Integer, 1, <<EOF)
The number of the first footnote

Expand Down
5 changes: 4 additions & 1 deletion lib/kramdown/parser/kramdown/list.rb
Expand Up @@ -219,6 +219,8 @@ def parse_definition_list
deflist.children.each do |it|
next if it.type == :dt

first_as_para = it.options.delete(:first_as_para)

parse_blocks(it, it.value)
it.value = nil
next if it.children.size == 0
Expand All @@ -229,7 +231,8 @@ def parse_definition_list
last = nil
end

if it.children.first && it.children.first.type == :p && !it.options.delete(:first_as_para)
if it.children.first && it.children.first.type == :p &&
!(first_as_para || (@options[:auto_dd_para] && it.children.size > 1))
it.children.first.children.first.value << "\n" if it.children.size > 1
it.children.first.options[:transparent] = true
end
Expand Down
32 changes: 32 additions & 0 deletions test/testcases/block/13_definition_list/auto_dd_para.html
@@ -0,0 +1,32 @@
<dl>
<dt>item1</dt>
<dd>
<p>para-iff-auto</p>

<p>para</p>
</dd>
<dt>item2</dt>
<dd>
<p>para-iff-auto</p>
<p>para</p>
</dd>
<dt>item3</dt>
<dd>
<p>para</p>
</dd>
<dt>item4</dt>
<dd>inline</dd>
<dt>item5</dt>
<dd>
<p>para-iff-auto</p>
</dd>
<dt>item6</dt>
<dd>
<p>para</p>
</dd>
<dt>item7</dt>
<dd>inline</dd>
<dt>item9</dt>
<dd>inline</dd>
</dl>

@@ -0,0 +1 @@
:auto_dd_para: true
33 changes: 33 additions & 0 deletions test/testcases/block/13_definition_list/auto_dd_para.text
@@ -0,0 +1,33 @@
item1
: para-iff-auto

para

item2
: para-iff-auto
^
para

item3

: para

item4
: inline

item5
: para-iff-auto
^

item6
: ^
para

item7
:
inline

item9
: inline