Skip to content

Checkbox range is wrong if text has multibyte characters #11

Open
@daisukes

Description

@daisukes

This test will fail.

    func testMarkdownCheckbox_withChecked_withMultibyte() {
        let markdown = """
            - [x] テスト
            - [x] テスト2
            """
        let node = Node(markdown: markdown)!
        XCTAssertEqual(node.elements.count, 1)
        
        guard case .list(let items, _)? = node.elements.first else { fatalError() }
        // get second checkbox after multibyte characters
        guard case .paragraph(let paragraph)? = items[1].first else { fatalError() }
        guard case .checkbox(let checked, let range) = paragraph[1] else { fatalError() }

        XCTAssertEqual(markdown.substring(with: range), "[x]")
    }

The location of the checkbox should be converted into multibyte count here.
I'm not sure the best way to get the whole content of the original markdown, but this might work.

        var location = Int(cmark_node_get_checkbox_location(node))
        if let string = String(bytes: original_markdown.utf8.prefix(location), encoding:.utf8) {
            location = string.count
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions