Skip to content

Commit 8ff786e

Browse files
committed
fix: issue #75
1 parent 7bbb4c5 commit 8ff786e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/lib/tags.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ import { INDEX_TAG } from '../constants.js';
66
* @returns The updated text
77
*/
88
export const findFirstParagraph = (text: string): string => {
9-
const regex = /^(##\s.+)\n{1,3}/gm;
10-
const split = text.split(regex);
11-
split.splice(1, 0, `<!-- ${INDEX_TAG}-start -->\n<!-- ${INDEX_TAG}-end -->\n\n`);
12-
return split.join('');
9+
const regex = /^(##\s.+)(\n{1,3})/gm;
10+
const match = regex.exec(text);
11+
12+
if (!match) {
13+
return text;
14+
}
15+
16+
const matchStart = match.index;
17+
const matchEnd = match.index + match[0].length;
18+
19+
const beforeMatch = text.substring(0, matchStart);
20+
const headingAndNewlines = match[0];
21+
const afterMatch = text.substring(matchEnd);
22+
23+
return `${beforeMatch}<!-- ${INDEX_TAG}-start -->\n<!-- ${INDEX_TAG}-end -->\n\n${headingAndNewlines}${afterMatch}`;
1324
};
1425

1526
/**

0 commit comments

Comments
 (0)