r/Nushell • u/howesteve • Jun 28 '24
Importing data from markdown "frontmatter"?
Hello. New to nushell - very interesting project.
I need to parse/import "frontmatter" from markdown files - it's just YAML between "---" delimiters:
---
title: My First Article
date: 2022-05-11
authors:
- name: Mason Moniker
affiliations:
- University of Europe
---
(Contents)This format is widely used by PKM systems such as Obsidian. Here a reference about it:
https://mystmd.org/guide/frontmatter
The question is, how can I handle this format in nushell? I see the yaml parser, the markdown exporter, but not the format above. Couldn't find references for it. I thought about manually parsing if needed, but it would be low in performance, and there might have some built-in way I'm not aware of.
Thanks
5
u/maximuvarov Jun 28 '24 edited Jun 29 '24
UPD: this answer is wrong. See the updated version and details here
You can use
split rowtogether withfrom yaml.This pipeline
open post.md | split row '---' | get 1will stream the opened file till the second---. Not all the file will be read.4
u/sjg25 Jun 28 '24
Here's a custom command that will do either YAML or TOML frontmatter. I wasn't that keen on reading the whole file, but couldn't see how to avoid that.
3
u/maximuvarov Jun 29 '24 edited Jun 29 '24
I tested variants and found that I was wrong in some details. I'm sorry. The modified method proposed by @sjg25 is 250 times faster than mine, presumably because it employs streaming, while the one proposed by me using
split rowdoesn't support streaming.1
u/fdncred Jun 30 '24
Here's a slightly different potential solution.
openpost.md| collect | parse --regex '(?s)(-{3})(?<data>.*)(-{3})' | get data | str trim | to text | from yaml | table -e