@@ -598,12 +598,17 @@ async function run(
598598 function getRelativePath ( ) {
599599 // Get the project url
600600 const projectFolder = addedFolder [ 0 ] ;
601-
601+
602602 // FIXED: Better root folder determination for Termux URIs
603603 let rootFolder = pathName ;
604-
604+
605605 // Special handling for Termux URIs - extract the actual root from the URI structure
606- if ( activeFile && activeFile . uri && activeFile . uri . includes ( "com.termux.documents" ) && activeFile . uri . includes ( "tree/" ) ) {
606+ if (
607+ activeFile &&
608+ activeFile . uri &&
609+ activeFile . uri . includes ( "com.termux.documents" ) &&
610+ activeFile . uri . includes ( "tree/" )
611+ ) {
607612 // Extract the tree part and decode it to get the actual root path
608613 const treeMatch = activeFile . uri . match ( / t r e e \/ ( [ ^ : ] + ) / ) ;
609614 if ( treeMatch ) {
@@ -615,53 +620,57 @@ async function run(
615620 console . error ( "Error decoding Termux root:" , e ) ;
616621 }
617622 }
618- } else if ( projectFolder !== undefined && pathName && pathName . includes ( projectFolder . url ) ) {
623+ } else if (
624+ projectFolder !== undefined &&
625+ pathName &&
626+ pathName . includes ( projectFolder . url )
627+ ) {
619628 rootFolder = projectFolder . url ;
620629 }
621-
630+
622631 //make the uri absolute if necessary
623632 rootFolder = makeUriAbsoluteIfNeeded ( rootFolder ) ;
624-
633+
625634 // Parent of the file
626635 let filePath = pathName ;
627-
636+
628637 if ( rootFolder . startsWith ( "ftp:" ) || rootFolder . startsWith ( "sftp:" ) ) {
629638 if ( rootFolder . includes ( "?" ) ) {
630639 rootFolder = rootFolder . split ( "?" ) [ 0 ] ;
631640 }
632641 }
633-
642+
634643 //remove the query string if present this is needs to be removed because the url is not valid
635644 if ( filePath . startsWith ( "ftp:" ) || rootFolder . startsWith ( "sftp:" ) ) {
636645 if ( filePath . includes ( "?" ) ) {
637646 filePath = filePath . split ( "?" ) [ 0 ] ;
638647 }
639648 }
640-
649+
641650 // Create full file path
642651 let temp = Url . join ( filePath , filename ) ;
643-
652+
644653 // Special handling for Termux URIs
645654 if ( temp . includes ( "com.termux.documents" ) && temp . includes ( "::" ) ) {
646655 try {
647656 const [ , realPath ] = temp . split ( "::" ) ;
648-
657+
649658 console . log ( `DEBUG - realPath: ${ realPath } ` ) ;
650659 console . log ( `DEBUG - rootFolder: ${ rootFolder } ` ) ;
651-
660+
652661 // Ensure rootFolder doesn't have trailing slash for comparison
653662 const normalizedRoot = rootFolder . replace ( / \/ + $ / , "" ) ;
654-
663+
655664 // Check if realPath starts with rootFolder
656665 if ( realPath . startsWith ( normalizedRoot ) ) {
657666 // Remove the rootFolder from the beginning of realPath
658667 let relativePath = realPath . substring ( normalizedRoot . length ) ;
659-
668+
660669 // Remove leading slash if present
661670 relativePath = relativePath . replace ( / ^ \/ + / , "" ) ;
662-
671+
663672 console . log ( `DEBUG - relativePath: ${ relativePath } ` ) ;
664-
673+
665674 if ( relativePath ) {
666675 return relativePath ;
667676 }
@@ -670,25 +679,25 @@ async function run(
670679 console . error ( "Error handling Termux URI:" , e ) ;
671680 }
672681 }
673-
682+
674683 // Handle other content:// URIs
675684 if ( temp . includes ( "content://" ) && temp . includes ( "::" ) ) {
676685 try {
677686 // Get the part after :: which contains the actual file path
678687 const afterDoubleColon = temp . split ( "::" ) [ 1 ] ;
679-
688+
680689 if ( afterDoubleColon ) {
681690 // Extract the rootFolder's content path if it has ::
682691 let rootFolderPath = rootFolder ;
683692 if ( rootFolder . includes ( "::" ) ) {
684693 rootFolderPath = rootFolder . split ( "::" ) [ 1 ] ;
685694 }
686-
695+
687696 // If rootFolder doesn't have ::, try to extract the last part of the path
688697 if ( ! rootFolderPath . includes ( "::" ) ) {
689698 const rootParts = rootFolder . split ( "/" ) ;
690699 const lastPart = rootParts [ rootParts . length - 1 ] ;
691-
700+
692701 // Check if the lastPart is encoded
693702 if ( lastPart . includes ( "%3A" ) ) {
694703 // Try to decode it
@@ -703,14 +712,16 @@ async function run(
703712 rootFolderPath = lastPart ;
704713 }
705714 }
706-
715+
707716 // Use direct string replacement instead of path component comparison
708717 const normalizedRoot = rootFolderPath . replace ( / \/ + $ / , "" ) ;
709718 if ( afterDoubleColon . startsWith ( normalizedRoot ) ) {
710- let relativePath = afterDoubleColon . substring ( normalizedRoot . length ) ;
719+ let relativePath = afterDoubleColon . substring (
720+ normalizedRoot . length ,
721+ ) ;
711722 // Remove leading slash if present
712723 relativePath = relativePath . replace ( / ^ \/ + / , "" ) ;
713-
724+
714725 if ( relativePath ) {
715726 return relativePath ;
716727 }
@@ -720,14 +731,14 @@ async function run(
720731 console . error ( "Error parsing content URI:" , e ) ;
721732 }
722733 }
723-
734+
724735 // For regular paths or if content:// URI parsing failed
725736 // Try to find a common prefix between rootFolder and temp
726737 // and remove it from temp
727738 try {
728739 const rootParts = rootFolder . split ( "/" ) ;
729740 const tempParts = temp . split ( "/" ) ;
730-
741+
731742 let commonIndex = 0 ;
732743 for ( let i = 0 ; i < Math . min ( rootParts . length , tempParts . length ) ; i ++ ) {
733744 if ( rootParts [ i ] === tempParts [ i ] ) {
@@ -736,19 +747,19 @@ async function run(
736747 break ;
737748 }
738749 }
739-
750+
740751 if ( commonIndex > 0 ) {
741752 return tempParts . slice ( commonIndex ) . join ( "/" ) ;
742753 }
743754 } catch ( e ) {
744755 console . error ( "Error finding common path:" , e ) ;
745756 }
746-
757+
747758 // If all else fails, just return the filename
748759 if ( filename ) {
749760 return filename ;
750761 }
751-
762+
752763 console . log ( "Unable to determine relative path, returning full path" ) ;
753764 return temp ;
754765 }
@@ -757,7 +768,7 @@ async function run(
757768 * Opens the preview in browser
758769 */
759770 function openBrowser ( ) {
760- console . log ( `Running ${ Url . join ( pathName , filename ) } ` )
771+ console . log ( `Running ${ Url . join ( pathName , filename ) } ` ) ;
761772 let url = "" ;
762773 if ( pathName === null && ! activeFile . location ) {
763774 url = `http://localhost:${ port } /__unsaved_file__` ;
0 commit comments