Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metaedit doesn't edit arrays of tags properly. #94

Open
Smitty010 opened this issue May 19, 2023 · 1 comment
Open

Metaedit doesn't edit arrays of tags properly. #94

Smitty010 opened this issue May 19, 2023 · 1 comment

Comments

@Smitty010
Copy link

Smitty010 commented May 19, 2023

Not sure whether I'm looking for a bug fix or an enhancement.

I tried to update my tags in the frontmatter using the frontmatter editor:

tags: 
- state/inprogress
- course-flash/writing-obsidian-plugins

I edited the string and ended up with:

tags: state/finished,course-flash/writing-obsidian-plugins
- state/inprogress
- course-flash/writing-obsidian-plugins

Not what I was expecting. I'm not sure how you are getting the original value of the tags ("state/inprogress,course-flash/writing-obsidian-plugins"), but either you should display a warning saying you don't edit arrays or it should work. Just my 2 cents. I should probably mention I did this via the metaedit command in the command palette, not the api

@vorpalvorpal
Copy link

vorpalvorpal commented Aug 14, 2023

I had the same problem. The issue is that metaedit splits the file content by "/n", searches for the key in each line, and then replaces the line when it finds the appropriate key. I edited the "updatePropertyInFile" function to instead split the file content into frontmatter and body content. It then uses obsidian's inbuilt parseYaml and stringifyYaml functions on the frontmatter, and uses the existing approach on the body content.

     async updatePropertyInFile(property, newValue, file) {
        const YAMLregex = /((^---\s)([\s\S]*?[\r\n])(---\s)){0,1}([\s\S]*)/;
        const rawContent = await this.app.vault.read(file);
        const fileContent = rawContent.match(YAMLregex);
        var parsedYaml = obsidian.parseYaml(fileContent[3]);
          parsedYaml[property.key] = newValue
        const newBody = fileContent[5].split("\n").map(line => {
            if (this.lineMatch(property, line)) {
                return this.updatePropertyLine(property, newValue, line);
            }
            return line;
        }).join("\n");
        const newFileContent = fileContent[2] + obsidian.stringifyYaml(parsedYaml) + fileContent[4] + newBody
        await this.app.vault.modify(file, newFileContent);
    }

Arrays in the frontmatter work correctly for me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants