Skip to content

Commit

Permalink
Try and get tight / loose working
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Sep 28, 2024
1 parent 2db5510 commit 3694ebe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
11 changes: 6 additions & 5 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,14 +696,15 @@ impl<'o, 'c: 'o> HtmlFormatter<'o, 'c> {
.map(|n| n.data.borrow().value.clone())
{
Some(NodeValue::List(nl)) => nl.tight,
Some(NodeValue::DescriptionItem(nd)) => nd.tight,
_ => false,
};

let tight = tight
|| matches!(
node.parent().map(|n| n.data.borrow().value.clone()),
Some(NodeValue::DescriptionTerm)
);
// let tight = tight
// || matches!(
// node.parent().map(|n| n.data.borrow().value.clone()),
// Some(NodeValue::DescriptionTerm)
// );

if !tight {
if entering {
Expand Down
4 changes: 4 additions & 0 deletions src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ pub struct NodeDescriptionItem {

/// Number of characters between the start of the list marker and the item text (including the list marker(s)).
pub padding: usize,

/// Whether the list is [tight](https://github.github.com/gfm/#tight), i.e. whether the
/// paragraphs are wrapped in `<p>` tags when formatted as HTML.
pub tight: bool,
}

/// The type of list.
Expand Down
28 changes: 27 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1753,12 +1753,14 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {

fn parse_desc_list_details(&mut self, node: &mut &'a AstNode<'a>) -> bool {
let container = node;

let mut tight = false;

let last_child = match container.last_child() {
Some(lc) => lc,
None => {
// Happens when the detail line is directly after the term,
// without a blank line between.
tight = true;
*container = container.parent().unwrap();
container.last_child().unwrap()
}
Expand Down Expand Up @@ -1809,6 +1811,7 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
let metadata = NodeDescriptionItem {
marker_offset: self.indent,
padding: 2,
tight,
};

let item = self.add_child(
Expand All @@ -1827,12 +1830,14 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {

true
} else if node_matches!(last_child, NodeValue::DescriptionItem(..)) {
// ORIGINAL CODE
let parent = last_child.parent().unwrap();
reopen_ast_nodes(parent);

let metadata = NodeDescriptionItem {
marker_offset: self.indent,
padding: 2,
tight,
};

let item = self.add_child(
Expand All @@ -1847,6 +1852,27 @@ impl<'a, 'o, 'c: 'o> Parser<'a, 'o, 'c> {
*container = details;

true

// ATTEMPT 1
// reopen_ast_nodes(last_child);
//
// let details =
// self.add_child(last_child, NodeValue::DescriptionDetails, self.first_nonspace + 1);
// *container = details;
//
// true

// ATTEMPT 2
// let parent = last_child.parent().unwrap();
// let item = parent.last_child().unwrap();
//
// reopen_ast_nodes(item);
//
// let details =
// self.add_child(item, NodeValue::DescriptionDetails, self.first_nonspace + 1);
// *container = details;
//
// true
} else {
false
}
Expand Down
56 changes: 14 additions & 42 deletions src/tests/fixtures/description_lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ with `:` (after 0-2 spaces); subsequent lines must
be indented unless they are lazy paragraph
continuations.

There is no distinction between a "tight" list or a
"loose" list. Definitions are always wrapped in `<p>`
tags.
The list is tight if there is no blank line between
the term and the first definition, otherwise loose.

```````````````````````````````` example
apple
Expand All @@ -24,13 +23,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -68,13 +63,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand All @@ -101,8 +92,6 @@ orange

Multiple blocks in a definition:

Note that the column

```````````````````````````````` example
*apple*
Expand Down Expand Up @@ -161,7 +150,6 @@ term
````````````````````````````````

Multiple definitions, tight:
(always rendered as loose)

```````````````````````````````` example
apple
Expand All @@ -174,19 +162,11 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>
<p>computer company</p>
</dd>
<dd>red fruit</dd>
<dd>computer company</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>
<p>telecom company</p>
</dd>
<dd>orange fruit</dd>
<dd>telecom company</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -271,13 +251,9 @@ orange
.
<dl>
<dt>apple</dt>
<dd>
<p>red fruit</p>
</dd>
<dd>red fruit</dd>
<dt>orange</dt>
<dd>
<p>orange fruit</p>
</dd>
<dd>orange fruit</dd>
</dl>
````````````````````````````````

Expand Down Expand Up @@ -315,12 +291,8 @@ bim
<p>Foo</p>
<dl>
<dt>bar</dt>
<dd>
<p>baz</p>
</dd>
<dd>baz</dd>
<dt>bim</dt>
<dd>
<p>bor</p>
</dd>
<dd>bor</dd>
</dl>
````````````````````````````````

0 comments on commit 3694ebe

Please sign in to comment.