What I’m trying to do
I want to automatically convert text from webpages into YAML fields in my Obsidian notes. The text follows this format:
[manga title]
Year of Release: [year]
Region: [country]
Genre: [genre]
Author: [author]
Status: [status]
Description:
[manga description]
When I paste in text in this format, i want it to be automatically converted into YAML fields when added to my notes.
---
manga title: [manga title]
Year of Release: [year]
Region: [country]
Genre: [genre]
Author: [author]
Status: [status]
Description: [manga description]
---
Things I have tried
I tried using the Templater plugin in Obsidian with a JavaScript script to convert the text into YAML. I have zero knowledge in coding so I just asked chatGPT to write me some code. This is what it gave me:
Templater Script (convert-to-yaml.js):
async function convertToYAML(tp) {
const inputText = tp.file.content.trim();
const lines = inputText.split('\n');
const getField = (regex) => {
const match = lines.find(line => regex.test(line));
return match ? match.match(regex)[1].trim() : '';
}
const data = {
title: lines[0].trim(),
year: getField(/Year of Release: (\d+)/),
region: getField(/Region: ([^]+)/),
genre: getField(/Genre: ([^]+)/),
author: getField(/Author: (.+?)\(/),
status: getField(/Status: ([^。]+)/),
description: lines[6].trim()
};
const yamlData = YAML.stringify(data, { indent: 2, allowUnicode: true });
return `---\n${yamlData}---\n`;
}
module.exports = convertToYAML;
then put this in my template note:
---
<%* const yamlContent = await tp.user.convertToYAML(tp) %>
<%= yamlContent %>
---
but I keep having errors like “Template parsing error, aborting” and “tp.user.convertToYAML is not a function”. I’ve tried fixed suggested by chatGPT. but they don’t work.
I also searched for terms like “Obsidian YAML automation”, “Obsidian Templater script”, and “Obsidian auto-convert text to YAML” but haven’t found a solution yet.