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 ).
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 :
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: