2017-10-11 16:54:03 +00:00
/ * \
title : $ : / p l u g i n s / t i d d l y w i k i / e x t e r n a l - a t t a c h m e n t s / s t a r t u p . j s
type : application / javascript
module - type : startup
Startup initialisation
\ * /
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
var ENABLE _EXTERNAL _ATTACHMENTS _TITLE = "$:/config/ExternalAttachments/Enable" ,
USE _ABSOLUTE _FOR _DESCENDENTS _TITLE = "$:/config/ExternalAttachments/UseAbsoluteForDescendents" ,
USE _ABSOLUTE _FOR _NON _DESCENDENTS _TITLE = "$:/config/ExternalAttachments/UseAbsoluteForNonDescendents" ;
// Export name and synchronous status
exports . name = "external-attachments" ;
exports . platforms = [ "browser" ] ;
exports . after = [ "startup" ] ;
exports . synchronous = true ;
exports . startup = function ( ) {
test _makePathRelative ( ) ;
$tw . hooks . addHook ( "th-importing-file" , function ( info ) {
if ( document . location . protocol === "file:" && info . isBinary && info . file . path && $tw . wiki . getTiddlerText ( ENABLE _EXTERNAL _ATTACHMENTS _TITLE , "" ) === "yes" ) {
2019-09-06 16:40:03 +00:00
console . log ( "Wiki location" , document . location . pathname )
console . log ( "File location" , info . file . path )
2017-10-11 16:54:03 +00:00
info . callback ( [
{
title : info . file . name ,
type : info . type ,
"_canonical_uri" : makePathRelative (
2019-09-06 16:40:03 +00:00
info . file . path ,
document . location . pathname ,
2017-10-11 16:54:03 +00:00
{
useAbsoluteForNonDescendents : $tw . wiki . getTiddlerText ( USE _ABSOLUTE _FOR _NON _DESCENDENTS _TITLE , "" ) === "yes" ,
useAbsoluteForDescendents : $tw . wiki . getTiddlerText ( USE _ABSOLUTE _FOR _DESCENDENTS _TITLE , "" ) === "yes"
}
)
}
] ) ;
return true ;
} else {
return false ;
}
} ) ;
} ;
/ *
2019-09-06 16:40:03 +00:00
Given a source absolute filepath and a root absolute path , returns the source filepath expressed as a relative filepath from the root path .
sourcepath comes from the "path" property of the file object , with the following patterns :
/ p a t h / t o / f i l e . p n g f o r U n i x s y s t e m s
C : \ path \ to \ file . png for local files on Windows
\ \ sharename \ path \ to \ file . png for network shares on Windows
rootpath comes from document . location . pathname with urlencode applied with the following patterns :
/ p a t h / t o / f i l e . h t m l f o r U n i x s y s t e m s
/ C : / p a t h / t o / f i l e . h t m l f o r l o c a l f i l e s o n W i n d o w s
/ s h a r e n a m e / p a t h / t o / f i l e . h t m l f o r n e t w o r k s h a r e s o n W i n d o w s
2017-10-11 16:54:03 +00:00
* /
function makePathRelative ( sourcepath , rootpath , options ) {
options = options || { } ;
2019-09-06 16:40:03 +00:00
// First we convert the source path from OS-dependent format to generic file:// format
if ( options . isWindows || $tw . platform . isWindows ) {
sourcepath = sourcepath . replace ( /\\/g , "/" ) ;
// If it's a local file like C:/path/to/file.ext then add a leading slash
if ( sourcepath . charAt ( 0 ) !== "/" ) {
sourcepath = "/" + sourcepath ;
}
// If it's a network share then remove one of the leading slashes
if ( sourcepath . substring ( 0 , 2 ) === "//" ) {
sourcepath = sourcepath . substring ( 1 ) ;
}
}
// Split the path into parts
2017-10-11 16:54:03 +00:00
var sourceParts = sourcepath . split ( "/" ) ,
rootParts = rootpath . split ( "/" ) ,
outputParts = [ ] ;
2019-09-06 16:40:03 +00:00
// urlencode the parts of the sourcepath
$tw . utils . each ( sourceParts , function ( part , index ) {
sourceParts [ index ] = encodeURI ( part ) ;
} ) ;
2017-10-11 16:54:03 +00:00
// Identify any common portion from the start
2019-09-06 16:40:03 +00:00
var c = 0 ,
2017-10-11 16:54:03 +00:00
p ;
while ( c < sourceParts . length && c < rootParts . length && sourceParts [ c ] === rootParts [ c ] ) {
c += 1 ;
}
2019-09-06 16:40:03 +00:00
// Use an absolute path if there's no common portion, or if specifically requested
if ( c === 1 || ( options . useAbsoluteForNonDescendents && c < rootParts . length ) || ( options . useAbsoluteForDescendents && c === rootParts . length ) ) {
2017-10-11 16:54:03 +00:00
return sourcepath ;
}
// Move up a directory for each directory left in the root
2020-10-26 18:36:46 +00:00
for ( p = c ; p < rootParts . length - 1 ; p ++ ) {
2017-10-11 16:54:03 +00:00
outputParts . push ( ".." ) ;
}
// Add on the remaining parts of the source path
for ( p = c ; p < sourceParts . length ; p ++ ) {
outputParts . push ( sourceParts [ p ] ) ;
}
return outputParts . join ( "/" ) ;
}
function test _makePathRelative ( ) {
2019-09-06 16:40:03 +00:00
var test = function ( sourcepath , rootpath , result , options ) {
2020-10-26 18:36:46 +00:00
var actualResult = makePathRelative ( sourcepath , rootpath , options ) ;
if ( actualResult !== result ) {
console . log ( "makePathRelative test failed: makePathRelative(" + sourcepath + "," + rootpath + "," + JSON . stringify ( options ) + ") is " + actualResult + " and not equal to " + result ) ;
2019-09-06 16:40:03 +00:00
}
} ;
2020-10-26 18:36:46 +00:00
test ( "/Users/me/something/file.png" , "/Users/you/something/index.html" , "../../me/something/file.png" ) ;
test ( "/Users/me/something/file.png" , "/Users/you/something/index.html" , "/Users/me/something/file.png" , { useAbsoluteForNonDescendents : true } ) ;
test ( "/Users/me/something/else/file.png" , "/Users/me/something/index.html" , "else/file.png" ) ;
test ( "/Users/me/something/file.png" , "/Users/me/something/new/index.html" , "../file.png" ) ;
test ( "/Users/me/something/file.png" , "/Users/me/something/new/index.html" , "/Users/me/something/file.png" , { useAbsoluteForNonDescendents : true } ) ;
test ( "/Users/me/something/file.png" , "/Users/me/something/index.html" , "file.png" ) ;
test ( "/Users/jeremyruston/Downloads/Screenshot 2020-10-18 at 15.33.40.png" , "/Users/jeremyruston/git/Jermolene/TiddlyWiki5/editions/prerelease/output/index.html" , "../../../../../../Downloads/Screenshot%202020-10-18%20at%2015.33.40.png" ) ;
test ( "/Users/me/nothing/image.png" , "/Users/me/something/a/b/c/d/e/index.html" , "../../../../../../nothing/image.png" ) ;
test ( "C:\\Users\\me\\something\\file.png" , "/C:/Users/me/something/index.html" , "file.png" , { isWindows : true } ) ;
test ( "\\\\SHARE\\Users\\me\\something\\file.png" , "/SHARE/Users/me/somethingelse/index.html" , "../something/file.png" , { isWindows : true } ) ;
test ( "\\\\SHARE\\Users\\me\\something\\file.png" , "/C:/Users/me/something/index.html" , "/SHARE/Users/me/something/file.png" , { isWindows : true } ) ;
2017-10-11 16:54:03 +00:00
}
2019-09-06 16:40:03 +00:00
2017-10-11 16:54:03 +00:00
} ) ( ) ;