export interface TodoFormat { prefix: string; content: string; owner?: string; issueId?: string; metaData?: string[]; } export const parse = (content: string) => { const result: TodoFormat = { prefix: '', content: '', metaData: [], }; const todoText = content.toLowerCase().indexOf('todo'); result.prefix = content.substring(0, todoText + 4); let rest = content.substring(todoText + 4); const structureMatches = rest.match(/(\([^)]*\))(.*)/) if (structureMatches && structureMatches.length === 3) { rest = structureMatches[2]; const structured = structureMatches[1].substring(1).slice(0, -1); structured.split(',').forEach(info => { const ninfo = info.trim(); if (ninfo[0] === '#') { result.issueId = ninfo.substring(1); } else if (ninfo[0] === '@') { result.owner = ninfo.substring(1); } else { result.metaData = [...result.metaData || [], ninfo]; ; } }); } result.content = rest.replace(/ *\:? */, ''); return result; } export const build = (todo: TodoFormat) => { const meta = [ todo.issueId ? `#${todo.issueId}` : undefined, todo.owner, ...todo.metaData || [], ].filter(a => a).join(','); const result = [ todo.prefix, meta ? `(${meta})` : undefined, ': ', todo.content, ]; return result.join(''); }