From aa422d83ca131f569ceb98a57bed694816e81cf3 Mon Sep 17 00:00:00 2001 From: Leilei332 Date: Sun, 2 Feb 2025 17:32:53 +0800 Subject: [PATCH] Fix incorrect downloaded file extension --- boot/boot.js | 4 ++-- core/modules/saver-handler.js | 2 +- core/modules/savers/download.js | 8 ++++++-- core/templates/exporters/CsvFile.tid | 1 + core/templates/exporters/JsonFile.tid | 1 + core/templates/exporters/TidFile.tid | 1 + core/wiki/macros/export.tid | 1 + 7 files changed, 13 insertions(+), 5 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index 2efa58b1d..f1f6e4906 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -232,10 +232,10 @@ $tw.utils.error = function(err) { var link = dm("a"), text = JSON.stringify(tiddlers); if(Blob !== undefined) { - var blob = new Blob([text], {type: "text/html"}); + var blob = new Blob([text], {type: "application/json"}); link.setAttribute("href", URL.createObjectURL(blob)); } else { - link.setAttribute("href","data:text/html," + encodeURIComponent(text)); + link.setAttribute("href","data:application/json," + encodeURIComponent(text)); } link.setAttribute("download","emergency-tiddlers-" + (new Date()) + ".json"); document.body.appendChild(link); diff --git a/core/modules/saver-handler.js b/core/modules/saver-handler.js index 612613140..e760fd8f1 100644 --- a/core/modules/saver-handler.js +++ b/core/modules/saver-handler.js @@ -185,7 +185,7 @@ SaverHandler.prototype.saveWiki = function(options) { // Call the highest priority saver that supports this method for(var t=this.savers.length-1; t>=0; t--) { var saver = this.savers[t]; - if(saver.info.capabilities.indexOf(method) !== -1 && saver.save(text,method,callback,{variables: {filename: variables.filename}})) { + if(saver.info.capabilities.indexOf(method) !== -1 && saver.save(text,method,callback,{variables: {filename: variables.filename, type: variables.type}})) { this.logger.log("Saving wiki with method",method,"through saver",saver.info.name); return true; } diff --git a/core/modules/savers/download.js b/core/modules/savers/download.js index 0a1d565ce..003c332c3 100644 --- a/core/modules/savers/download.js +++ b/core/modules/savers/download.js @@ -22,6 +22,7 @@ DownloadSaver.prototype.save = function(text,method,callback,options) { options = options || {}; // Get the current filename var filename = options.variables.filename; + var type = options.variables.type; if(!filename) { var p = document.location.pathname.lastIndexOf("/"); if(p !== -1) { @@ -32,13 +33,16 @@ DownloadSaver.prototype.save = function(text,method,callback,options) { if(!filename) { filename = "tiddlywiki.html"; } + if(!type) { + type = "text/html"; + } // Set up the link var link = document.createElement("a"); if(Blob !== undefined) { - var blob = new Blob([text], {type: "text/html"}); + var blob = new Blob([text], {type: type}); link.setAttribute("href", URL.createObjectURL(blob)); } else { - link.setAttribute("href","data:text/html," + encodeURIComponent(text)); + link.setAttribute("href","data:" + type + "," + encodeURIComponent(text)); } link.setAttribute("download",filename); document.body.appendChild(link); diff --git a/core/templates/exporters/CsvFile.tid b/core/templates/exporters/CsvFile.tid index 23d3bbd73..724827d89 100644 --- a/core/templates/exporters/CsvFile.tid +++ b/core/templates/exporters/CsvFile.tid @@ -2,5 +2,6 @@ title: $:/core/templates/exporters/CsvFile tags: $:/tags/Exporter description: {{$:/language/Exporters/CsvFile}} extension: .csv +file-type: text/csv <$macrocall $name="csvtiddlers" filter=<> format="quoted-comma-sep" $output="text/raw"/> diff --git a/core/templates/exporters/JsonFile.tid b/core/templates/exporters/JsonFile.tid index 9008906cc..2ae5495c1 100644 --- a/core/templates/exporters/JsonFile.tid +++ b/core/templates/exporters/JsonFile.tid @@ -2,5 +2,6 @@ title: $:/core/templates/exporters/JsonFile tags: $:/tags/Exporter description: {{$:/language/Exporters/JsonFile}} extension: .json +file-type: application/json <$macrocall $name="jsontiddlers" filter=<> $output="text/raw"/> diff --git a/core/templates/exporters/TidFile.tid b/core/templates/exporters/TidFile.tid index 7b0bb2d78..94f9744a8 100644 --- a/core/templates/exporters/TidFile.tid +++ b/core/templates/exporters/TidFile.tid @@ -2,6 +2,7 @@ title: $:/core/templates/exporters/TidFile tags: $:/tags/Exporter description: {{$:/language/Exporters/TidFile}} extension: .tid +file-type: text/vnd.tiddlywiki condition: [compare:lte[1]] \define renderContent() diff --git a/core/wiki/macros/export.tid b/core/wiki/macros/export.tid index 117359e08..61855eb7d 100644 --- a/core/wiki/macros/export.tid +++ b/core/wiki/macros/export.tid @@ -39,6 +39,7 @@ tags: $:/tags/Macro $param=<> exportFilter=<> filename={{{ [addsuffix{!!extension}] }}} + type={{!!file-type}} /> <$action-deletetiddler $tiddler=<>/> <$transclude field="description"/>