Skip to content

Commit

Permalink
Address issues with link references with empty labels
Browse files Browse the repository at this point in the history
Fixes GitHub issue #421.
  • Loading branch information
marcusklaas committed Jan 6, 2020
1 parent 9489d8c commit 32dba3c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
17 changes: 17 additions & 0 deletions specs/regression.txt
Original file line number Diff line number Diff line change
Expand Up @@ -730,3 +730,20 @@ An unordered list before the footnotes:
<p>Cool.</p>
</div>
````````````````````````````````

ISSUE 421

```````````````````````````````` example
[][a]

[a]: b

# assimp-rs [![][crates-badge]][crates]

[crates]: https://crates.io/crates/assimp
[crates-badge]: http://meritbadge.herokuapp.com/assimp
.
<p><a href="b"></a></p>

<h1>assimp-rs <a href="https://crates.io/crates/assimp"><img alt="" src="http://meritbadge.herokuapp.com/assimp"></a></h1>
````````````````````````````````
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ mod tree;
#[cfg(all(target_arch = "x86_64", feature = "simd"))]
mod simd;

pub use crate::parse::{Alignment, CodeBlockKind, Event, LinkType, OffsetIter, Options, Parser, Tag};
pub use crate::parse::{
Alignment, CodeBlockKind, Event, LinkType, OffsetIter, Options, Parser, Tag,
};
pub use crate::strings::{CowStr, InlineStr};
22 changes: 11 additions & 11 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,7 @@ impl<'a> FirstPass<'a> {
self.tree.append(Item {
start: start_ix,
end: 0, // will get set later
body: ItemBody::FencedCodeBlock(
self.allocs.allocate_cow(info_string),
),
body: ItemBody::FencedCodeBlock(self.allocs.allocate_cow(info_string)),
});
self.tree.push();
loop {
Expand Down Expand Up @@ -2147,7 +2145,6 @@ impl<'a> Parser<'a> {
// ok, so its not an inline link. maybe it is a reference
// to a defined link?
let scan_result = scan_reference(&self.tree, block_text, next);
let label_node = self.tree[tos.node].next;
let node_after_link = match scan_result {
RefScan::LinkLabel(_, next_node) => next_node,
RefScan::Collapsed(next_node) => next_node,
Expand All @@ -2158,6 +2155,7 @@ impl<'a> Parser<'a> {
RefScan::Collapsed(..) => LinkType::Collapsed,
RefScan::Failed => LinkType::Shortcut,
};

let label: Option<ReferenceLabel<'a>> = match scan_result {
RefScan::LinkLabel(l, ..) => Some(ReferenceLabel::Link(l)),
RefScan::Collapsed(..) | RefScan::Failed => {
Expand Down Expand Up @@ -2217,17 +2215,20 @@ impl<'a> Parser<'a> {
} else {
ItemBody::Link(link_ix)
};
let label_node = self.tree[tos.node].next;

// lets do some tree surgery to add the link to the tree
// 1st: skip the label node and close node
self.tree[tos.node].next = node_after_link;

// then, add the label node as a child to the link node
self.tree[tos.node].child = label_node;
// then, if it exists, add the label node as a child to the link node
if label_node != cur {
self.tree[tos.node].child = label_node;

// finally: disconnect list of children
if let TreePointer::Valid(prev_ix) = prev {
self.tree[prev_ix].next = TreePointer::Nil;
// finally: disconnect list of children
if let TreePointer::Valid(prev_ix) = prev {
self.tree[prev_ix].next = TreePointer::Nil;
}
}

// set up cur so next node will be node_after_link
Expand Down Expand Up @@ -2349,10 +2350,9 @@ impl<'a> Parser<'a> {
fn scan_inline_link(
&self,
underlying: &'a str,
start_ix: usize,
mut ix: usize,
node: TreePointer,
) -> Option<(usize, CowStr<'a>, CowStr<'a>)> {
let mut ix = start_ix;
if scan_ch(&underlying.as_bytes()[ix..], b'(') == 0 {
return None;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/suite/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,22 @@ An unordered list before the footnotes:

test_markdown_html(original, expected);
}

#[test]
fn regression_test_61() {
let original = r##"[][a]
[a]: b
# assimp-rs [![][crates-badge]][crates]
[crates]: https://crates.io/crates/assimp
[crates-badge]: http://meritbadge.herokuapp.com/assimp
"##;
let expected = r##"<p><a href="b"></a></p>
<h1>assimp-rs <a href="https://crates.io/crates/assimp"><img alt="" src="http://meritbadge.herokuapp.com/assimp"></a></h1>
"##;

test_markdown_html(original, expected);
}

0 comments on commit 32dba3c

Please sign in to comment.