Find and replace specific lines in Draft

Hey, I’m trying to cleanup some metadata before I import a Draft into Obsidian. Obsidian can’t have “: / |” in the backlinks but often a podcast or episode title contains them. I’ve used the regex to ignore “podcast:” and “episode:” so those ":"s aren’t replaced.

I know the lines will always be [6] and [9] and so I want to replace all instances of these symbols with " -" before I import them. Would be great if I could run this on multiple Drafts at once.

Here’s the code I have so far:

// clean up Drafts metadata
// define regex to use...
const regexOne = /(?<!podcast):/gm;
const regexTwo = /(?<!episode):/gm;
// define replacement expression...
const replaceWith = " -";

let podcastName = draft.lines[6];
let episodeName = draft.lines[9];

// do the replacement...
draft.content = draft.lines[6].replace(regexOne, replaceWith);
episodeName = draft.lines[9].replace(regexTwo, replaceWith);
draft.update();

As you probably guessed, this replaces the whole Draft with line [6] and I’m not sure how to target just line [6] or [9].

Here is an example of the metadata. I haven’t tried replacing “/” or “|” yet. Just working on the “:” for now.

# How To Start Your First Business in 48 Hours - Noah Kagan - 00:16:11
---
class: podcast clip
type: podcast clip
date added: 2024-02-03
time added: 07:26:51
podcast: [[Deep Dive: with Ali Abdaal]]
creator-host: 
guests: 
episode: [[How To Start Your First Business in 48 Hours: Noah Kagan]]

Any pointers on how to target those lines when updating draft.content would be greatly appreciated. Cheers!

If you would allow me to redefine your problem a little more simply, it sounds like you want to replace any instances of “:” that appear inside a [[wiki-link]] style link, right? Really doesn’t matter where they occur. That could be done with:

// regex to find all `[[link]]` style links
let re = /(\[\[[^\]]+\]\])/gm

// loop over results, replacing occurrences of “:” with “ -“
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll(":", " -")
})
draft.update()
1 Like

@agiletortoise Yes! Thanks for this.

I originally tried to figure out how to do this with just regex but couldn’t figure out how to match only the wiki-links.

I updated the code you sent so I could replace other symbols, this feels very repetitive and I’m sure there’s a better way. lol. But it’s working!

// regex to find all `[[link]]` style links
let re = /(\[\[[^\]]+\]\])/gm

// loop over results, replacing occurrences of “:” with “ -“
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll(":", " -")
})
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll("/", "-")
})
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll("/", "-")
})
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll("|", "-")
})
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll("#", "")
})
draft.update()

Thanks again!

Well, if it works, no need to change, but you could simplify by doing all that in one replace. The way I wrote that replace, it’s using a function to do the replacements, which returns the result you want inserted. That function can do more than one thing, so this would work fine:


// regex to find all `[[link]]` style links
let re = /(\[\[[^\]]+\]\])/gm

// loop over results, replacing occurrences of “:” with “ -“
draft.content = draft.content.replace(re, function(match, text) {
	return text.replaceAll(":", " -")
               .replaceAll("/", "-")
	           .replaceAll("/", "-")
               // etc, etc.
})
draft.update()
1 Like

Much better. Thanks!