@@ -48,34 +48,60 @@ export interface FrontmatterPluginOptions {
4848
4949const FORBIDDEN_KEYS = new Set ( [ "__proto__" , "constructor" , "prototype" ] ) ;
5050
51+ interface QuoteState {
52+ inDouble : boolean ;
53+ inSingle : boolean ;
54+ backslashCount : number ;
55+ }
56+
57+ const updateQuoteState = (
58+ char : string ,
59+ nextChar : string | undefined ,
60+ state : QuoteState ,
61+ ) : { handledEscape : boolean ; skipNext : boolean } => {
62+ if ( char === "\\" ) {
63+ state . backslashCount ++ ;
64+ return { handledEscape : true , skipNext : false } ;
65+ }
66+ const isEscaped = state . backslashCount % 2 === 1 ;
67+ state . backslashCount = 0 ;
68+
69+ if ( char === '"' && ! state . inSingle && ! isEscaped ) {
70+ state . inDouble = ! state . inDouble ;
71+ } else if ( char === "'" && ! state . inDouble ) {
72+ if ( state . inSingle && nextChar === "'" ) {
73+ return { handledEscape : false , skipNext : true } ;
74+ }
75+ state . inSingle = ! state . inSingle ;
76+ }
77+ return { handledEscape : false , skipNext : false } ;
78+ } ;
79+
5180/**
5281 * Strips comments from a line while preserving '#' within quotes or URLs.
5382 * Tracks backslash parity to correctly identify escaped quotes.
5483 */
5584const stripComment = ( line : string ) : string => {
56- let inDouble = false ;
57- let inSingle = false ;
58- let backslashCount = 0 ;
85+ const state : QuoteState = {
86+ inDouble : false ,
87+ inSingle : false ,
88+ backslashCount : 0 ,
89+ } ;
5990
6091 for ( let i = 0 ; i < line . length ; i ++ ) {
6192 const char = line [ i ] ;
62-
63- if ( char === "\\" ) {
64- backslashCount ++ ;
93+ const { handledEscape, skipNext } = updateQuoteState (
94+ char ,
95+ line [ i + 1 ] ,
96+ state ,
97+ ) ;
98+ if ( handledEscape ) continue ;
99+ if ( skipNext ) {
100+ i ++ ;
65101 continue ;
66102 }
67- const isEscaped = backslashCount % 2 === 1 ;
68- backslashCount = 0 ;
69-
70- if ( char === '"' && ! inSingle && ! isEscaped ) {
71- inDouble = ! inDouble ;
72- } else if ( char === "'" && ! inDouble ) {
73- if ( inSingle && line [ i + 1 ] === "'" ) {
74- i ++ ;
75- continue ;
76- }
77- inSingle = ! inSingle ;
78- } else if ( char === "#" && ! inDouble && ! inSingle ) {
103+
104+ if ( char === "#" && ! state . inDouble && ! state . inSingle ) {
79105 if ( i === 0 || / \s / . test ( line [ i - 1 ] ) ) {
80106 return line . slice ( 0 , i ) . trimEnd ( ) ;
81107 }
@@ -90,31 +116,30 @@ const stripComment = (line: string): string => {
90116const splitQuotedArray = ( str : string ) : string [ ] => {
91117 const items : string [ ] = [ ] ;
92118 let current = "" ;
93- let inDouble = false ;
94- let inSingle = false ;
95- let backslashCount = 0 ;
119+ const state : QuoteState = {
120+ inDouble : false ,
121+ inSingle : false ,
122+ backslashCount : 0 ,
123+ } ;
96124
97125 for ( let i = 0 ; i < str . length ; i ++ ) {
98126 const char = str [ i ] ;
99-
100- if ( char === "\\" ) {
101- backslashCount ++ ;
127+ const { handledEscape, skipNext } = updateQuoteState (
128+ char ,
129+ str [ i + 1 ] ,
130+ state ,
131+ ) ;
132+ if ( handledEscape ) {
102133 current += char ;
103134 continue ;
104135 }
105- const isEscaped = backslashCount % 2 === 1 ;
106- backslashCount = 0 ;
107-
108- if ( char === '"' && ! inSingle && ! isEscaped ) {
109- inDouble = ! inDouble ;
110- } else if ( char === "'" && ! inDouble ) {
111- if ( inSingle && str [ i + 1 ] === "'" ) {
112- current += "''" ;
113- i ++ ;
114- continue ;
115- }
116- inSingle = ! inSingle ;
117- } else if ( char === "," && ! inDouble && ! inSingle ) {
136+ if ( skipNext ) {
137+ current += "''" ;
138+ i ++ ;
139+ continue ;
140+ }
141+
142+ if ( char === "," && ! state . inDouble && ! state . inSingle ) {
118143 if ( current . trim ( ) ) items . push ( current . trim ( ) ) ;
119144 current = "" ;
120145 continue ;
0 commit comments