diff options
Diffstat (limited to 'src/features/messages.js')
| -rw-r--r-- | src/features/messages.js | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index c803d4d..06371cf 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -8016,6 +8016,30 @@ export function messagesFeature(ctx) { // `em` is the event's own ["emoji", code, url] map (NIP-30): a :code: it // names becomes its picture, any other :code: stays text — a message // never borrows pictures from packs the READER happens to hold. + // Inline markdown in a post's plain text, the subset people actually type + // and Amethyst already shows: **bold** / __bold__, *italic* / _italic_, + // ~~struck~~ and `code`. Strict about edges so snake_case, 2*3*4 and a lone + // asterisk stay as typed; markers never reach across a link, mention or + // hashtag (those are split out first). No headings or lists in posts — + // "#" at a line's start is a hashtag here, and articles have their own. + const MD_INLINE = /(`[^`\n]+`)|(\*\*\*(?=\S)[^\n]*?\S\*\*\*)|(\*\*(?=\S)[^\n]*?\S\*\*)|((?<![\p{L}\p{N}_])__(?=\S)[^\n]*?\S__(?![\p{L}\p{N}_]))|(~~(?=\S)[^\n]*?\S~~)|((?<![\p{L}\p{N}*])\*(?=[^\s*])[^*\n]*?[^\s*]\*(?![\p{L}\p{N}*]))|((?<![\p{L}\p{N}_])_(?=[^\s_])[^_\n]*?[^\s_]_(?![\p{L}\p{N}_]))/u; + function mdInline(str) { + const out = []; + let rest = String(str); + for (let guard = 0; rest && guard < 200; guard++) { + const m = MD_INLINE.exec(rest); + if (!m) { out.push(rest); break; } + if (m.index) out.push(rest.slice(0, m.index)); + const tok = m[0]; + if (m[1]) out.push(h('code', { class: 'md-code' }, tok.slice(1, -1))); + else if (m[2]) out.push(h('strong', {}, h('em', {}, ...mdInline(tok.slice(3, -3))))); + else if (m[3] || m[4]) out.push(h('strong', {}, ...mdInline(tok.slice(2, -2)))); + else if (m[5]) out.push(h('s', {}, ...mdInline(tok.slice(2, -2)))); + else out.push(h('em', {}, ...mdInline(tok.slice(1, -1)))); + rest = rest.slice(m.index + tok.length); + } + return out; + } function noteBody(text, depth = 0, em = null) { const out = []; const emojiAt = em && em.size ? (c) => em.get(c) : null; @@ -8062,8 +8086,8 @@ export function messagesFeature(ctx) { onClick: (e) => { e.preventDefault(); e.stopPropagation(); openTopicFeed(part.slice(1)); }, }, part)); } else if (emojiAt && part.includes(':')) { - for (const p of splitEmoji(part, emojiAt)) out.push(typeof p === 'string' ? p : emojiImg(p.code, p.url)); - } else out.push(part); + for (const p of splitEmoji(part, emojiAt)) out.push(...(typeof p === 'string' ? mdInline(p) : [emojiImg(p.code, p.url)])); + } else out.push(...mdInline(part)); } return out; } |
