Skip to content

Commit

Permalink
Fix bug with legacy task list position calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolleromero committed Jul 6, 2023
1 parent 70cc227 commit ac211f0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/drafts/MarkdownViewer/_useListInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {ListItem, listItemToString, parseListItem} from '../MarkdownEditor/_useL

type TaskListItem = ListItem & {taskBox: '[ ]' | '[x]'}

const isCodeBlockDelimiter = (line: string) => line.trimStart().startsWith('```')

const isTaskListItem = (item: ListItem | null): item is TaskListItem => typeof item?.taskBox === 'string'

const toggleTaskListItem = (item: TaskListItem): TaskListItem => ({
Expand Down Expand Up @@ -43,11 +45,17 @@ export const useListInteraction = ({
const onToggleItem = useCallback(
(toggledItemIndex: number) => () => {
const lines = markdownRef.current.split('\n')
let inCodeBlock = false

for (let lineIndex = 0, taskIndex = 0; lineIndex < lines.length; lineIndex++) {
if (isCodeBlockDelimiter(lines[lineIndex])) {
inCodeBlock = !inCodeBlock
continue
}

const parsedLine = parseListItem(lines[lineIndex])

if (!isTaskListItem(parsedLine)) continue
if (!isTaskListItem(parsedLine) || inCodeBlock) continue

if (taskIndex === toggledItemIndex) {
const updatedLine = listItemToString(toggleTaskListItem(parsedLine))
Expand Down

0 comments on commit ac211f0

Please sign in to comment.