@@ -31,14 +31,65 @@ function assignAttributes(rootTargetNode, newNode) {
31
31
}
32
32
}
33
33
34
+ function getOutputLocations ( originalSource , outputDirectoryFromAttribute , pageContext , options ) {
35
+ let projectOutputDirectory = options . directories . output ;
36
+
37
+ if ( outputDirectoryFromAttribute ) {
38
+ if ( path . isAbsolute ( outputDirectoryFromAttribute ) ) {
39
+ return {
40
+ outputDir : path . join ( projectOutputDirectory , outputDirectoryFromAttribute ) ,
41
+ urlPath : outputDirectoryFromAttribute ,
42
+ } ;
43
+ }
44
+ return {
45
+ outputDir : path . join ( projectOutputDirectory , pageContext . url , outputDirectoryFromAttribute ) ,
46
+ urlPath : path . join ( pageContext . url , outputDirectoryFromAttribute ) ,
47
+ } ;
48
+ }
49
+
50
+ if ( options . urlPath ) {
51
+ // do nothing, user has specified directories in the plugin options.
52
+ return { } ;
53
+ }
54
+
55
+ if ( path . isAbsolute ( originalSource ) ) {
56
+ // if the path is an absolute one (relative to the content directory) write to a global output directory to avoid duplicate writes for identical source images.
57
+ return {
58
+ outputDir : path . join ( projectOutputDirectory , "/img/" ) ,
59
+ urlPath : "/img/" ,
60
+ } ;
61
+ }
62
+
63
+ // If original source is a relative one, this colocates images to the template output.
64
+ let dir = path . dirname ( pageContext . outputPath ) ;
65
+
66
+ // filename is included in url: ./dir/post.html => /dir/post.html
67
+ if ( pageContext . outputPath . endsWith ( pageContext . url ) ) {
68
+ // remove file name
69
+ let split = pageContext . url . split ( "/" ) ;
70
+ split [ split . length - 1 ] = "" ;
71
+
72
+ return {
73
+ outputDir : dir ,
74
+ urlPath : split . join ( "/" ) ,
75
+ } ;
76
+ }
77
+
78
+ // filename is not included in url: ./dir/post/index.html => /dir/post/
79
+ return {
80
+ outputDir : dir ,
81
+ urlPath : pageContext . url ,
82
+ } ;
83
+ }
84
+
34
85
function transformTag ( context , sourceNode , rootTargetNode , opts ) {
35
86
let originalSource = getSourcePath ( sourceNode , rootTargetNode ) ;
36
87
37
88
if ( ! originalSource ) {
38
89
return sourceNode ;
39
90
}
40
91
41
- let { inputPath, outputPath , url } = context . page ;
92
+ let { inputPath } = context . page ;
42
93
43
94
sourceNode . attrs . src = Util . normalizeImageSource ( {
44
95
input : opts . directories . input ,
@@ -51,36 +102,8 @@ function transformTag(context, sourceNode, rootTargetNode, opts) {
51
102
sourceNode . attrs [ ATTRS . ORIGINAL_SOURCE ] = originalSource ;
52
103
}
53
104
54
- let instanceOptions = { } ;
55
-
56
- let outputDirectory = getOutputDirectory ( sourceNode ) ;
57
- if ( outputDirectory ) {
58
- if ( path . isAbsolute ( outputDirectory ) ) {
59
- instanceOptions = {
60
- outputDir : path . join ( opts . directories . output , outputDirectory ) ,
61
- urlPath : outputDirectory ,
62
- } ;
63
- } else {
64
- instanceOptions = {
65
- outputDir : path . join ( opts . directories . output , url , outputDirectory ) ,
66
- urlPath : path . join ( url , outputDirectory ) ,
67
- } ;
68
- }
69
- } else if ( opts . urlPath ) {
70
- // do nothing, user has specified directories in the plugin options.
71
- } else if ( path . isAbsolute ( originalSource ) ) {
72
- // if the path is an absolute one (relative to the content directory) write to a global output directory to avoid duplicate writes for identical source images.
73
- instanceOptions = {
74
- outputDir : path . join ( opts . directories . output , "/img/" ) ,
75
- urlPath : "/img/" ,
76
- } ;
77
- } else {
78
- // If original source is a relative one, this colocates images to the template output.
79
- instanceOptions = {
80
- outputDir : path . dirname ( outputPath ) ,
81
- urlPath : url ,
82
- } ;
83
- }
105
+ let outputDirectoryFromAttribute = getOutputDirectory ( sourceNode ) ;
106
+ let instanceOptions = getOutputLocations ( originalSource , outputDirectoryFromAttribute , context . page , opts ) ;
84
107
85
108
// returns promise
86
109
return imageAttributesToPosthtmlNode ( sourceNode . attrs , instanceOptions , opts ) . then ( newNode => {
0 commit comments