cmark-gfm-swift icon indicating copy to clipboard operation
cmark-gfm-swift copied to clipboard

Checkbox range is wrong if text has multibyte characters

Open daisukes opened this issue 6 years ago • 2 comments

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
        }

daisukes avatar Mar 13 '19 16:03 daisukes

Yikes thanks for this. Sorry took me so long to get around to it. And thank you for the failing test. We will fix this.

rnystrom avatar Jun 03 '19 16:06 rnystrom

Hi @rnystrom

It'd be appreciated if you will fix this! I think these bugs are also related to these.

https://github.com/GitHawkApp/GitHawk/issues/2753 https://github.com/GitHawkApp/GitHawk/issues/2655

daisukes avatar Nov 09 '19 15:11 daisukes