Skip to content

FIX: Plugin is not generating cards correctly, when the dataview YAML is already present in the md file. #225

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

Open
Bakterio opened this issue May 4, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Bakterio
Copy link

Bakterio commented May 4, 2025

Hi everyone,
I came across a bug. The plugin will crash, if I want to generate new cards, when I already have some YAML dataview in the beginning of the md file. It can't write the card-deck property. The cards are generated but they are not given IDs below the paragraph so that they cannot be updated later on.

I made a fix in .obsidian/plugins/flashcards-obsidian/main.js. I am using regex instead of hard-typing the string character position. I am attaching my fix, but I don't know, how to make a pull request to this project. I am not a js dev, just a student who is studiing for finals.

I would be glad, if someone can push my fix to official version of this plugin, so others doesn't have to deal with it like me.

This is the old code from line 6929 to line 6951.

    CardsService.prototype.updateFrontmatter = function (frontmatter, deckName) {
        var newFrontmatter = "";
        var cardsDeckLine = "cards-deck: ".concat(deckName, "\n");
        if (frontmatter) {
            var oldFrontmatter = this.file.substring(frontmatter.position.start.offset, frontmatter.position.end.offset);
            if (!oldFrontmatter.match(this.regex.cardsDeckLine)) {
                newFrontmatter =
                    oldFrontmatter.substring(0, oldFrontmatter.length - 3) +
                        cardsDeckLine +
                        "---";
                this.totalOffset += cardsDeckLine.length;
                this.file =
                    newFrontmatter +
                        this.file.substring(frontmatter.position.end.offset, this.file.length + 1);
            }
        }
        else {
            newFrontmatter = "---\n".concat(cardsDeckLine, "---\n\n");
            this.totalOffset += newFrontmatter.length;
            this.file = newFrontmatter + this.file;
        }
    };

This is my fix:

        CardsService.prototype.updateFrontmatter = function(frontmatter, deckName) {
            var newFrontmatter = "";
            var cardsDeckLine = "cards-deck: ".concat(deckName, "\n");
            if (frontmatter) {
                //var oldFrontmatter = this.file.substring(frontmatter.position.start.offset, frontmatter.position.end.offset);
                var oldFrontmatter = this.file.match(/---\s*([\s\S]*?)\s*---/)[0];
                if (!oldFrontmatter.match(this.regex.cardsDeckLine)) {
                    newFrontmatter = oldFrontmatter.substring(0, oldFrontmatter.length - 3) + cardsDeckLine + "---";
                    this.totalOffset += cardsDeckLine.length;
                    this.file = newFrontmatter + this.file.substring(oldFrontmatter.length);
                }
            } else {
                newFrontmatter = "---\n".concat(cardsDeckLine, "---\n\n");
                this.totalOffset += newFrontmatter.length;
                this.file = newFrontmatter + this.file;
            }
        }
@Bakterio Bakterio added the bug Something isn't working label May 4, 2025
Copy link

github-actions bot commented May 4, 2025

Thank you for taking the time to report the issue and help me to make the project better! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant