Fix the datauri macro to work with _canonical_uri tiddlers

This commit is contained in:
Jeremy Ruston 2020-03-30 10:55:37 +01:00
parent 6f4daa70e1
commit 091bcfce7d
4 changed files with 18 additions and 11 deletions

View File

@ -22,14 +22,15 @@ exports.name = "makedatauri";
exports.params = [
{name: "text"},
{name: "type"}
{name: "type"},
{name: "_canonical_uri"}
];
/*
Run the macro
*/
exports.run = function(text,type) {
return $tw.utils.makeDataUri(text,type);
exports.run = function(text,type,_canonical_uri) {
return $tw.utils.makeDataUri(text,type,_canonical_uri);
};
})();

View File

@ -728,16 +728,20 @@ exports.timer = function(base) {
/*
Convert text and content type to a data URI
*/
exports.makeDataUri = function(text,type) {
exports.makeDataUri = function(text,type,_canonical_uri) {
type = type || "text/vnd.tiddlywiki";
var typeInfo = $tw.config.contentTypeInfo[type] || $tw.config.contentTypeInfo["text/plain"],
isBase64 = typeInfo.encoding === "base64",
parts = [];
parts.push("data:");
parts.push(type);
parts.push(isBase64 ? ";base64" : "");
parts.push(",");
parts.push(isBase64 ? text : encodeURIComponent(text));
if(_canonical_uri) {
parts.push(_canonical_uri);
} else {
parts.push("data:");
parts.push(type);
parts.push(isBase64 ? ";base64" : "");
parts.push(",");
parts.push(isBase64 ? text : encodeURIComponent(text));
}
return parts.join("");
};

View File

@ -60,7 +60,7 @@ column-count: $columns$;
\end
\define datauri(title)
<$macrocall $name="makedatauri" type={{$title$!!type}} text={{$title$}}/>
<$macrocall $name="makedatauri" type={{$title$!!type}} text={{$title$}} _canonical_uri={{$title$!!_canonical_uri}}/>
\end
\define if-sidebar(text)

View File

@ -1,5 +1,5 @@
created: 20131228163141555
modified: 20150221223416000
modified: 20200330105334133
tags: Macros [[Core Macros]]
title: makedatauri Macro
type: text/vnd.tiddlywiki
@ -15,5 +15,7 @@ The <<.def makedatauri>> [[macro|Macros]] takes a piece of text and an associate
: The text to be converted to a data URI
;type
: The ContentType of the text
;_canonical_uri
: The optional ''_canonical_uri'' address of the content
<<.macro-examples "makedatauri">>