Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and Unify Line Break Handling Across Platforms #79

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fixtures/multiple-paragraphs/expected.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"textarea-one": "1st paragraph\n\n2nd paragraph",
"textarea-two": "1st paragraph\n2nd paragraph"
"textarea-two": "1st paragraph\n\n2nd paragraph"
}
23 changes: 16 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ async function run(env, body, fs, core) {
return input;
}

const value = [input, ...extraLines].join("\n\n").trim();

let value = [input, ...extraLines].join("\n\n").trim();
if (value.toLowerCase() === "_no response_") {
return "";
}
Expand Down Expand Up @@ -111,20 +111,22 @@ async function run(env, body, fs, core) {
return result
}

// Divide each issue into its component sections.
result = body
.trim()
.split("###")
.filter(Boolean)
.map((line) => {
return line
.map((section) => {
return section
.split(/\r?\n\r?\n/)
.filter(Boolean)
.map((item, index, arr) => {
const line = item.trim();

// If the relevant input is a checkbox element
if (line.startsWith("- [")) {
return line.split(/\r?\n/).map((check) => {
const field = check.replace(/- \[[X\s]\]\s+/i, "");

const previousIndex = index === 0 ? index : index - 1;
const key = arr[previousIndex].trim();
// set the type of the field to checkboxes to ensure that values will be represented as an array
Expand All @@ -135,12 +137,20 @@ async function run(env, body, fs, core) {
}
return [key];
});
// Otherwise presume that input is a collected chunk of text lines and separarate them.
} else {

const splitText = line.split(/\r?\n/);
if (splitText.length === 1) {
return line;
}
return splitText.join('\n\n')
}

return line;
});
})
.reduce((prev, curr) => {
// If the item is a checkbox field
if (Array.isArray(curr[1])) {
return [...prev, ...curr[1]];
}
Expand All @@ -151,7 +161,6 @@ async function run(env, body, fs, core) {
result = result.map(([key, ...lines]) => {
const checkListValue = lines.find((line) => Array.isArray(line));
const value = checkListValue ? toValue(checkListValue) : toValue(...lines);

return [toKey(key), value];
});

Expand Down
2 changes: 1 addition & 1 deletion test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ it("multiple paragraphs", () => {
expect(core.getInput).toHaveBeenCalledWith('template-path')
expect(core.setOutput).toHaveBeenCalledWith('jsonString', JSON.stringify(expectedOutput, null, 2))
expect(core.setOutput).toHaveBeenCalledWith('issueparser_textarea-one', '1st paragraph\n\n2nd paragraph')
expect(core.setOutput).toHaveBeenCalledWith('issueparser_textarea-two', '1st paragraph\n2nd paragraph')
expect(core.setOutput).toHaveBeenCalledWith('issueparser_textarea-two', '1st paragraph\n\n2nd paragraph')
expect(core.setOutput.mock.calls.length).toBe(3)
});

Expand Down