Transclusion adds extraneous newline

I am in the process of migrating my website from Hugo to TW so I have one piece of software less to worry about. Whilst porting the templates I stumbled upon the fact that transclude and view seem to add an additional newline. This seems to happen for every HTML tag I want to transclude stuff into. Those newlines unfortunately breaks some things (the link in this example was just the first I noticed). I tried \whitespace trim, $mode="inline", $output="text/plain" and checked $:/config/blog/link for a newline at the end - which there is none.

title: $:/blog/template

`<!doctype html>
<html>
  <head>
    <link href="`<$transclude $tiddler="$:/config/blog/link" />`" />
  </head>
</html>`
title: $:/config/blog/link

https://example.com
> node ./tiddlywiki.js ~/blog --render "[!is[system]]" "" "text/plain" "$:/blog/template"

Output:

<!doctype html>
<html>
  <head>
    <link href="https://example.com
" />
  </head>
</html>

I guess I am missing some magic flag or something?

EDIT: @etardiff pointed out, that I forgot the backticks in the template tiddler.

Widgets (like $transclude and $view) aren’t meant to be used as attribute values; they’re intended for displaying content. Here, you’ll want to use a transcluded attribute value or a filtered attribute value instead. Note that both use curly braces, not widget syntax!

In place of

<link href="`<$transclude $tiddler="$:/config/blog/link" />`" />

try this:

<link href={{$:/config/blog/link}} />

The filtered transclusion <link href={{{ [[$:/config/blog/link]get[text]] }}} /> would also have the same output, but it’s more complicated than you need in this context.

Unfortunately that yields the same result. There is still a newline at the end of the inserted string.

<link href="`{{$:/config/blog/link}}`" />
<link href="https://example.com
" />

Your example still shows invalid syntax – can you check again that you used one of the syntaxes that @etardiff suggested?

You only use ` if you’re trying to substitute more than one thing into the attribute (and even then, what you’ve done isn’t valid syntax ­– you can only use variables or filters inside, not field transclusions). You could theoretically do:

<link href=`${ [{$:/config/blog/link}] }$`>

…but that’s way more complicated than necessary.

If I omit the backtick there does not happen any transclusion at all, since the whole template is wrapped in backticks. If I omit those (the ones around the whole template) I do not get any output.

Template:

<link href="{{$:/config/blog/link}}" />

Result:

<link href="{{$:/config/blog/link}}" />

I pretty much copied what is inside $:/core/templates/static.tiddler.html and modified so it would reflect my website. And there backticks are used.

Please note that my examples do not use backticks or quotes around the transclusion and try pasting the code I suggested, without modification. Quotes are usually used to indicate a literal value, which is probably why the transclusion isn’t happening.

I admit I don’t have any experience with static templates in particular; I can’t speak to what’s going on there. But your current code isn’t standard TW syntax, so I’m not surprised it doesn’t work as intended.

Tried skipping the quotes already, same result unfortunately :cry:

Template:

<link href={{$:/config/blog/link}} />

Result:

<link href={{$:/config/blog/link}} />

Thanks for clarifying. I took a closer look at $:/core/templates/static.tiddler.html and now see what you mean: the whole template, minus the transclusions, is wrapped in backticks which you didn’t show in your sample template code. The first line would thus look like this:

`<!doctype html>

and the backtick on <link href="` is actually marking the end of a code block, not the beginning of a code block (or substituted attribute).

Unfortunately the static template issue is outside my area of knowledge, and looking at the output of $:/core/templates/static.tiddler.html, I don’t see any reason why you’d get an extra newline from

<link href="`{{$:/config/blog/link}}`" />

but not

<meta name="tiddlywiki-version" content="`{{$:/core/templates/version}}`" />

which is used in the core tiddler… unless perhaps you have an extra newline hiding at the end of $:/config/blog/link?

I suggest amending your first post to include the initial and final backticks so other readers don’t have the same confusion. I hope someone else is able to help!

1 Like

Disclaimer: this is not my area of TW expertise either.

unless perhaps you have an extra newline hiding at the end of $:/config/blog/link ?

Hmm, on Unix-based systems every file ends with a newline by convention. @tjout, it sounds like you’re using a Node wiki at ~/blog, right? I wonder if something about the way rendering with the $:/blog/template save template works is just yanking the exact content of the file into the transclusion, and the file ends with a newline (as it should).

You said you checked for a newline at the end of the tiddler, but did you do that in the TW editor? I don’t think a file-ending newline would appear within the TW editor even if it was physically present in the file. Maybe you can check the file corresponding to $:/config/blog/link on your filesystem and see if it has a trailing newline. If so, you could try removing it and see if that changes the result.

No, I did it the usual way since stuff like this happened to me more than once in the past with Python, YAML and the likes^^. I opened the file in my text editor (Neovim) which did not show an additional line and to double check I ran cat.

> ~$ cat tiddlers/\$\ _config_blog_link.tid 
created: 20240601054431516
modified: 20240601054441212
tags: 
title: $:/config/blog/link

https://example.com> ~$

Since the new prompt starts on the same line the output of cat ends I concluded there is no new line.

Unfortunately the forum does not let me upload archives…

title: $:/blog/template/article

\whitespace trim
`<!DOCTYPE html>
<html lang="en">
	<head>
		<link href=`<$view tiddler="$:/config/blog/link" />` />
	</head>
</html>`
title: $:/config/blog/link

https://example.com
title: testarticle
created: 20220121211516400

lorem ipsum
#!/bin/sh

node tiddlywiki.js "./blog" --render "[!is[system]has[created]]" "[get[created]format:date[YYYY/0ddddd/0]addsuffix[.html]]" "text/plain" "$:/blog/template/article"

I am at checkout 268aaebaf0a45dafcfe985d25b78ac9dfca10f17 on the multi-wiki-support branch. Behaviour is identical at 64f5dd942cda4c0b6f332d2da30fa50e828fb265 on the master branch.

Hi @tjout thanks for raising this. These templates for static output can be very tricky. It’s a fairly subtle trick to reuse the wikification process in this way.

Great to hear. There are a lot of similarities between modern Static Site Generators and TiddlyWiki, and it should be an interesting migration.

Thank you for the clear description of the problem. I am travelling today and so can’t investigate properly. It is particularly strange given that the very similar core templates $:/core/templates/static.tiddler.html and $:/core/templates/tiddlywiki5.html do not appear to exhibit this problem.

I’ll investigate as soon as I can and respond here.

Quick notes

Unless designed to be saved as a html file you don’t need the doc type and it introduces a new line.

Use the internals plugin to see the resulting html in the previews and find how your extra line feed is occuring.