Using transclusion to add html or css

My uses for transclusion

The classics description for using transclusion is to assemble a book from its chapters which are individual markdown files (it nearly always seems to be Alice in Wonderland as the example :thinking:).

I initially tried transclusion as a means of putting ‘standard text’ into documents, but it didn’t really work as I often needed to edit the included text to match the rest of the document. ‘Snippet’ tools like Apple’s keyboard text replacement, Alfred snippets, Keyboard Maestro, Text Expander, Devonthink’s Xmenu and other tools worked better as they injected text that could then be modified.

Recently I found a good uses for transclusion :sunglasses::

1. Adding a first page ‘header’ to a document

Depending on the client, I may need to put a different logo at top of a document. Here is an example where the ‘header’ is created in its own md document:

![][logo]

[logo]: images/Avocet_chick.png style="float: right; width:5%; "
<div style="clear:both"></div>

----

If this is added to the top of the main document:

{{Test logo.md}}

# Document with header on first page

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In nisl nisi scelerisque eu. Dictum sit amet justo donec. Quis auctor elit sed vulputate mi sit amet mauris commodo. Proin sed libero enim sed faucibus. Neque gravida in fermentum et sollicitudin ac orci phasellus. In hendrerit gravida rutrum quisque non tellus orci ac. Leo integer malesuada nunc 

This results in:

2. One off CSS overrides

An example of this is changing headers to numbered headers for a document. The following style css is included in its own md document:

<style>
body {
counter-reset: head head2 head3;
}
h1:has(~ h2) {
counter-reset: head2;
}
h2:has(~h3) {
counter-reset: head3;
}
h1::before, h2::before, h3::before {
padding-right: .5rem;
}
h1::before {
counter-increment: head;
content: counter(head)".";
margin-left: 0;
}
h2::before {
counter-increment: head2;
content: counter(head)"."counter(head2);
}
h3::before {
counter-increment: head3;
content: counter(head)"."counter(head2)"."counter(head3);
}
</style>

If this is added to the top of a markdown document with headers:

{{Numbered headings}}

# Heading 1

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In nisl nisi scelerisque eu. Dictum sit amet justo donec. Quis auctor elit sed vulputate mi sit amet mauris commodo. Proin sed libero enim sed faucibus. 

## Sub Heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In nisl nisi scelerisque eu. Dictum sit amet justo donec. Quis auctor elit sed vulputate mi sit amet mauris commodo. Proin sed libero enim sed faucibus. 

### Sub sub heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In nisl nisi scelerisque eu. Dictum sit amet justo donec. Quis auctor elit sed vulputate mi sit amet mauris commodo. Proin sed libero enim sed faucibus. 

## Sub heading

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. In nisl nisi scelerisque eu. Dictum sit amet justo donec. Quis auctor elit sed vulputate mi sit amet mauris commodo. Proin sed libero enim sed faucibus. 

The result is numbered headers:

You can even mix and match:

Interesting concept

I am curious in your business use cases why you prefer transclusion over inserting the same text as text expander snippets.

They key difference I see is that transclusion allows you to change every reference to that text simultaneously - including documents you have already written. It seems to me a classic example of how this could be useful is changing a policy statement or even a logo on a blog or website where you do indeed want to change even previously written content.

But for documents you send to clients, might you want or need a permanent copy of the document exactly as the client received it? If your logo changes do you want last month’s or last year’s invoices to reflect a logo that did not really exist then?

@rkaplan - you are quite correct. The ‘header’ could indeed be injected using a snippet. I just find it quick to add {{logo}} at the top of the document (which usually ends up as a PDF before distribution so it is ‘frozen’).

The numbered headings idea allows me to quickly turn numbered headings on and off by adding or deleting one line.

1 Like

Thanks for sharing your find :slight_smile:

Why do you have clear: both in your header? That’s only needed with float which in turn shouldn’t be needed anymore (some exceptions exist)

Belt and braces as I do float the logo to the right in the header.

I suggest having a look at display:flex. float is a bit rusty around the edges.

1 Like

I remember this came up somewhere before, which also lead me to play around with it. It’s a nice setup: set general preferences in your main stylesheet, but easily override parts of it for certain types of documents.

(… Found it, @vinschger and others discuss it in this thread. It also has some tips if you want to get more advanced in styling your header counters):

Your example not related to styling is interesting for inspiration. But as rkaplan mention, one needs to consider if/when a permanent record is important.

If you are not aware, you can also use MMD metadata as placeholders. Very useful for templates! You can set a key: value in the metadata header, and put ‌[%key] wherever you need it in the body text (one or multiple times). The placeholder is then substituted on render/export.

So you can create templates with a set of predefined metadata, filled out or not as it makes sense. I guess you can even combine this with transclusion.

iA Writer gives this demonstration example (from their help page on metadata):

I never tried but was curious if it works with image links. It seems not, at least I can’t get it to work so far. I tried in DEVONthink with relative links, x-devonthink-item://, and https:// – in different variations of key/placeholder:

key placeholder
logo: ![](https://...) ‌[%logo]
logo: https://... ![]([%logo])
logo: (https://...) ![][%logo]
logo: [](https://...) ![%logo]

But I think this is a minor detail. Most would likely use it for general text.

Edit to add: The official MMD User Guide does say this:

Metadata is processed as plain text, so it should not include MultiMarkdown markup.

So I guess it makes sense that you can’t put a full ![](image-link) in the header. But is a bare URL considered markup? Seems like an edge case.

1 Like