Skip to content

insertNoteIDs ,failed. (clinging Code that might work) #146

@beiyuming

Description

@beiyuming

Problem:
The current insertNoteIDs logic only inserts the <!-- notecardId: ... --> comment if the card front exactly matches the Markdown header line (e.g., ## Question). If the front spans multiple lines or there are formatting differences, the comment is not inserted.

Cause:
The code converts each card’s front (question) to markdown and tries to strictly match it with the ## header line in the markdown file.
However, in practice, the card front may include not only the header but also the lines below it (sometimes everything before %).
For example, the card front might be:

Apply to transformer....
extra explanation

But the markdown header line is only:

## Apply to transformer....

Since these are not exactly the same, the match fails and the notecardId is not inserted.

Solution:
Change the logic to insert the notecardId comment after every header (e.g., every ##), in order, without matching the content.This maybe works well as long as each card is associated with a single ## header.

Suggested code:

insertNoteIDs(cards: Card[]) {
  let lines = this.source.cachedContent.split("\n");
  let match = new RegExp(this.getConfig("card.separator") as string);

  let headerLineNumbers: number[] = [];
  lines.forEach((element, index) => {
    if (match.exec(element)) {
      headerLineNumbers.push(index);
    }
  });

  const editor = window.activeTextEditor;
  editor?.edit((editBuilder) => {
    headerLineNumbers.forEach((lineIndex, i) => {
      const noteID = cards[i]?.noteId;
      if (noteID) {
        editBuilder.insert(new Position(lineIndex + 1, 0), `\n<!-- notecardId: ${noteID} -->\n`);
      }
    });
  });
}

This makes the insertion robust for all Markdown styles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions