Skip to content

Commit

Permalink
fix: handling of newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Jan 16, 2023
1 parent 8e5391b commit b141433
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/reducers/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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});
}
Expand Down

0 comments on commit b141433

Please sign in to comment.