Skip to content

Commit 0eeedb3

Browse files
authored
fix: correct end-of-section line detection (#1096)
Fixes #593
1 parent 9db2dc4 commit 0eeedb3

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/formatters/helpers/getEndOfSection.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ test("getEndOfSection - capture to last line, shouldConsiderSubsections OFF", ()
311311
expect(result).toBe(2);
312312
});
313313

314+
test("getEndOfSection - target heading with only subsections, should not consider subsections", () => {
315+
const lines = [
316+
"## Insert", // target (0)
317+
"1",
318+
"2", // result (2)
319+
"### Subsection",
320+
"sub content",
321+
];
322+
323+
const targetLine = 0;
324+
325+
const result = getEndOfSection(lines, targetLine, false);
326+
expect(result).toBe(2);
327+
});
328+
314329

315330
test("getMarkdownHeadings - correctly identifies headings", () => {
316331
const lines = [

src/formatters/helpers/getEndOfSection.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,9 @@ function findNextHeading(
177177
fromIdxInBody: number,
178178
headings: Heading[],
179179
): number | null {
180-
const nextheading = headings.findIndex(
181-
(heading) => heading.line > fromIdxInBody,
182-
);
180+
const nextHeading = headings.find((heading) => heading.line > fromIdxInBody);
183181

184-
return nextheading === -1 ? null : nextheading;
182+
return nextHeading ? nextHeading.line : null;
185183
}
186184

187185
function findPriorIdx<T>(

0 commit comments

Comments
 (0)