(body: string)
| 63 | } |
| 64 | |
| 65 | export function parseNoteBody(body: string): ParsedNoteBody { |
| 66 | const match = body.match(NOTE_PATTERN); |
| 67 | |
| 68 | if (!match) { |
| 69 | const legacy = body.match(LEGACY_NOTE_PATTERN); |
| 70 | if (legacy) { |
| 71 | return { content: legacy[2].trim(), resolved: legacy[1] === 'RESOLVED' }; |
| 72 | } |
| 73 | |
| 74 | // A comment typed straight into the thread on the host. It is still a note, |
| 75 | // just an unresolved one with no recorded author. |
| 76 | return { content: body.trim(), resolved: false }; |
| 77 | } |
| 78 | |
| 79 | let payload: Record<string, unknown>; |
| 80 | try { |
| 81 | payload = JSON.parse(match[1]); |
| 82 | } catch { |
| 83 | // Someone edited the marker by hand into something unparseable. Treat the |
| 84 | // whole comment as content rather than dropping the note. |
| 85 | return { content: body.trim(), resolved: false }; |
| 86 | } |
| 87 | |
| 88 | const author = asString(payload.author); |
| 89 | const authorId = asString(payload.authorId); |
| 90 | |
| 91 | // Both or neither, as formatNoteBody writes them. |
| 92 | const identity = author && authorId ? { author, authorId } : {}; |
| 93 | |
| 94 | return { |
| 95 | content: match[2].trim(), |
| 96 | resolved: payload.resolved === true, |
| 97 | author: identity.author, |
| 98 | authorId: identity.authorId, |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | export function commentToNote(comment: CommentData): Note { |
| 103 | if (!comment || !comment.body) { |
no test coverage detected