-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiddlyweb-external-attachments.json
1 lines (1 loc) · 12.1 KB
/
tiddlyweb-external-attachments.json
1
[{"title":"$:/plugins/tiddlywiki/tiddlyweb-external-attachments","name":"TiddlyWeb External Attachments","description":"External attachments for TiddlyWeb","list":"readme settings","version":"5.2.7-prerelease","plugin-type":"plugin","dependents":"","type":"application/json","text":"{\"tiddlers\":{\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/startup.js\":{\"title\":\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/startup.js\",\"text\":\"/*\\\\\\ntitle: $:/plugins/tiddlywiki/tiddlyweb-external-attachments/startup.js\\ntype: application/javascript\\nmodule-type: startup\\n\\nStartup initialisation\\n\\n\\\\*/\\n(function () {\\n\\n\\t/*jslint node: true, browser: true */\\n\\t/*global $tw: false */\\n\\t\\\"use strict\\\";\\n\\n\\n\\tconst CONFIG_HOST_TIDDLER = \\\"$:/config/tiddlyweb/host\\\";\\n\\tconst DEFAULT_HOST_TIDDLER = \\\"$protocol$//$host$/\\\";\\n\\n\\tconst ENABLE_EXTERNAL_ATTACHMENTS_TITLE = \\\"$:/config/TiddlyWebExternalAttachments/Enable\\\";\\n\\tconst EXTERNAL_ATTACHMENTS_PATH_TITLE = '$:/config/TiddlyWebExternalAttachments/ExternalAttachmentsPath';\\n\\tconst ONLY_BINARY_TITLE = '$:/config/TiddlyWebExternalAttachments/OnlyBinary';\\n\\tconst SIZE_TITLE = '$:/config/TiddlyWebExternalAttachments/SizeForExternal';\\n\\tconst DEBUG_TITLE = '$:/config/TiddlyWebExternalAttachments/Debug';\\n\\n\\n\\tconst WIKITEXT_TYPE = 'text/vnd.tiddlywiki';\\n\\n\\t// Export name and synchronous status\\n\\texports.name = \\\"tiddlyweb-external-attachments\\\";\\n\\texports.platforms = [\\\"browser\\\"];\\n\\texports.after = [\\\"startup\\\"];\\n\\texports.synchronous = true;\\n\\n\\texports.startup = function () {\\n\\t\\tconst log = (...args) => {\\n\\t\\t\\tif ($tw.wiki.getTiddlerText(DEBUG_TITLE, \\\"\\\") !== \\\"yes\\\") return;\\n\\t\\t\\tconsole.log.apply(console, args)\\n\\t\\t};\\n\\n\\t\\tlog(\\\"[startup]\\\", exports, this);\\n\\n\\t\\tconst refTable = {};\\n\\n\\t\\t// add file reference\\n\\t\\t$tw.hooks.addHook(\\\"th-importing-file\\\", function (info) {\\n\\t\\t\\tif ($tw.wiki.getTiddlerText(ENABLE_EXTERNAL_ATTACHMENTS_TITLE, \\\"\\\") !== \\\"yes\\\") return false; // skip\\n\\t\\t\\tlog(\\\"[th-importing-file]\\\", exports, info);\\n\\n\\t\\t\\tconst {\\n\\t\\t\\t\\tfile,\\n\\t\\t\\t\\ttype,\\n\\t\\t\\t\\tisBinary,\\n\\t\\t\\t\\tcallback,\\n\\t\\t\\t\\tdeserializer,\\n\\t\\t\\t\\t...other\\n\\t\\t\\t} = info;\\n\\n\\t\\t\\t// Figure out if we're reading a binary file\\n\\t\\t\\tlet contentTypeInfo = $tw.config.contentTypeInfo[type];\\n\\t\\t\\t// let isBinary = contentTypeInfo ? contentTypeInfo.encoding === \\\"base64\\\" : false;\\n\\t\\t\\tlet isBin = (contentTypeInfo && contentTypeInfo.encoding === \\\"base64\\\") || !contentTypeInfo || isBinary;\\n\\n\\t\\t\\t// $tw.wiki.readFileContent(file, type, isBin, options.deserializer, callback);\\n\\t\\t\\t$tw.wiki.readFileContent(file, type, isBin, deserializer, (data) => {\\n\\t\\t\\t\\tlog(\\\"[readFileContent]cb\\\", info, file, data);\\n\\t\\t\\t\\tif (data.length && isBin) {\\n\\t\\t\\t\\t\\tconst token = `${Date.now()}-${makeid(8)}`;\\n\\t\\t\\t\\t\\tdata[0]['ref'] = token;\\n\\t\\t\\t\\t\\trefTable[token] = file;\\n\\t\\t\\t\\t}\\n\\t\\t\\t\\tcallback(data);\\n\\t\\t\\t});\\n\\t\\t\\treturn true;\\n\\t\\t});\\n\\n\\t\\t// remove unused reference\\n\\t\\t$tw.hooks.addHook(\\\"th-before-importing\\\", function (importTiddler) {\\n\\t\\t\\tif ($tw.wiki.getTiddlerText(ENABLE_EXTERNAL_ATTACHMENTS_TITLE, \\\"\\\") !== \\\"yes\\\") return importTiddler; // skip\\n\\n\\t\\t\\tlet tiddlers = JSON.parse(importTiddler.fields.text || '{}').tiddlers || {};\\n\\t\\t\\tfor (let title in tiddlers) {\\n\\t\\t\\t\\tconst tiddler = tiddlers[title];\\n\\t\\t\\t\\tif (importTiddler.fields[\\\"selection-\\\" + title] === \\\"unchecked\\\") {\\n\\t\\t\\t\\t\\tconst token = tiddler.ref;\\n\\t\\t\\t\\t\\tdelete refTable[token]; // remove reference\\n\\t\\t\\t\\t}\\n\\t\\t\\t}\\n\\t\\t\\tlog(\\\"[th-before-importing]\\\", exports, importTiddler, tiddlers, refTable);\\n\\n\\t\\t\\treturn importTiddler;\\n\\t\\t});\\n\\n\\t\\t$tw.hooks.addHook(\\\"th-importing-tiddler\\\", (tiddler) => {\\n\\t\\t\\tif ($tw.wiki.getTiddlerText(ENABLE_EXTERNAL_ATTACHMENTS_TITLE, \\\"\\\") !== \\\"yes\\\") return tiddler; // skip\\n\\n\\t\\t\\tlog(\\\"[th-importing-tiddler]0\\\", exports, tiddler, refTable); // debug\\n\\t\\t\\tconst {\\n\\t\\t\\t\\ttitle,\\n\\t\\t\\t\\ttype = WIKITEXT_TYPE,\\n\\t\\t\\t\\ttext = null,\\n\\t\\t\\t\\tref = null, // file reference\\n\\t\\t\\t\\t...other\\n\\t\\t\\t} = tiddler.fields || {};\\n\\n\\t\\t\\tconst file = refTable[ref] || null;\\n\\t\\t\\tif (ref !== null) { // remove reference\\n\\t\\t\\t\\ttiddler = new $tw.Tiddler(tiddler, { ref: null });\\n\\t\\t\\t\\t// delete tiddler.fields[ref];\\n\\t\\t\\t\\tdelete refTable[ref];\\n\\t\\t\\t}\\n\\t\\t\\tlog(\\\"[th-importing-tiddler]\\\", tiddler?.fields, file, refTable); // debug\\n\\n\\t\\t\\t// check binary and size\\n\\t\\t\\tlet contentTypeInfo = $tw.config.contentTypeInfo[type]\\n\\t\\t\\tlet isBinary = (contentTypeInfo && contentTypeInfo.encoding === \\\"base64\\\") || !contentTypeInfo;\\n\\t\\t\\tif (!isBinary && $tw.wiki.getTiddlerText(ONLY_BINARY_TITLE, \\\"yes\\\") === \\\"yes\\\") return tiddler; // skip text file\\n\\n\\t\\t\\tconst size = Number.parseInt($tw.wiki.getTiddlerText(SIZE_TITLE, \\\"\\\")) || 16384;\\n\\t\\t\\tif (text && text.length < size) return tiddler; // skip small file\\n\\n\\t\\t\\t// do upload, and update Tiddler when finish\\n\\t\\t\\treturn doUpload(tiddler.fields, file);\\n\\t\\t});\\n\\n\\t\\tconst doUpload = (tiddlerFields, file) => {\\n\\t\\t\\tconst {\\n\\t\\t\\t\\ttitle,\\n\\t\\t\\t\\ttype = WIKITEXT_TYPE,\\n\\t\\t\\t\\ttext = null,\\n\\t\\t\\t\\t...other\\n\\t\\t\\t} = tiddlerFields;\\n\\n\\t\\t\\t// const convFn = $tw.syncadaptor.convertTiddlerToTiddlyWebFormat || JSON.stringify;\\n\\t\\t\\tconst convFn = JSON.stringify;\\n\\t\\t\\tconst meta = {\\n\\t\\t\\t\\ttitle,\\n\\t\\t\\t\\ttype,\\n\\t\\t\\t\\t...other\\n\\t\\t\\t};\\n\\t\\t\\tif (!file) { // use base64 string in text to build blob\\n\\t\\t\\t\\tconst textU8 = Uint8Array.from(atob(text), (c) => c.charCodeAt(0));\\n\\t\\t\\t\\tfile = new Blob([textU8], { type: 'application/octet-stream' });\\n\\t\\t\\t}\\n\\t\\t\\tconst data = new FormData();\\n\\t\\t\\tdata.append('meta', convFn(meta));\\n\\t\\t\\tdata.append('text', file, 'text');\\n\\n\\t\\t\\tlog(\\\"[upload]\\\", title, tiddlerFields, data, meta, file);\\n\\n\\t\\t\\t// $tw.utils.httpRequest() can not use FormData\\n\\t\\t\\tfetch(`${getHost()}upload/`, {\\n\\t\\t\\t\\tmethod: 'POST',\\n\\t\\t\\t\\tmode: 'cors',\\n\\t\\t\\t\\t// headers: {\\n\\t\\t\\t\\t// \\t'Content-type': 'multipart/form-data',\\n\\t\\t\\t\\t// },\\n\\t\\t\\t\\tcredentials: 'same-origin',\\n\\t\\t\\t\\tbody: data,\\n\\t\\t\\t}).then(async (resp) => {\\n\\t\\t\\t\\tif (!resp.ok) throw new Error(resp.statusText);\\n\\n\\t\\t\\t\\t// TODO: use json?\\n\\t\\t\\t\\tconst token = await resp.text();\\n\\n\\t\\t\\t\\t// TODO: update tiddler\\n\\t\\t\\t\\tconst base_path = $tw.wiki.getTiddlerText(EXTERNAL_ATTACHMENTS_PATH_TITLE, \\\"/\\\");\\n\\t\\t\\t\\tconst tiddler = new $tw.Tiddler(tiddlerFields, {\\n\\t\\t\\t\\t\\ttype: type, // set type back\\n\\t\\t\\t\\t\\ttext: null, // remove text\\n\\t\\t\\t\\t\\t'_canonical_uri': `${base_path}${token}`,\\n\\t\\t\\t\\t});\\n\\t\\t\\t\\t$tw.wiki.addTiddler(tiddler);\\n\\t\\t\\t}).catch((err) => {\\n\\t\\t\\t\\tlog(\\\"[upload]err\\\", title, tiddlerFields, err);\\n\\t\\t\\t\\t// show error as text\\n\\t\\t\\t\\t$tw.wiki.addTiddler(new $tw.Tiddler(tiddlerFields, {\\n\\t\\t\\t\\t\\ttype: WIKITEXT_TYPE,\\n\\t\\t\\t\\t\\ttext: `! upload \\\\`${title}\\\\` error\\n\\\\`\\\\`\\\\`\\n${err}\\n\\\\`\\\\`\\\\``,\\n\\t\\t\\t\\t}));\\n\\t\\t\\t})\\n\\n\\t\\t\\t// show uploading\\n\\t\\t\\treturn new $tw.Tiddler(tiddlerFields, {\\n\\t\\t\\t\\ttype: WIKITEXT_TYPE,\\n\\t\\t\\t\\ttext: `uploading.... \\\\`${title}\\\\``,\\n\\t\\t\\t});\\n\\t\\t};\\n\\t};\\n\\n\\tconst getHost = () => {\\n\\t\\tvar text = $tw.wiki.getTiddlerText(CONFIG_HOST_TIDDLER, DEFAULT_HOST_TIDDLER),\\n\\t\\t\\tsubstitutions = [\\n\\t\\t\\t\\t{ name: \\\"protocol\\\", value: document.location.protocol },\\n\\t\\t\\t\\t{ name: \\\"host\\\", value: document.location.host }\\n\\t\\t\\t];\\n\\t\\tfor (var t = 0; t < substitutions.length; t++) {\\n\\t\\t\\tvar s = substitutions[t];\\n\\t\\t\\ttext = $tw.utils.replaceString(text, new RegExp(\\\"\\\\\\\\$\\\" + s.name + \\\"\\\\\\\\$\\\", \\\"mg\\\"), s.value);\\n\\t\\t}\\n\\t\\treturn text;\\n\\t};\\n\\n\\tconst makeid = (length) => {\\n\\t\\tvar result = '';\\n\\t\\tvar characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\\n\\t\\tvar charactersLength = characters.length;\\n\\t\\tfor (var i = 0; i < length; i++) {\\n\\t\\t\\tresult += characters.charAt(Math.floor(Math.random() * charactersLength));\\n\\t\\t}\\n\\t\\treturn result;\\n\\t}\\n\\n\\tconsole.log(\\\"[init]\\\", exports, this)\\n\\n})();\\n\",\"type\":\"application/javascript\",\"module-type\":\"startup\"},\"$:/config/TiddlyWebExternalAttachments/Enable\":{\"title\":\"$:/config/TiddlyWebExternalAttachments/Enable\",\"text\":\"yes\"},\"$:/config/TiddlyWebExternalAttachments/OnlyBinary\":{\"title\":\"$:/config/TiddlyWebExternalAttachments/OnlyBinary\",\"text\":\"yes\"},\"$:/config/TiddlyWebExternalAttachments/ExternalAttachmentsPath\":{\"title\":\"$:/config/TiddlyWebExternalAttachments/ExternalAttachmentsPath\",\"text\":\"files/\"},\"$:/config/TiddlyWebExternalAttachments/SizeForExternal\":{\"title\":\"$:/config/TiddlyWebExternalAttachments/SizeForExternal\",\"text\":\"16384\"},\"$:/config/TiddlyWebExternalAttachments/Debug\":{\"title\":\"$:/config/TiddlyWebExternalAttachments/Debug\",\"text\":\"no\"},\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/readme\":{\"title\":\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/readme\",\"text\":\"! Introduction\\n\\nThis plugin provides support for importing tiddlers as external attachments. That means that instead of importing binary files as self-contained tiddlers, they are imported as \\\"skinny\\\" tiddlers that reference the original file via the ''_canonical_uri'' field. This reduces the size of the wiki and thus improves performance. However, it does mean that the wiki is no longer fully self-contained.\\n\\n! Compatibility\\n\\nThis plugin only works when using TiddlyWiki with platforms such as TiddlyWeb that support the ''attachment api'' for imported/dragged files.\\n\\n\"},\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/settings\":{\"title\":\"$:/plugins/tiddlywiki/tiddlyweb-external-attachments/settings\",\"text\":\"When used on platforms that provide the necessary support (such as ~TiddlyDesktop), you can optionally import binary files as external tiddlers that reference the original file via the ''_canonical_uri'' field.\\n\\nBy default, a relative path is used to reference the file. Optionally, you can specify that an absolute path is used instead. You can do this separately for \\\"descendent\\\" attachments -- files that are contained within the directory containing the wiki -- vs. \\\"non-descendent\\\" attachments.\\n\\n<$checkbox tiddler=\\\"$:/config/TiddlyWebExternalAttachments/Enable\\\" field=\\\"text\\\" checked=\\\"yes\\\" unchecked=\\\"no\\\" default=\\\"yes\\\"> <$link to=\\\"$:/config/TiddlyWebExternalAttachments/Enable\\\">Enable importing files as external attachments</$link> </$checkbox>\\n\\n<$checkbox tiddler=\\\"$:/config/TiddlyWebExternalAttachments/OnlyBinary\\\" field=\\\"text\\\" checked=\\\"yes\\\" unchecked=\\\"no\\\" default=\\\"yes\\\"> <$link to=\\\"$:/config/TiddlyWebExternalAttachments/OnlyBinary\\\">only binary files as external attachments</$link> </$checkbox>\\n\\n<$link to=\\\"$:/config/TiddlyWebExternalAttachments/ExternalAttachmentsPath\\\">External Attachments Path</$link>: base path for external files <$edit-text tiddler=\\\"$:/config/TiddlyWebExternalAttachments/ExternalAttachmentsPath\\\" field=\\\"text\\\" tag=\\\"input\\\" default=\\\"files/\\\"></$edit-text>\\n\\n<$link to=\\\"$:/config/TiddlyWebExternalAttachments/SizeForExternal\\\">Size For External</$link>: use external attachment if file size large than <$edit-text tiddler=\\\"$:/config/TiddlyWebExternalAttachments/SizeForExternal\\\" field=\\\"text\\\" tag=\\\"input\\\" default=\\\"16384\\\"></$edit-text> Bytes\\n\"}}}","revision":"0","bag":"default"}]