Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 5x 42x 27x 25x 17x 9x 79x 163x 163x | export interface InlinePart {
type: 'text' | 'bold' | 'italic' | 'strike' | 'code' | 'mention'
text: string
}
// shortcut: only **bold**, _italic_, ~~strike~~, `code` and @name, plus the lists of lib/blocks.ts;
// links, quotes and code blocks come with MSG-1 in M2 (the composer already writes their markers).
// The lookbehinds keep the @ of an email address (bob@asoft.com) and the underscores of snake_case
// from reading as a mention or italic.
const TOKEN =
/(\*\*[^*]+\*\*|~~[^~]+~~|`[^`]+`|(?<![\p{L}\p{N}_])_[^_]+_(?![\p{L}\p{N}_])|(?<![\p{L}\p{N}_])@[\p{L}\p{N}_]+)/u
function toTokenPart(piece: string): InlinePart {
if (piece.startsWith('**')) return { type: 'bold', text: piece.slice(2, -2) }
if (piece.startsWith('~~')) return { type: 'strike', text: piece.slice(2, -2) }
if (piece.startsWith('`')) return { type: 'code', text: piece.slice(1, -1) }
if (piece.startsWith('_')) return { type: 'italic', text: piece.slice(1, -1) }
return { type: 'mention', text: piece }
}
// Parts are rendered as React text nodes, so a message body can never inject markup.
export function parseInline(body: string): InlinePart[] {
// split() with one capture group puts the matches at the odd indexes.
return body
.split(TOKEN)
.map((piece, index): InlinePart => {
return index % 2 === 1 ? toTokenPart(piece) : { type: 'text', text: piece }
})
.filter((part) => part.text !== '')
}
|