File tree Expand file tree Collapse file tree 1 file changed +23
-8
lines changed
packages/site-kit/src/lib/markdown Expand file tree Collapse file tree 1 file changed +23
-8
lines changed Original file line number Diff line number Diff line change @@ -60,30 +60,45 @@ export function smart_quotes(
60
60
html ?: boolean ;
61
61
} = { }
62
62
) {
63
- let open_quote = false ;
63
+ const stack : Array < "'" | '"' > = [ ] ;
64
64
let res = '' ;
65
65
const len = str . length ;
66
66
for ( let index = 0 ; index < len ; index ++ ) {
67
- let char = str . charAt ( index ) ;
67
+ const char = str . charAt ( index ) ;
68
68
if ( html && char === '&' ) {
69
69
if ( str . slice ( index , index + 5 ) === ''' ) {
70
- let left : boolean = ( first && ! open_quote ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
71
- open_quote = left ;
70
+ const left : boolean =
71
+ ( first && stack . at ( - 1 ) !== "'" ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
72
72
res += `&${ left ? 'l' : 'r' } squo;` ;
73
73
index += 4 ;
74
+ if ( ! left ) {
75
+ stack . pop ( ) ;
76
+ } else {
77
+ stack . push ( "'" ) ;
78
+ }
74
79
} else if ( str . slice ( index , index + 6 ) === '"' ) {
75
- let left : boolean = ( first && ! open_quote ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
76
- open_quote = left ;
80
+ const left : boolean =
81
+ ( first && stack . at ( - 1 ) !== '"' ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
77
82
res += `&${ left ? 'l' : 'r' } dquo` ;
78
83
index += 5 ;
84
+ if ( ! left ) {
85
+ stack . pop ( ) ;
86
+ } else {
87
+ stack . push ( '"' ) ;
88
+ }
79
89
} else {
80
90
res += '&' ;
81
91
}
82
92
} else if ( ! html && ( char === '"' || char === "'" ) ) {
83
- let left : boolean = ( first && ! open_quote ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
84
- open_quote = left ;
93
+ let left : boolean =
94
+ ( first && stack . at ( - 1 ) !== char ) || ( index > 1 && str . charAt ( index - 1 ) === '=' ) ;
85
95
let double = char === '"' ;
86
96
res += double ? ( left ? '“' : '”' ) : left ? '‘' : '’' ;
97
+ if ( ! left ) {
98
+ stack . pop ( ) ;
99
+ } else {
100
+ stack . push ( char ) ;
101
+ }
87
102
} else {
88
103
res += char ;
89
104
}
You can’t perform that action at this time.
0 commit comments