File tree Expand file tree Collapse file tree
packages/sv-utils/src/tooling Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11type Header = `${'#' | '##' | '###' | '####' | '#####' | '######' } ${string } `;
22
33const HEADER_REGEX = / ^ # { 1 , 6 } .+ $ / m;
4+ const HEADER_LEVEL_REGEX = / ^ # { 1 , 6 } / m;
5+
6+ function getHeaderLevel ( header : Header ) : number {
7+ return header . split ( ' ' ) [ 0 ] . length ;
8+ }
9+
10+ function findSection (
11+ content : string ,
12+ header : Header
13+ ) : { content : string ; start : number ; end : number } | null {
14+ const [ headerLevel , ...headerNameArray ] = header . split ( ' ' ) ;
15+ const headerName = headerNameArray . join ( ' ' ) ;
16+
17+ const sectionRegex = new RegExp ( `^(${ headerLevel } ) ${ escapeRegex ( headerName ) } \\s*$` , 'm' ) ;
18+ const headerMatch = content . match ( sectionRegex ) ;
19+
20+ if ( ! headerMatch ) return null ;
21+
22+ const start = headerMatch . index ! ;
23+
24+ const afterHeader = content . slice ( start + header . length ) ;
25+ const nextHeaderMatch = afterHeader . match ( HEADER_LEVEL_REGEX ) ;
26+
27+ let end : number ;
28+ if ( nextHeaderMatch ) {
29+ end = start + header . length + afterHeader . indexOf ( nextHeaderMatch [ 0 ] ) ;
30+ } else {
31+ end = content . length ;
32+ }
33+
34+ return {
35+ content : content . slice ( start , end ) ,
36+ start,
37+ end
38+ } ;
39+ }
440
541export function upsert (
642 content : string ,
743 lines : Array < string | false | undefined | null | 0 | 0n > ,
844 options ?: {
945 header ?: {
1046 name : Header ;
11- parent : Header ;
12- position : 'prepend' | 'append' ;
47+ parent ? : Header ;
48+ // position? : 'prepend' | 'append';
1349 } ;
1450 // mode?: 'prepend' | 'append';
1551 }
You can’t perform that action at this time.
0 commit comments