1
+ import { EOL } from 'node:os' ;
2
+
1
3
// General helpers for dealing with markdown files / content.
2
4
3
5
/**
@@ -12,7 +14,7 @@ export function replaceOrCreateHeader(
12
14
newHeader : string ,
13
15
marker : string
14
16
) {
15
- const lines = markdown . split ( '\n' ) ;
17
+ const lines = markdown . split ( EOL ) ;
16
18
17
19
const titleLineIndex = lines . findIndex ( ( line ) => line . startsWith ( '# ' ) ) ;
18
20
const markerLineIndex = lines . indexOf ( marker ) ;
@@ -22,16 +24,18 @@ export function replaceOrCreateHeader(
22
24
// Any YAML front matter or anything else above the title should be kept as-is ahead of the new header.
23
25
const preHeader = lines
24
26
. slice ( 0 , Math . max ( titleLineIndex , dashesLineIndex2 + 1 ) )
25
- . join ( '\n' ) ;
27
+ . join ( EOL ) ;
26
28
27
29
// Anything after the marker comment, title, or YAML front matter should be kept as-is after the new header.
28
30
const postHeader = lines
29
31
. slice (
30
32
Math . max ( markerLineIndex + 1 , titleLineIndex + 1 , dashesLineIndex2 + 1 )
31
33
)
32
- . join ( '\n' ) ;
34
+ . join ( EOL ) ;
33
35
34
- return `${ preHeader ? `${ preHeader } \n` : '' } ${ newHeader } \n${ postHeader } ` ;
36
+ return `${
37
+ preHeader ? `${ preHeader } ${ EOL } ` : ''
38
+ } ${ newHeader } ${ EOL } ${ postHeader } `;
35
39
}
36
40
37
41
/**
@@ -42,7 +46,7 @@ export function findSectionHeader(
42
46
str : string
43
47
) : string | undefined {
44
48
// Get all the matching strings.
45
- const regexp = new RegExp ( `## .*${ str } .*\n ` , 'giu' ) ;
49
+ const regexp = new RegExp ( `## .*${ str } .*${ EOL } ` , 'giu' ) ;
46
50
const sectionPotentialMatches = [ ...markdown . matchAll ( regexp ) ] . map (
47
51
( match ) => match [ 0 ]
48
52
) ;
@@ -64,7 +68,7 @@ export function findSectionHeader(
64
68
}
65
69
66
70
export function findFinalHeaderLevel ( str : string ) {
67
- const lines = str . split ( '\n' ) ;
71
+ const lines = str . split ( EOL ) ;
68
72
const finalHeader = lines . reverse ( ) . find ( ( line ) => line . match ( '^(#+) .+$' ) ) ;
69
73
return finalHeader ? finalHeader . indexOf ( ' ' ) : undefined ;
70
74
}
0 commit comments