Skip to content

Commit

Permalink
Merge 1aad0c1 into 8c96909
Browse files Browse the repository at this point in the history
  • Loading branch information
iulia-b authored Nov 28, 2023
2 parents 8c96909 + 1aad0c1 commit c657987
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-swans-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/react': patch
---

Update MarkdownEditor to correctly identify non-standard formatted tasklist items
10 changes: 6 additions & 4 deletions src/drafts/MarkdownEditor/_useListEditing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ const calculateNextListItemStarter = ({leadingWhitespace = '', delimeter, taskBo
* 3. Task box (optional)
* 4. Everything following
*/
export const listItemRegex = /^(\s*)([*-]|(\d+)\.)\s(?:(\[[\sx]\])\s)?(.*)/i
export const listItemRegex = /^(\s*)([*-]|(\d+)\.)(\s{1,4})(?:(\[[\sx]\])\s)?(.*)/i

export type ListItem = {
leadingWhitespace: string
middleWhitespace: string
text: string
delimeter: '-' | '*' | number
taskBox: '[ ]' | '[x]' | null
Expand All @@ -45,22 +46,23 @@ const isNumericListItem = (item: ListItem | null): item is NumericListItem => ty
export const parseListItem = (line: string): ListItem | null => {
const result = listItemRegex.exec(line)
if (!result) return null
const [, leadingWhitespace = '', fullDelimeter, itemNumberStr = '', taskBox = null, text] = result
const [, leadingWhitespace = '', fullDelimeter, itemNumberStr = '', middleWhitespace, taskBox = null, text] = result
const itemNumber = Number.parseInt(itemNumberStr, 10)
const delimeter = Number.isNaN(itemNumber) ? (fullDelimeter as '*' | '-') : itemNumber

return {
leadingWhitespace,
text,
delimeter,
middleWhitespace,
taskBox: taskBox as '[ ]' | '[x]' | null,
}
}

export const listItemToString = (item: ListItem) =>
`${item.leadingWhitespace}${typeof item.delimeter === 'number' ? `${item.delimeter}.` : item.delimeter}${
item.taskBox ? ` ${item.taskBox}` : ''
} ${item.text}`
item.middleWhitespace
}${item.taskBox || ''} ${item.text}`

/**
* Provides support for list editing in the Markdown editor. This includes inserting new
Expand Down

0 comments on commit c657987

Please sign in to comment.