@@ -3,20 +3,37 @@ import {fetchPost} from "../../util/fetch";
33import { Constants } from "../../constants" ;
44
55export const encodeBase64 = ( text : string ) : string => {
6- const encoder = new TextEncoder ( ) ;
7- const bytes = encoder . encode ( text ) ;
8- return btoa ( String . fromCharCode ( ...bytes ) ) ;
6+ if ( typeof Buffer !== "undefined" ) {
7+ return Buffer . from ( text , "utf8" ) . toString ( "base64" ) ;
8+ } else {
9+ const encoder = new TextEncoder ( ) ;
10+ const bytes = encoder . encode ( text ) ;
11+ let binary = "" ;
12+ const chunkSize = 0x8000 ; // 避免栈溢出
13+
14+ for ( let i = 0 ; i < bytes . length ; i += chunkSize ) {
15+ const chunk = bytes . subarray ( i , Math . min ( i + chunkSize , bytes . length ) ) ;
16+ binary += String . fromCharCode . apply ( null , chunk ) ;
17+ }
18+
19+ return btoa ( binary ) ;
20+ }
921} ;
1022
1123const getSiyuanHTML = ( text : IClipboardData ) => {
12- const siyuanMatch = text . textHTML . match ( / < ! - - s i y u a n - d a t a : ( [ ^ > ] + ) - - > / ) ;
24+ const siyuanMatch = text . textHTML . match ( / < ! - - d a t a - s i y u a n = ' ( [ ^ ' ] + ) ' - - > / ) ;
1325 if ( siyuanMatch ) {
1426 try {
15- const decoder = new TextDecoder ( ) ;
16- const bytes = Uint8Array . from ( atob ( siyuanMatch [ 1 ] ) , char => char . charCodeAt ( 0 ) ) ;
17- text . siyuanHTML = decoder . decode ( bytes ) ;
27+ if ( typeof Buffer !== "undefined" ) {
28+ const decodedBytes = Buffer . from ( siyuanMatch [ 1 ] , "base64" ) ;
29+ text . siyuanHTML = decodedBytes . toString ( "utf8" ) ;
30+ } else {
31+ const decoder = new TextDecoder ( ) ;
32+ const bytes = Uint8Array . from ( atob ( siyuanMatch [ 1 ] ) , char => char . charCodeAt ( 0 ) ) ;
33+ text . siyuanHTML = decoder . decode ( bytes ) ;
34+ }
1835 // 移除注释节点,保持原有的 text/html 内容
19- text . textHTML = text . textHTML . replace ( / < ! - - s i y u a n - d a t a : [ ^ > ] + - - > / , "" ) ;
36+ text . textHTML = text . textHTML . replace ( / < ! - - d a t a - s i y u a n = ' [ ^ ' ] + ' - - > / , "" ) ;
2037 } catch ( e ) {
2138 console . log ( "Failed to decode siyuan data from HTML comment:" , e ) ;
2239 }
0 commit comments