New Features in TiddlyWiki 5.3.0: HTTP request and deserialize operator

Amongst the new features being introduced in TiddlyWiki v5.3.0 is the ability to make HTTP requests from wikitext. This will allow users to fetch data from other sources and convert them into tiddlers, amongst other things.

The new release will also include a deserialize[] filter operator that converts the various data formats that TW understands into a JSON representation of tiddlers.

Uses case like loading tiddlers from another wiki will now be entirely possible from wikitext as long as the server hosting the wiki allows cross domain communication (TiddlyHost, Github pages and others).

Here is a very quick reworking of my earlier code to use the core features and implement the functionality to load tiddlers from another wiki entirely in wikitext:

\define loadWikiActions(wikiURL)
	<!-- actions invoked after fetching the remote data -->
	\define getWikiCallback()
		<!-- actions to turn the remote data into tiddlers -->
		\define importTiddlers()
			<$let
				tiddlerJSON={{{[<data>deserialize[application/json]]}}}
				tiddlers={{{ [<tiddlerJSON>jsonindexes[]] :map[<tiddlerJSON>jsonget<currentTiddler>,[title]] :and[format:titlelist[]join[ ]] }}}
			>
				<!-- iterate over each position in the array -->
				<$list filter="[<tiddlerJSON>jsonindexes[]]" variable="index">
					<!-- get the tiddler at that position in the array from the JSON -->
					<$let
						tiddler={{{ [<tiddlerJSON>jsonextract<index>] }}} 
						title={{{ [<tiddler>jsonget[title]] }}}
					>
						<!-- make sure we have a title for the tiddler and exclude system tiddlers -->
						<$list filter="[<title>!is[blank]!is[system]]">
							<$action-setmultiplefields
								$fields="[<tiddler>jsonindexes[]] externalTiddler is_volatile includeTimestamp"
								$values="[<tiddler>jsonindexes[]] :map[<tiddler>jsonget<currentTiddler>] yes =yes [<now [UTC]YYYY0MM0DD0hh0mm0ssXXX>]"
								$timestamp="no"
								/>
						</$list>
					</$let>
				</$list>
			</$let>
		\end importTiddlers
		<!-- actions invoked if there is an error fetching the data -->
		\define failureHandler()
			<$action-log status="error fetching the wiki"/>
			<$action-setfield $tiddler={{{ [[$:/temp/http/error/]addsuffix<now [UTC]YYYY0MM0DD0hh0mm0ssXXX]>] }}} text={{{ [[There was an error fetching the wiki ]addsuffix<wikiURL>addsuffix<error>] }}} tags="$:/tags/Alert"/>
		\end failureHandler
		<$list filter="[<status>match[200]]" variable="null" emptyValue=<<failureHandler>> >
			<$action-log data=<<data>> status="succcess" />
			<<importTiddlers>>
		</$list>
	\end getWikiCallback

	<!-- fetch the remote data source-->
	\define getWikiActions()
		<$action-sendmessage
			$message="tm-http-request"
			method="GET"
			bind-status={{{ [[$:/temp/http/load-content/]addsuffix<wikiURL>] }}}
			oncompletion=<<getWikiCallback>>
			url=<<wikiURL>>
			var-wikiURL=<<wikiURL>>
			>
	\end getWikiActions
<<getWikiActions>>
\end loadWikiActions

In order to load the content from other wikis at startup, you would run the above actions at startup using the system tag $:/tags/StartupAction/PostRender

9 Likes