Include code from another note inside code block?

What I’m trying to do

I would love this for when I’m using the excellent D2 flowchart and diagram plugin, but I guess it would apply to many other types of code.

I have a number of D2 diagrams, which all use the same shape classes (“classes” are defined sets of formatting applied to shapes). At the moment I have to include the class definitions in every diagram code block.

Is there any way to “include” text from another note inside the code block?

So now I must do this:

```d2
classes: {  link: {
    shape: callout
    height: 80
    style: {  fill: hotpink  }
}
## lots of other class definitions
}

Link test.class: link
A -> B -> Link test

It would be great to do this:

```d2
![[D2 Class Definitions]]

Link test.class: link
A -> B -> Link test

Things I have tried

I tested the basic transclusion with ![[D2 Class Definitions]] but that gave an error. I also serahced around. Any ideas?

I’ve seen some possibilities with dataviewjs, but I can’t figure out how to make it work.

Perhaps something like this:

```dataviewjs
dv.paragraph("```d2");
const path1= "/D2 Code Class Definitions.md";
const fileContents1 = await dv.io.load(path1);
console.log(fileContents1);
const path2= "/D2 Code Diagram.md";
const fileContents2 = await dv.io.load(path2);
console.log(fileContents2);
dv.paragraph("```");

I’ve tried the above but it just displays “Generating…” and nothing else.

I think you need to collate the entire block before presenting it so try something like:

```dataviewjs
const path1= "/D2 Code Class Definitions.md";
const fileContents1 = await dv.io.load(path1);
console.log(fileContents1);
const path2= "/D2 Code Diagram.md";
const fileContents2 = await dv.io.load(path2);
console.log(fileContents2);
dv.paragraph(`
~~~d2
${ fileContents1 }
${ fileContents2 }
~~~
`);

Thank you!
This worked with some tweaks. I had to escape the symbols as below:

\~\~\~d2

Also, when including a D2 file, care has to be taken not to include non-breaking spaces in the D2 code, because it causes rendering errors.