From b14143367fced308c6678d8d648866d93e55a838 Mon Sep 17 00:00:00 2001 From: Ioannis Canellos Date: Mon, 16 Jan 2023 09:46:31 +0200 Subject: [PATCH] fix: handling of newlines --- src/reducers/org.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/reducers/org.js b/src/reducers/org.js index 49c3a452..73632c0b 100644 --- a/src/reducers/org.js +++ b/src/reducers/org.js @@ -854,12 +854,11 @@ const insertCapture = (state, action) => { } if (!shouldCaptureAsNewHeader) { - const header = getTargetHeader(template, headers); + const headerPaths = template.get('headerPaths'); + const header = findHeaderMatchingPaths(headers, headerPaths); const headerId = header.get('id') const rawDescription = header.get('rawDescription'); - const newRawDescription = rawDescription ? - (shouldPrepend ? content + rawDescription : rawDescription + content) - : content; + const newRawDescription = shouldPrepend ? prependContent(rawDescription, content) : appendContent(rawDescription, content); return updateHeaderDescription (state, {headerId, newRawDescription}); } else { const newHeader = newHeaderFromText(content, state.get('todoKeywordSets')).set( @@ -878,8 +877,23 @@ const insertCapture = (state, action) => { return state; }; -const getTargetHeader = (template, headers) => { - const headerPaths = template.get('headerPaths'); +const prependContent = (existing, content) => { + if (!existing || existing === '') { + return content; + } + existing = existing.replace(/^[\s\n]*/, ""); + return content + '\n' + existing; +} + +const appendContent = (existing, content) => { + if (!existing || existing === '') { + return content; + } + existing = existing.replace(/[\s\n]*$/, ""); + return existing + '\n' + content; +} + +const findHeaderMatchingPaths = (headers, headerPaths) => { const header = headerWithPath(headers, headerPaths); return header !== null ? header : newHeaderFromText("", {nestingLevel: 1}); }