Skip to content

Commit b914eca

Browse files
committed
feat: improve body parsing logic
Current body parsing logic with trim() + split("###") is too fragile and pose problems with some body that contains case with ### in the middle of the line or case with codeblock ``` section. These case will cause the script to parse these as separate section and produce wrong outputs and in some case even prints error assuming things are checkbox and errors out on the concat function. To make the parsing logic more solid, implement a dedicated function and parse with this logic: - We split the body for "\n" - We ignore codeblock ``` section - We check "###" only at the start of the line - We check for "### " (with the space included) as that is the correct section Github issue template expects. With the following change case like: ``` root@OpenWrt:~# cat /boot/config.txt ``` Are correctly parsed as all part of a single section instead of being wrongly treated as different empty sections. Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
1 parent 2ea9b35 commit b914eca

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,35 @@ async function run(env, body, fs, core) {
111111
return result
112112
}
113113

114-
result = body
115-
.trim()
116-
.split("###")
114+
function parseBody(body) {
115+
let result = [];
116+
let ignore = false;
117+
118+
body.split("\n").reduce((str, line, idx, arr) => {
119+
// ``` Section must be ignored and not parsed as new section
120+
if (line == "```")
121+
ignore = !ignore
122+
123+
// Parse new setion only if they start with ### SPACE
124+
if (!ignore && line.startsWith("### ")) {
125+
result.push(str.trim())
126+
127+
return line.replace(/^### /, "")+"\n";
128+
}
129+
130+
str += line + "\n"
131+
132+
// Push the last string if we are at the end of lines
133+
if (arr.length - 1 == idx)
134+
result.push(str.trim())
135+
136+
return str;
137+
}, "")
138+
139+
return result;
140+
}
141+
142+
result = parseBody(body)
117143
.filter(Boolean)
118144
.map((line) => {
119145
return line

0 commit comments

Comments
 (0)