Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('```')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec is a little looser than this, so there will be some types of code blocks that don't get caught.

They can be delimited by tildes instead of backticks:

~~~
example
~~~

They can also have more than three backticks:

````
example
````

In this case, you'd need to keep track of how long the starting delimiter is so that you can ensure the ending delimiter is the same length.

@nicolleromero nicolleromero Jul 6, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point! Will make that update.


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