Replace links to other Drafts notes with their text

I am looking for a way to essentially create a table of contents note with links to other Drafts notes (sometimes multiple levels deep) and stitch together a single new note that I could then decide what actions to taken on it. I would be able to use this for long-form writing as well as things like requirements where I don’t want to have large notes that require navigating within to find and edit what I am looking for.

Have not found any actions to support this. Is anyone aware of any that I may have missed?

1 Like

What are you conceiving that the action would do for you exactly?

Here are just a few interpretations that I think could match the first part of the above, but are each significantly different.

  • Allow you to select a bunch of Drafts and generate a new draft containing a list of drafts URLs to each one.
  • One by one allow you to choose drafts in order and let you buIild a table of contents that is the name of the draft and is set at a level of indentation related to the heading level of the first line of the draft, and output as a Markdown style link to the related draft.
  • Allow you to select a draft and generate a Markdown link that you can paste into another draft so you can manually build a table of contents.

The fact that you then want an action to stitch drafts together sounds like a different function again.

Is this something instead of or in addition to another action? Do you want an action to build a table of contents in some way and merge the linked drafts in order?

Great questions and thank you for the quick reply! Right now I am just looking for a way to merge all the notes with drafts5:// links from a master note (TOC) into one new note in the order in which they appear in the master. That way I can work in bite-sized pieces but build a full document that I can preview, publish, print, etc.

There are a few details such as do you use the link Title in any way or maintain formatting from the master such as indentation but if I get the merging action I am confident I could tweak it to handle those as needed.

Generating a list of links for ordering as per your first bullet would be helpful but not required.

This could certainly be done. I don’t have time to write up and example today, but the recommended method would be to use regular expression matching to find the drafts5:// links in the text and separate the UUID parameter, then use it to lookup the draft content using Draft.find(uuid).

Excellent. Thank you! So basically:

  1. Create a new draft that is a copy of the TOC
  2. Use regex to find the drafts links
  3. Pull out the UID from the link
  4. Use find with the UID to get the the linked draft
  5. Get its content and replace the link in the new draft.

Only question left is if there is an easy way to do the duplicate of the TOC into a new draft? I didn’t see anything in the API but know that I can call create.

Thanks again!

I assume you are starting on the draft with the TOC. I would do something like the below, structurally:

let toc = draft.content; // get TOC content in a var
let newContent = toc; // start new content with TOC

// write a regex with a capture group () to get the UUID
// I won't pretend to get that right off the top of my head
let re = /matching-regex/g // not sure exact regex

// loop over the matches, replacing URLs matched with draft content
let matches = toc.match(re);
for (let match of matches) {
    let uuid = ?; // depends on your regex capture group
    let d = Draft.find(uuid);
    newContent = newContent + d.content + "\n\n";

let newDraft = Draft.create();
newDraft.content = newContent;

Exactly. I was a few steps behind you on the script but you nailed it. Very much appreciate your help.