Skip to content

Markdown checkboxes toggle incorrect ones if they start with the same text #7113

Open

Description

@phoenixeliot

What happened?

If you create multiple markdown checkboxes with the same text, when you click on one, it toggles the first checkbox before the one you clicked with that same text (that is in the same toggle state) instead of the one you clicked.

Reproduce the Bug

Create a block with this text (in markdown file mode):

necessary first line  
* [ ] thing
* [ ] thing
* [ ] thing

Then click on the 2nd or 3rd checkboxes multiple times and observe that the one you click isn't necessarily the one that gets toggled.

Expected Behavior

The one you click is the one that should toggle.

Screenshots

Screen.Recording.2022-10-26.at.12.07.31.mov

Desktop Platform Information

macOS 12.5, Logseq Version 0.8.9

Mobile Platform Information

No response

Additional Context

The logic seems to include any line that starts with the same text as the line being clicked on, even if also has other text after it:

necessary first line
* [ ] thing
* [ ] thing2
* [ ] t

Activity

  1. changed the title [-]Markdown checkboxes toggle incorrectly if they have the same text[/-] [+]Markdown checkboxes toggle incorrect ones if they have the same text[/+] on Oct 26, 2022
  2. changed the title [-]Markdown checkboxes toggle incorrect ones if they have the same text[/-] [+]Markdown checkboxes toggle incorrect ones if they start with the same text[/+] on Oct 26, 2022
  3. situ2001 commented on Oct 27, 2022

    @situ2001
    Contributor

    I can reproduce this bug, too. I found the logic of code segment related to list-checkbox.

    (defn list-checkbox
    [config checked?]
    (ui/checkbox
    {:style {:margin-right 6}
    :checked checked?
    :on-change (fn [event]
    (let [target (.-target event)
    block (:block config)
    item-content (.. target -nextSibling -data)
    item-full-content (str (if checked? "[X]" "[ ]") " " item-content)
    new-item-full-content (str (if checked? "[ ]" "[X]") " " item-content)]
    (editor-handler/toggle-list-checkbox block item-full-content new-item-full-content)))}))

    The behavior is that if I check a box in a block, this on-change event listener will compute the new content based on the content of checkbox item. And call the following function that replaces the first matched string of this block.

    (defn toggle-list-checkbox
    [{:block/keys [content] :as block} old-item-content new-item-content]
    (let [new-content (string/replace-first content old-item-content new-item-content)]
    (save-block-if-changed! block new-content)))
    that matches item-full-content.

    For example, if I create a list like this.

    necessary first line  
    * [ ] thing
    * [ ] thing
    * [ ] thing
    

    When I click the last checkbox, the codes will recognize that the status checked of the target checkbox turns to true and compute the result item-full-content="[ ] thing" and new-item-full-content="[X] thing". These result will be passed as parameters to function toggle-list-checkbox which replaces the first matched string of the block.

    Hence getting the block

    necessary first line  
    * [X] thing
    * [ ] thing
    * [ ] thing
    

    But not what I expect.

    necessary first line  
    * [ ] thing
    * [ ] thing
    * [X] thing
    
  4. situ2001 commented on Oct 27, 2022

    @situ2001
    Contributor

    If the content of every items are not the same. Everything goes well.

    Hmm, really interesting
    * [ ] thing1
    * [ ] thing2
    * [ ] thing3
    
    Kapture.2022-10-27.at.12.30.31.mp4
  5. m1aw commented on Nov 30, 2022

    @m1aw

    To add some more information on this ticket. If the text starts with the same markup element a similar behaviour happens. In example:

    * [ ] ~~something else~~
    * [ ] something new
    * [ ] ~~another thing~~
    
  6. moved this to 🗄 Backlog in :Logseq/Issue-Trackeron Jan 1, 2023
  7. JustAnotherTill commented on Mar 5, 2023

    @JustAnotherTill

    Also if a checkbox starts with a link (or contains only a link), the wrong checkboxes are marked.

    What happened?

    When checking off checkboxes that start with a link or contain only a link, the wrong checkboxes are marked (from top to bottom).

    Reproduce the Bug

    Create a block with this text (in markdown file mode):

    ## Checklist
    * [ ] Checkbox 1
    * [ ] Checkbox 2
    * [ ] Checkbox 3
    * [ ] Checkbox 4
    * [ ] Checkbox 5
    * [ ] [[Just a link without text before or behind (doesn't work correctly)]]
    * [ ] [[Link to some page]] with text behind (doesn't work)
    * [ ] Text before a [[link to some page]] (works fine)
    

    Then click several times second and third last checkboxes and notice that the box you clicked is not necessarily the one that is toggled.

    Expected Behavior

    The one you click is the one that should toggle.

  8. added
    :type/enhancement
    estimate: medium
    on May 24, 2023
  9. cnrpman commented on May 24, 2023

    @cnrpman
    Collaborator

    The content-based implementation is really awful 🤣
    PR is welcome.

  10. phoenixeliot commented on May 28, 2024

    @phoenixeliot
    ContributorAuthor

    Forgot about and rediscovered this bug again. Sharing this video of slightly different buggy behavior for the record.

    Screen.Recording.2024-05-28.at.00.42.12.mov
  11. JustAnotherTill commented on May 28, 2024

    @JustAnotherTill

    @phoenixeliot I think in your last post the cause of the misbehavior is at the beginning of line 1 & 4 with the same word ‘Mattress’. Could you rename the fourth entry to ‘Another Mattress’ for testing purposes, and then try again?

  12. dm-canteen commented on Jun 30, 2024

    @dm-canteen

    I have been annoyed by this for a while too; you cannot toggle checkboxes which are italic or bold:
    checkbox-behaviour

  13. reddiewalsh commented on Nov 16, 2024

    @reddiewalsh

    I'm able to replicate the same/similar bugs in org-mode:

    • cant check boxes that start with links
    • toggle incorrect box when the words are the same.
    • can't toggle boxes that start with formatting character (bold, italic, underline)
    Screencast.From.2024-11-16.11-02-16.webm

    This is my first time posting about a bug, so let me know if I need to make a separate issue for this.

  14. dm-canteen commented on Nov 20, 2024

    @dm-canteen

    This is still an issue, but the fix involves complicated tree traversal of the generated HTML to identify which checkbox was clicked (currently it is done via a text search, which is the root cause for the wrong one getting ticked).

    I would imagine it will be easier to fix after #9858 is merged

  15. quinn-p-mchugh commented on Aug 6, 2025

    @quinn-p-mchugh

    I would imagine it will be easier to fix after #9858 is merged

    Update: #9858 was merged on May 29th.

    Would love to see this fixed. Anyone interested in working on PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    :type/enhancementestimate: mediumformat:markdown

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions