const TASK_ITEM_RE = /]*data-type="taskItem"[^>]*)>/gi; export function toggleTaskItemInHtml(html: string, taskIndex: number, checked: boolean): string { let index = 0; return html.replace(TASK_ITEM_RE, (match, attrs: string) => { const current = index; index += 1; if (current !== taskIndex) return match; const checkedValue = checked ? "true" : "false"; let nextAttrs = attrs; if (/data-checked="(?:true|false)"/i.test(nextAttrs)) { nextAttrs = nextAttrs.replace( /data-checked="(?:true|false)"/i, `data-checked="${checkedValue}"`, ); } else { nextAttrs = `${nextAttrs} data-checked="${checkedValue}"`; } return ``; }); } export function countTaskItems(html: string): number { return [...html.matchAll(TASK_ITEM_RE)].length; }