Move inline fields en masse to the first line of a file

I have around a thousand notes that have inline fields, something like this:

# File Name

![[embedded image]]

---
title:: abc
date:: 2022-08-08
director:: [[Ab Cdee]]
actors:: [[x ray]], [[bizzy bobs]], [[hunker munker]]
key:: value
anotherkey:: another value
---

## summary

paragraphs of text

All of the inline fields are bound by three opening and three closing dashes (---) on their own lines.

Is it possible to use regex (in VS Code) or something else to capture everything between the opening and closing dashes and move all the lines to the top of the files to create properties-friendly YAML headers? Like this:

---
title:: abc
date:: 2022-08-08
director:: [[Ab Cdee]]
actors:: [[x ray]], [[bizzy bobs]], [[hunker munker]]
key:: value
anotherkey:: another value
---

# File Name

![[embedded image]]

## summary

paragraphs of text

I have looked online but not found a way to do what I want to do.

Thanks in advance.

This isn’t a simple find-and-replace operation, so you’ll need a shell script to parse the header, find the first two occurrences of the yaml hyphens, then copy the lines in between into a list, copy whatever else comes before that yaml block to another list, then rewrite your note and put both lines in the right order, add the rest of your note minus the lines you copied and put this routine in a loop to process notes in a folder / your vault.

1 Like

Many thanks for the detailed advice.

Way beyond what I can do, so those notes will just have to stay as they are with inline fields.

Grateful. :+1: :grin:

I could lose all the opening text, I guess. So is it possible to search for and delete everything from the start of each file to the first set of dashes? That would move all of the inline fields to the top of each file.

Thanks again for any tips.

C’mon you won’t lose anything.

Btw, how about using chatgp or how this free ai service is called? :stuck_out_tongue_winking_eye:

1 Like

Think I should be able to figure out the regex to capture:

# XXXXXXX

XXXXX
---
title

If I then replace with:

---
title

… all of the files will open with the triple-dash needed for a YAML header. If I figure it out, I will share the regex.

VS cide is (mostly) capable of doing this, but there are a lot of caveats you need to handle. Here are sime of them:

  • how to handle if you’ve used the triple dashes for a horizontal line somewhere and not bas start or end of a block?
  • inline fields use double colons, not a single colon, to separate key and value
  • inline fields doesn’t use quotes around links, but yaml needs that
  • list syntax has slightly different syntax in the various incantations of inline vs yaml
  • how will you handle notes having a yaml block from already? How to merge together a yaml block and an inline block?
  • if I’ve understood it correctly, there is also the slight matter of inline field links being recognised as outlinks, while yaml links (currently?) are not?

In other words it’s not just as “simple” as either moving the first block after the other, or removing lines in front of your inline field blocks. So care should be taken before proceeding. Thread carefully…

1 Like

Hi and thanks.

All of these notes only have inline fields, so nothing to merge.

I can deal with :: to : using a simple search in VS Code.

I can make links using find [[ replace "[[and find ]] replace ]]"

Each file actually starts like:

[![Unbreakable|200](https://m.media-amazon.com/images/M/MV5BMDIwMjAxNzktNmEzYS00ZDY5LWEyZjktM2Y0MmUzZDkyYmZkXkEyXkFqcGdeQXVyNTA4NzY1MzY@._V1_SX300.jpg)](https://www.imdb.com/title/tt0217869)
***

# Unbreakable (2000) | [IMDb](https://www.imdb.com/title/tt0217869)   

---
Aka::   

So I need to match between the opening [![ and Aka::.

I can then replace that with:

---
Aka:

If it works, all the notes will have moved the inline fields to the top of the file. I can them clean them up.

Will make a backup first, of course.

After a couple of hours of regex nowhere-near misses, happiness turns out to be:

  1. Listen to @Jopp
  2. Leave the notes with inline fields
  3. There is no step 3

Thanks to Jopp and holroy.

EDIT: For anyone else with a similar issue, this post at least got the first line:

^(?<![\s\S\r]).*

If you want to torment(?) yourself more, or want to pursue this issue another time, I recommend reading this post where I did something similar.

It displays how I dealt with some of the caveats in a similar case, and it might give you some suggestions as to what kind of regex’s you’ll need to handle your case as well.

1 Like

Of note:

1 Like

Thanks for the notes, @holroy and @gino_m.

Python is way out of my tiny sphere of knowledge. Would be useful if Linter offered this feature, as discussed in the thread linked above and on GitHub.

I realized that I could use the regex above to repeatedly remove the first line, so I did that several times until it reached the triple-dash YAML header.

Have been running various search and replace ideas on the text and it is getting closer to what I want.

Just using a sandbox of test data at the moment. If I can really get things pretty good, I will either do a clean run of all the search and replaces or swap the sandbox data into my main vault.

Not going to be completely accurate but probably good enough until a problem crops up and needs to be rectified by hand.

I probably would have gone down the same route: that way I can measure and applaud some progress.
But – as I learned the hard way a year ago myself, overbiting with my scant regex forte – there are many unknowns down the road. So a sandbox idea was a good one.

1 Like

VS code and regex can really turn around and bite you. I have made a lot of mistakes in my time … :rofl:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.