Repository: st3v3nmw/obsidian-spaced-repetition
Pull request #802 (2023-12-02 11:44:28)

Bug: review note corrupts metadata for files with cr/lf line endings 776
This fixes bug #776 , and includes unit test cases.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1bed66b..1cc39a6 120000
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,1 +1,1 @@
-docs/changelog.md
\ No newline at end of file
+docs/changelog.md
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 651dc17..9815d5b 120000
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -1,1 +1,1 @@
-docs/en/contributing.md
\ No newline at end of file
+docs/en/contributing.md
diff --git a/docs/changelog.md b/docs/changelog.md
index 2147fc1..593f97b 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. Dates are d
 
 Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
 
+#### [Unreleased]
+
+-   Bug fix: When reviewing an entire note, the metadata is applied incorrectly (on top of the already existing metadata) [`#776`](https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/776)
+
 #### [1.10.5](https://github.com/st3v3nmw/obsidian-spaced-repetition/compare/1.10.4...1.10.5)
 
 -   Bug edit flashcard sometimes corrupts question text in file [`#784`](https://github.com/st3v3nmw/obsidian-spaced-repetition/pull/784)
diff --git a/src/constants.ts b/src/constants.ts
index 7563cb5..8acd7a4 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -1,6 +1,8 @@
+// To cater for both LF and CR-LF line ending styles, "\r?\n" is used to match the newline character sequence
+// https://github.com/st3v3nmw/obsidian-spaced-repetition/issues/776
 export const SCHEDULING_INFO_REGEX =
-    /^---\n((?:.*\n)*)sr-due: (.+)\nsr-interval: (\d+)\nsr-ease: (\d+)\n((?:.*\n)?)---/;
-export const YAML_FRONT_MATTER_REGEX = /^---\n((?:.*\n)*?)---/;
+    /^---\r?\n((?:.*\r?\n)*)sr-due: (.+)\r?\nsr-interval: (\d+)\r?\nsr-ease: (\d+)\r?\n((?:.*\r?\n)?)---/;
+export const YAML_FRONT_MATTER_REGEX = /^---\r?\n((?:.*\r?\n)*?)---/;
 
 export const MULTI_SCHEDULING_EXTRACTOR = /!([\d-]+),(\d+),(\d+)/gm;
 export const LEGACY_SCHEDULING_EXTRACTOR = /<!--SR:([\d-]+),(\d+),(\d+)-->/gm;


Test patch:
diff --git a/tests/unit/util/utils.test.ts b/tests/unit/util/utils.test.ts
index d930316..49e0c51 100644
--- a/tests/unit/util/utils.test.ts
+++ b/tests/unit/util/utils.test.ts
@@ -1,3 +1,4 @@
+import { YAML_FRONT_MATTER_REGEX } from "src/constants";
 import { literalStringReplace } from "src/util/utils";
 
 describe("literalStringReplace", () => {
@@ -72,3 +73,21 @@ $$\\huge F_g=\\frac {G m_1 m_2}{d^2}$$
         expect(actual).toEqual(expectedStr);
     });
 });
+
+function createTestStr1(sep: string): string {
+    return `---${sep}sr-due: 2024-08-10${sep}sr-interval: 273${sep}sr-ease: 309${sep}---`;
+}
+
+describe("YAML_FRONT_MATTER_REGEX", () => {
+    test("New line is line feed", async () => {
+        const sep: string = String.fromCharCode(10);
+        const text: string = createTestStr1(sep);
+        expect(YAML_FRONT_MATTER_REGEX.test(text)).toEqual(true);
+    });
+
+    test("New line is carriage return line feed", async () => {
+        const sep: string = String.fromCharCode(13, 10);
+        const text: string = createTestStr1(sep);
+        expect(YAML_FRONT_MATTER_REGEX.test(text)).toEqual(true);
+    });
+});