Skip to content

Inconsistent sourcepos for two link types #478

Closed
@SamWilsn

Description

@SamWilsn

Running into a bit of a confusing problem (at least for me.) Two different kinds of links give different sourcepos values when I believe they should be the same.

Here's some demo code:

use comrak::{
    arena_tree::{Node, NodeEdge},
    nodes::{NodeValue, Sourcepos},
    parse_document, Arena, ExtensionOptions, Options,
};

fn demo(source: &str) -> Sourcepos {
    let arena = Arena::new();
    let mut extension = ExtensionOptions::default();
    extension.autolink = true;

    let root = parse_document(
        &arena,
        source,
        &Options {
            extension,
            ..Default::default()
        },
    );

    let link = root
        .traverse()
        .filter_map(|x| match x {
            NodeEdge::Start(Node { data, .. }) => Some(data.borrow().clone()),
            _ => None,
        })
        .filter(|x| matches!(x.value, NodeValue::Link(_)))
        .next()
        .unwrap();

    link.sourcepos
}

fn main() {
    let a = demo("hello <https://example.com/fooo> world");
    let b = demo("hello [foo](https://example.com) world");
    assert_eq!(a, b);
}

And the output I'm getting:

thread 'main' panicked at src/main.rs:37:5:
assertion `left == right` failed
  left: Sourcepos { start: LineColumn { line: 1, column: 8 }, end: LineColumn { line: 1, column: 33 } }
 right: Sourcepos { start: LineColumn { line: 1, column: 7 }, end: LineColumn { line: 1, column: 32 } }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions