What are TW parsing rules for newlines with a div element?

I noticed that if a div element is not followed by a newline then TW generates following HTML.

Example 1-a

TW markup:

<div>
eg 1
</div>

produces following HTML:

<p><div>
eg 1
</div></p>

Notice that div is surrounded by p.

Example 1-b

But if div is followed a by a newline like so in the following TW markup

<div>

eg 2
</div>

produces following HTML:

<div><p>eg 2
</p></div>

Notice, now div has p inside its body.

Example 2-a

Here is a macro example,

\define test()

<$transclude mode="block" tiddler="pdf-tiddler" />
\end

<<test>>

Produces this HTML:

<p><embed src="https://example.com/file.pdf"></p>

Notice how embed is surrounded by p.

Example 2-b

But if I insert a newline before \end like so

\define test()

<$transclude mode="block" tiddler="pdf-tiddler" />

\end

<<test>>

Produces this HTML:

<embed src="https://example.com/file.pdf">

p element disappears.

Example 3

I noticed similar issue in template. If a <$transclude mode="block" tiddler="pdf-tiddler" /> is preceded with two blank lines that the embed is not surrounded by p, otherwise it is surrounded by p.

Question

How does TW interpret newline when parsing?

Others will give a more technical answer and you can even find out how yourself, because its all there, in a readable form in tiddlywiki. It comes with its own source code. Here is one way to think of it.

If instead you wrote native HTML you would use a combination of inline and block elements and this will result in newlines being honoured, inserted or ignored according to how they are used in these elements.

Wiki text is not as complex and hard to use as HTML tags and its various special characters will be translated into HTML for display so the parsing process needs to make decisions how and when to translate the different content and when to use inline and block elements.

  • Some widgets even let you set inline or block mode, for most wikitext tiddlywiki makes educated guesses.
  • As a result a number of decisions were made to make this as strait forward and understandable as possible, basically to as “should we treat the content and inline or block”.
  • Inline and block thus produce different handling of the newline.

One of the ways to make this happen, happens to be “if a blank line follows, an opening tag treat is as a block element”.

There are workarounds and tricks for a few edge cases when what you want seems a little tricky, one of the simplest is just to use HTML.

Now I hand it to others with a more detailed understanding of the parsing.

As you found out there are some problems in the existing parser.

This I Would Like to Fix the redundant P-tag Problem may be of interest.

1 Like

As far as opening tags of elements are concerned, if the opening tag of an element is followed by two line breaks then the content of the element is parsed in block mode, otherwise it is parsed in inline mode. In block mode paragraph tags are automatically generated and block level formatting commands like headings are recognised.

There’s some documentation on this topic linked from here:

https://tiddlywiki.com/#WikiText%20Parser%20Modes

1 Like