Merging several notes

What I’m trying to do

For the last 8 months I have been gathering information for a fotoboek.
All that information is in a lot of different separate notes.
Now it’s time to send all this information to a writer, but instead of sending dozens of files I would like to send just one.

Things I have tried

The classic way of merging seems to be deleting the ‘original’ document, this I don’t want.

The closesd way I found was this:

Dataview list with ![[xxx]]

Is this the easist way ?

(I don’t have a definitive answer for you.)

  1. The thread you linked isn’t using Dataview. Those are basic embedded links.

  2. It depends how you want to send the document. This isn’t going to be a format you can send to someone. Because if you send that document directly, it will just be the ![[Something]] links. It won’t have any knowledge of the notes that are being linked.

  3. If you export the note as PDF, you’ll have all the content, but each embedded link will have an extra level of indentation on the side. And the data will be in a read-only PDF format.

  1. You could try the Long Form plugin, which has “a workflow based compilation tool that can create manuscripts from your projects.” But there might be a bit of a learning curve, and it might not immediately work with whatever structure your notes are in. If you search the forum for “Longform” or Long Form" you might find some specific discussions around exporting for writers, including additional workarounds.

  2. Enhancing Export plugin - Based on Pandoc, can export to things like HTML, DOCX, ePub, or PDF. (Never tried it. I’m just searching community plugins for “Export”)

  3. “Easiest” way with no learning curve might involve just copy/pasting the notes into a new note or something. Might be annoying, but you’ll get the job done.

3 Likes

@rigmarole gave a very good break down of various solutions. One thing I do to combat the issue addressed in item 2 for documents using ![[Something]] links is with with a special copy/paste…

  • first put the document into Reading Mode
  • select the entire document
  • right-click and select copy.
  • paste into a new note and it will have the full contents of the note originally created with links.

This also is very useful when a note contains Dataview elements. I have found that this method suffers in a couple ways.

  1. The formatting can change, especially spacing.
  2. Since we copied effectively from the HTML rendered version of the note, Obsidian changes all the links to [Note](app://obsidian.md/Note) which breaks the links for me in Obsidian. I use an external editor to do a regular expression search to move all those changes back into a format Obsidian supports, like WikiLinks. (If anyone knows how to change this behavior, I would love to hear a solution)

Why bother creating a document built from embedded links instead of just copying/pasting? It might be easier to move around note elements as embedded links first. Once you’ve arranged everything the way you like it, then you could just copy/paste from reading mode to get the final info. However, formatting issues might not make it worth the effort.

1 Like

Thank you,
About the indent, when embedding other files I found (on the forum ?) a css hack that removes this. clean-embeds

1 Like

Thank you all, for the helpful responses.
I’m going to take a look at the Longform plugin and the forum.

As time is not on my side, it’ll try to embed everything in one note and try to copy that. If the Long form does not help.

1 Like

Here is a very basic Templater script that recursively looks for embedded WikiLinks not of the form ![[note#something]] and extracts the embedded values into a new note. For simple markdown notes, this works OK. It can cause issues with dataview queries since the file paths change. It also won’t look good with embedded notes in lists since it will extract the whole file inside the list element. For example…

- ![[some_note]]

It requires you to…

  • create a JavaScript file named process_embedded_links_with_markdown.js in a subfolder of your vault (e.g. “Scripts”)
  • create a templater file (e.g. “Extract-Embedded-Template.md”)
  • tell the templater plugin where to look for javascript files (Settings->Community plugins->Templater->Script files folder location)
  • tell templater where the template file is located (Settings->Community plugins->Templater->Template folder location)

process_embedded_links_with_markdown.js

const fs = require('fs');
const path = require('path');

module.exports = async (tp) => {
    const inputFilePath = tp.file.path(true);
    let combinedContents = await tp.file.content; // Starting point is the original file

    let search = true;
    while (search) {
        // look for embedded links
        const embeddedLink = combinedContents.match(/!\[\[([^\]#.]+?)\]\]/); //ignores #sub-blocks and links with "." in name
        if (embeddedLink) {
            try {
                const file = tp.file.find_tfile(embeddedLink[1]); // embeddedLink[1] is link without ![[ ]]
                const relativeFilePath = file.path;
                // read the contents of the embedded link
                const linkFileContents = await tp.app.vault.adapter.read(relativeFilePath);
                // replace all instances of the embedded link with the contents of the note
                combinedContents = combinedContents.replaceAll(embeddedLink[0],`${linkFileContents}`);
            } catch (error) {
                search = false;
                new Notice(`Error extracting note: ${embeddedLink[0]}`);
                new Notice(`Terminating early. Not all embedded links were extracted!`);
                console.error(`Error extracting note: ${embeddedLink[0]}`, error);
            }
        } else {
            search = false;
        }
    }
    // Write the expanded note out to a new file
    const outputFilePath = inputFilePath.replace('.md', '_with_embedded_contents.md');
    await tp.app.vault.create(outputFilePath, combinedContents);
    // Open new file
    const file = tp.file.find_tfile(outputFilePath);
    app.workspace.activeLeaf.openFile(file);
    // Report skipped links
    const skippedLinks = combinedContents.match(/!\[\[([^\].]+?)\]\]/g); // ignore links with a "." in name
    new Notice(`Skipped Links: ${skippedLinks}`);
    console.info(`Skipped Links: ${skippedLinks}`);
};

Extract-Embedded-Template.md

<%* tp.user.process_embedded_links_with_markdown(tp) %>

Usage

To use this new template: Extract-Embedded-Template

  1. open the main file with the embedded links (e.g. note.md)
  2. run the new Extract-Embedded-Template template.
    • Ctrl-P to open the Command Palette
    • Templater: Open insert template modal
    • select Extract-Embedded-Template
  3. The script will recursively replace embedded WikiLinks, including embedded links contained in embedded notes.
  4. The script will report any links it skipped in a brief pop-up window.
  5. Look at the results
    • a new note based off your original note name will be created with the embedded files all extracted (e.g. note.mdnote_with_embedded_contents.md
    • if the script fails, it will display a quick message saying why it failed, and a file with the best effort will be created. The most likely reason for the failure is that there’s an embedded link to a missing note.

NOTE: once the output file (e.g. note_with_embedded_contents.md) is created, it must be deleted if you want to rerun the script on the original file again (e.g. note.md)

If for some reason the template failed because of some link issue, you can modify the new note_with_embedded_contents.md instead of the original notes to fix any issues with links and rerun the script on the updated note_with_embedded_contents.md to get another note.

1 Like

Incredible piece of work, thank you.
Saved me a lot of time, now I can send the text file instead of a pdf

2 Likes