From e9cc16573805b5dcf4fa2e084092ca3b9fdf3fac Mon Sep 17 00:00:00 2001 From: OFF0 Date: Sun, 27 Nov 2022 08:37:09 +0100 Subject: [PATCH] utils: move multiline function to domutils --- src/domutil.js | 12 ++++++++++++ src/main.js | 10 ++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/domutil.js b/src/domutil.js index 8c780a5..dced403 100644 --- a/src/domutil.js +++ b/src/domutil.js @@ -23,3 +23,15 @@ export function elem(name = 'div', {data, ...props} = {}, children = []) { } return el; } + +/** + * Renders line breaks + * + * @param {string} text with newlines + * @return Array + */ +export function multilineText(string) { + return string + .split('\n') + .reduce((acc, next, i) => acc.concat(i === 0 ? next : [elem('br'), next]), []); +} diff --git a/src/main.js b/src/main.js index bbf6a1c..66169c1 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,5 @@ import {relayPool, generatePrivateKey, getPublicKey, signEvent} from 'nostr-tools'; -import {elem} from './domutil.js'; +import {elem, multilineText} from './domutil.js'; import {dateTime, formatTime} from './timeutil.js'; // curl -H 'accept: application/nostr+json' https://nostr.x1ddos.ch const pool = relayPool(); @@ -152,12 +152,6 @@ setInterval(() => { }); }, 10000); -function renderMultilineText(string) { - return string - .split('\n') - .reduce((acc, next, i) => acc.concat(i === 0 ? next : [elem('br'), next]), []); -} - function createTextNote(evt, relay) { const {host, img, isReply, replies, time, userName} = getMetadata(evt, relay); const isLongContent = evt.content.length > 280; @@ -178,7 +172,7 @@ function createTextNote(evt, relay) { elem('time', {dateTime: time.toISOString()}, formatTime(time)), ]), ]), - elem('div', {data: isLongContent ? {append: evt.content.slice(280)} : null}, renderMultilineText(content)), + elem('div', {data: isLongContent ? {append: evt.content.slice(280)} : null}, multilineText(content)), elem('button', { className: 'btn-inline', name: 'star', type: 'button', data: {'eventId': evt.id, relay},